19aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Copyright 2010 Google Inc.
29aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//
39aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// This code is licensed under the same terms as WebM:
49aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//  Software License Agreement:  http://www.webmproject.org/license/software/
59aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
69aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// -----------------------------------------------------------------------------
79aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//
89aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// speed-critical functions.
99aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//
109aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Author: Skal (pascal.massimino@gmail.com)
119aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
129aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#include "vp8i.h"
139aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
149aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#if defined(__cplusplus) || defined(c_plusplus)
159aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldextern "C" {
169aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#endif
179aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
199aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// run-time tables (~4k)
209aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
219aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic uint8_t abs0[255 + 255 + 1];     // abs(i)
229aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic uint8_t abs1[255 + 255 + 1];     // abs(i)>>1
239aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic int8_t sclip1[1020 + 1020 + 1];  // clips [-1020, 1020] to [-128, 127]
249aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic int8_t sclip2[112 + 112 + 1];    // clips [-112, 112] to [-16, 15]
259aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic uint8_t clip1[255 + 510 + 1];    // clips [-255,510] to [0,255]
269aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2703d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// We declare this variable 'volatile' to prevent instruction reordering
2803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// and make sure it's set to true _last_ (so as to be thread-safe)
2903d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arorastatic volatile int tables_ok = 0;
309aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
3103d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Aroravoid VP8DspInitTables(void) {
329aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  if (!tables_ok) {
339aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    int i;
349aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    for (i = -255; i <= 255; ++i) {
359aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      abs0[255 + i] = (i < 0) ? -i : i;
369aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      abs1[255 + i] = abs0[255 + i] >> 1;
379aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
389aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    for (i = -1020; i <= 1020; ++i) {
399aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      sclip1[1020 + i] = (i < -128) ? -128 : (i > 127) ? 127 : i;
409aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    for (i = -112; i <= 112; ++i) {
429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      sclip2[112 + i] = (i < -16) ? -16 : (i > 15) ? 15 : i;
439aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
449aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    for (i = -255; i <= 255 + 255; ++i) {
459aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      clip1[255 + i] = (i < 0) ? 0 : (i > 255) ? 255 : i;
469aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tables_ok = 1;
489aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
499aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
519aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline uint8_t clip_8b(int v) {
529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
539aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
549aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
559aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Transforms (Paragraph 14.4)
579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
589aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#define STORE(x, y, v) \
599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  dst[x + y * BPS] = clip_8b(dst[x + y * BPS] + ((v) >> 3))
609aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
619aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic const int kC1 = 20091 + (1 << 16);
629aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic const int kC2 = 35468;
639aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#define MUL(a, b) (((a) * (b)) >> 16)
649aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
65466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arorastatic void TransformOne(const int16_t* in, uint8_t* dst) {
669aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int C[4 * 4], *tmp;
679aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  tmp = C;
699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 4; ++i) {    // vertical pass
709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a = in[0] + in[8];    // [-4096, 4094]
719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int b = in[0] - in[8];    // [-4095, 4095]
729aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int c = MUL(in[4], kC2) - MUL(in[12], kC1);   // [-3783, 3783]
739aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int d = MUL(in[4], kC1) + MUL(in[12], kC2);   // [-3785, 3781]
749aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp[0] = a + d;   // [-7881, 7875]
759aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp[1] = b + c;   // [-7878, 7878]
769aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp[2] = b - c;   // [-7878, 7878]
779aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp[3] = a - d;   // [-7877, 7879]
789aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp += 4;
799aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    in++;
809aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
819aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  // Each pass is expanding the dynamic range by ~3.85 (upper bound).
829aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  // The exact value is (2. + (kC1 + kC2) / 65536).
839aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  // After the second pass, maximum interval is [-3794, 3794], assuming
849aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  // an input in [-2048, 2047] interval. We then need to add a dst value
859aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  // in the [0, 255] range.
869aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  // In the worst case scenario, the input to clip_8b() can be as large as
879aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  // [-60713, 60968].
889aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  tmp = C;
899aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 4; ++i) {    // horizontal pass
909aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int dc = tmp[0] + 4;
919aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a =  dc +  tmp[8];
929aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int b =  dc -  tmp[8];
939aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int c = MUL(tmp[4], kC2) - MUL(tmp[12], kC1);
949aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int d = MUL(tmp[4], kC1) + MUL(tmp[12], kC2);
959aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    STORE(0, 0, a + d);
969aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    STORE(1, 0, b + c);
979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    STORE(2, 0, b - c);
989aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    STORE(3, 0, a - d);
999aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp++;
1009aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    dst += BPS;
1019aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
1029aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
1039aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#undef MUL
1049aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
105466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arorastatic void TransformTwo(const int16_t* in, uint8_t* dst, int do_two) {
106466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  TransformOne(in, dst);
107466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (do_two) {
108466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    TransformOne(in + 16, dst + 4);
109466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
110466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
111466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
1129aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void TransformUV(const int16_t* in, uint8_t* dst) {
113466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8Transform(in + 0 * 16, dst, 1);
114466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8Transform(in + 2 * 16, dst + 4 * BPS, 1);
1159aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
1169aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1179aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void TransformDC(const int16_t *in, uint8_t* dst) {
1189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int DC = in[0] + 4;
1199aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i, j;
1209aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (j = 0; j < 4; ++j) {
1219aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    for (i = 0; i < 4; ++i) {
1229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      STORE(i, j, DC);
1239aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
1249aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
1259aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
1269aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1279aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void TransformDCUV(const int16_t* in, uint8_t* dst) {
1289aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  if (in[0 * 16]) TransformDC(in + 0 * 16, dst);
1299aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  if (in[1 * 16]) TransformDC(in + 1 * 16, dst + 4);
1309aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  if (in[2 * 16]) TransformDC(in + 2 * 16, dst + 4 * BPS);
1319aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  if (in[3 * 16]) TransformDC(in + 3 * 16, dst + 4 * BPS + 4);
1329aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
1339aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1349aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#undef STORE
1359aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1369aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// default C implementations:
137466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraVP8Idct2 VP8Transform = TransformTwo;
1389aea642eefa7a641ab8b89d953251939221d2719Eric HassoldVP8Idct VP8TransformUV = TransformUV;
1399aea642eefa7a641ab8b89d953251939221d2719Eric HassoldVP8Idct VP8TransformDC = TransformDC;
1409aea642eefa7a641ab8b89d953251939221d2719Eric HassoldVP8Idct VP8TransformDCUV = TransformDCUV;
1419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
1439aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Paragraph 14.3
1449aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1459aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void TransformWHT(const int16_t* in, int16_t* out) {
1469aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int tmp[16];
1479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
1489aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 4; ++i) {
1499aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a0 = in[0 + i] + in[12 + i];
1509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a1 = in[4 + i] + in[ 8 + i];
1519aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a2 = in[4 + i] - in[ 8 + i];
1529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a3 = in[0 + i] - in[12 + i];
1539aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp[0  + i] = a0 + a1;
1549aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp[8  + i] = a0 - a1;
1559aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp[4  + i] = a3 + a2;
1569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    tmp[12 + i] = a3 - a2;
1579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
1589aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 4; ++i) {
1599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int dc = tmp[0 + i * 4] + 3;    // w/ rounder
1609aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a0 = dc             + tmp[3 + i * 4];
1619aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a1 = tmp[1 + i * 4] + tmp[2 + i * 4];
1629aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a2 = tmp[1 + i * 4] - tmp[2 + i * 4];
1639aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const int a3 = dc             - tmp[3 + i * 4];
1649aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    out[ 0] = (a0 + a1) >> 3;
1659aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    out[16] = (a3 + a2) >> 3;
1669aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    out[32] = (a0 - a1) >> 3;
1679aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    out[48] = (a3 - a2) >> 3;
1689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    out += 64;
1699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
1709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
1719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1729aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8TransformWHT)(const int16_t* in, int16_t* out) = TransformWHT;
1739aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1749aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
1759aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Intra predictions
1769aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1779aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#define OUT(x, y) dst[(x) + (y) * BPS]
1789aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1799aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline void TrueMotion(uint8_t *dst, int size) {
1809aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const uint8_t* top = dst - BPS;
1819aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const uint8_t* const clip0 = clip1 + 255 - top[-1];
1829aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int y;
1839aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (y = 0; y < size; ++y) {
1849aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    const uint8_t* const clip = clip0 + dst[-1];
1859aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    int x;
1869aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    for (x = 0; x < size; ++x) {
1879aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      dst[x] = clip[top[x]];
1889aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
1899aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    dst += BPS;
1909aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
1919aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
1929aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void TM4(uint8_t *dst)   { TrueMotion(dst, 4); }
1939aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void TM8uv(uint8_t *dst) { TrueMotion(dst, 8); }
1949aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void TM16(uint8_t *dst)  { TrueMotion(dst, 16); }
1959aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1969aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
1979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 16x16
1989aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
1999aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void VE16(uint8_t *dst) {     // vertical
2009aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int j;
2019aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (j = 0; j < 16; ++j) {
2029aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    memcpy(dst + j * BPS, dst - BPS, 16);
2039aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
2049aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2059aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2069aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void HE16(uint8_t *dst) {     // horizontal
2079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int j;
2089aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (j = 16; j > 0; --j) {
2099aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    memset(dst, dst[-1], 16);
2109aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    dst += BPS;
2119aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
2129aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2139aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2149aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline void Put16(int v, uint8_t* dst) {
2159aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int j;
2169aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (j = 0; j < 16; ++j) {
2179aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    memset(dst + j * BPS, v, 16);
2189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
2199aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2209aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2219aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void DC16(uint8_t *dst) {    // DC
2229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int DC = 16;
2239aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int j;
2249aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (j = 0; j < 16; ++j) {
2259aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    DC += dst[-1 + j * BPS] + dst[j - BPS];
2269aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
2279aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  Put16(DC >> 5, dst);
2289aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2299aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2309aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void DC16NoTop(uint8_t *dst) {   // DC with top samples not available
2319aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int DC = 8;
2329aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int j;
2339aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (j = 0; j < 16; ++j) {
2349aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    DC += dst[-1 + j * BPS];
2359aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
2369aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  Put16(DC >> 4, dst);
2379aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2389aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2399aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void DC16NoLeft(uint8_t *dst) {  // DC with left samples not available
2409aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int DC = 8;
2419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
2429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 16; ++i) {
2439aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    DC += dst[i - BPS];
2449aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
2459aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  Put16(DC >> 4, dst);
2469aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2489aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void DC16NoTopLeft(uint8_t *dst) {  // DC with no top and left samples
2499aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  Put16(0x80, dst);
2509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2519aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
2539aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 4x4
2549aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2559aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
2569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#define AVG2(a, b) (((a) + (b) + 1) >> 1)
2579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2589aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void VE4(uint8_t *dst) {    // vertical
2599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const uint8_t* top = dst - BPS;
2609aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const uint8_t vals[4] = {
2619aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    AVG3(top[-1], top[0], top[1]),
2629aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    AVG3(top[ 0], top[1], top[2]),
2639aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    AVG3(top[ 1], top[2], top[3]),
2649aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    AVG3(top[ 2], top[3], top[4])
2659aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  };
2669aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
2679aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 4; ++i) {
2689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    memcpy(dst + i * BPS, vals, sizeof(vals));
2699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
2709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2729aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void HE4(uint8_t *dst) {    // horizontal
2739aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int A = dst[-1 - BPS];
2749aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int B = dst[-1];
2759aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int C = dst[-1 + BPS];
2769aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int D = dst[-1 + 2 * BPS];
2779aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int E = dst[-1 + 3 * BPS];
2789aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  *(uint32_t*)(dst + 0 * BPS) = 0x01010101U * AVG3(A, B, C);
2799aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  *(uint32_t*)(dst + 1 * BPS) = 0x01010101U * AVG3(B, C, D);
2809aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  *(uint32_t*)(dst + 2 * BPS) = 0x01010101U * AVG3(C, D, E);
2819aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  *(uint32_t*)(dst + 3 * BPS) = 0x01010101U * AVG3(D, E, E);
2829aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2839aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2849aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void DC4(uint8_t *dst) {   // DC
2859aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  uint32_t dc = 4;
2869aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
2879aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 4; ++i) dc += dst[i - BPS] + dst[-1 + i * BPS];
2889aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  dc >>= 3;
2899aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 4; ++i) memset(dst + i * BPS, dc, 4);
2909aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
2919aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
2929aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void RD4(uint8_t *dst) {   // Down-right
2939aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int I = dst[-1 + 0 * BPS];
2949aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int J = dst[-1 + 1 * BPS];
2959aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int K = dst[-1 + 2 * BPS];
2969aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int L = dst[-1 + 3 * BPS];
2979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int X = dst[-1 - BPS];
2989aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int A = dst[0 - BPS];
2999aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int B = dst[1 - BPS];
3009aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int C = dst[2 - BPS];
3019aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int D = dst[3 - BPS];
3029aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 3)                                     = AVG3(J, K, L);
3039aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 2) = OUT(1, 3)                         = AVG3(I, J, K);
3049aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 1) = OUT(1, 2) = OUT(2, 3)             = AVG3(X, I, J);
3059aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 0) = OUT(1, 1) = OUT(2, 2) = OUT(3, 3) = AVG3(A, X, I);
3069aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 0) = OUT(2, 1) = OUT(3, 2)             = AVG3(B, A, X);
3079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(2, 0) = OUT(3, 1)                         = AVG3(C, B, A);
3089aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 0)                                     = AVG3(D, C, B);
3099aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
3109aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
3119aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void LD4(uint8_t *dst) {   // Down-Left
3129aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int A = dst[0 - BPS];
3139aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int B = dst[1 - BPS];
3149aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int C = dst[2 - BPS];
3159aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int D = dst[3 - BPS];
3169aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int E = dst[4 - BPS];
3179aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int F = dst[5 - BPS];
3189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int G = dst[6 - BPS];
3199aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int H = dst[7 - BPS];
3209aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 0)                                     = AVG3(A, B, C);
3219aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 0) = OUT(0, 1)                         = AVG3(B, C, D);
3229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(2, 0) = OUT(1, 1) = OUT(0, 2)             = AVG3(C, D, E);
3239aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 0) = OUT(2, 1) = OUT(1, 2) = OUT(0, 3) = AVG3(D, E, F);
3249aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 1) = OUT(2, 2) = OUT(1, 3)             = AVG3(E, F, G);
3259aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 2) = OUT(2, 3)                         = AVG3(F, G, H);
3269aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 3)                                     = AVG3(G, H, H);
3279aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
3289aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
3299aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void VR4(uint8_t *dst) {   // Vertical-Right
3309aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int I = dst[-1 + 0 * BPS];
3319aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int J = dst[-1 + 1 * BPS];
3329aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int K = dst[-1 + 2 * BPS];
3339aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int X = dst[-1 - BPS];
3349aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int A = dst[0 - BPS];
3359aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int B = dst[1 - BPS];
3369aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int C = dst[2 - BPS];
3379aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int D = dst[3 - BPS];
3389aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 0) = OUT(1, 2) = AVG2(X, A);
3399aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 0) = OUT(2, 2) = AVG2(A, B);
3409aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(2, 0) = OUT(3, 2) = AVG2(B, C);
3419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 0)             = AVG2(C, D);
3429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
3439aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 3) =             AVG3(K, J, I);
3449aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 2) =             AVG3(J, I, X);
3459aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 1) = OUT(1, 3) = AVG3(I, X, A);
3469aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 1) = OUT(2, 3) = AVG3(X, A, B);
3479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(2, 1) = OUT(3, 3) = AVG3(A, B, C);
3489aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 1) =             AVG3(B, C, D);
3499aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
3509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
3519aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void VL4(uint8_t *dst) {   // Vertical-Left
3529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int A = dst[0 - BPS];
3539aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int B = dst[1 - BPS];
3549aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int C = dst[2 - BPS];
3559aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int D = dst[3 - BPS];
3569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int E = dst[4 - BPS];
3579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int F = dst[5 - BPS];
3589aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int G = dst[6 - BPS];
3599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int H = dst[7 - BPS];
3609aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 0) =             AVG2(A, B);
3619aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 0) = OUT(0, 2) = AVG2(B, C);
3629aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(2, 0) = OUT(1, 2) = AVG2(C, D);
3639aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 0) = OUT(2, 2) = AVG2(D, E);
3649aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
3659aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 1) =             AVG3(A, B, C);
3669aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 1) = OUT(0, 3) = AVG3(B, C, D);
3679aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(2, 1) = OUT(1, 3) = AVG3(C, D, E);
3689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 1) = OUT(2, 3) = AVG3(D, E, F);
3699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold              OUT(3, 2) = AVG3(E, F, G);
3709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold              OUT(3, 3) = AVG3(F, G, H);
3719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
3729aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
3739aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void HU4(uint8_t *dst) {   // Horizontal-Up
3749aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int I = dst[-1 + 0 * BPS];
3759aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int J = dst[-1 + 1 * BPS];
3769aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int K = dst[-1 + 2 * BPS];
3779aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int L = dst[-1 + 3 * BPS];
3789aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 0) =             AVG2(I, J);
3799aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(2, 0) = OUT(0, 1) = AVG2(J, K);
3809aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(2, 1) = OUT(0, 2) = AVG2(K, L);
3819aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 0) =             AVG3(I, J, K);
3829aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 0) = OUT(1, 1) = AVG3(J, K, L);
3839aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 1) = OUT(1, 2) = AVG3(K, L, L);
3849aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 2) = OUT(2, 2) =
3859aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    OUT(0, 3) = OUT(1, 3) = OUT(2, 3) = OUT(3, 3) = L;
3869aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
3879aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
3889aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void HD4(uint8_t *dst) {  // Horizontal-Down
3899aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int I = dst[-1 + 0 * BPS];
3909aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int J = dst[-1 + 1 * BPS];
3919aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int K = dst[-1 + 2 * BPS];
3929aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int L = dst[-1 + 3 * BPS];
3939aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int X = dst[-1 - BPS];
3949aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int A = dst[0 - BPS];
3959aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int B = dst[1 - BPS];
3969aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int C = dst[2 - BPS];
3979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
3989aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 0) = OUT(2, 1) = AVG2(I, X);
3999aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 1) = OUT(2, 2) = AVG2(J, I);
4009aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 2) = OUT(2, 3) = AVG2(K, J);
4019aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(0, 3)             = AVG2(L, K);
4029aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4039aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(3, 0)             = AVG3(A, B, C);
4049aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(2, 0)             = AVG3(X, A, B);
4059aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 0) = OUT(3, 1) = AVG3(I, X, A);
4069aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 1) = OUT(3, 2) = AVG3(J, I, X);
4079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 2) = OUT(3, 3) = AVG3(K, J, I);
4089aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  OUT(1, 3)             = AVG3(L, K, J);
4099aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
4109aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4119aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#undef AVG3
4129aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#undef AVG2
4139aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4149aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
4159aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Chroma
4169aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4179aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void VE8uv(uint8_t *dst) {    // vertical
4189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int j;
4199aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (j = 0; j < 8; ++j) {
4209aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    memcpy(dst + j * BPS, dst - BPS, 8);
4219aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
4229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
4239aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4249aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void HE8uv(uint8_t *dst) {    // horizontal
4259aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int j;
4269aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (j = 0; j < 8; ++j) {
4279aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    memset(dst, dst[-1], 8);
4289aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    dst += BPS;
4299aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
4309aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
4319aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4329aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// helper for chroma-DC predictions
4339aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline void Put8x8uv(uint64_t v, uint8_t* dst) {
4349aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int j;
4359aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (j = 0; j < 8; ++j) {
4369aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    *(uint64_t*)(dst + j * BPS) = v;
4379aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
4389aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
4399aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4409aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void DC8uv(uint8_t *dst) {     // DC
4419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int dc0 = 8;
4429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
4439aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 8; ++i) {
4449aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    dc0 += dst[i - BPS] + dst[-1 + i * BPS];
4459aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
4469aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  Put8x8uv((uint64_t)((dc0 >> 4) * 0x0101010101010101ULL), dst);
4479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
4489aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4499aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void DC8uvNoLeft(uint8_t *dst) {   // DC with no left samples
4509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int dc0 = 4;
4519aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
4529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 8; ++i) {
4539aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    dc0 += dst[i - BPS];
4549aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
4559aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  Put8x8uv((uint64_t)((dc0 >> 3) * 0x0101010101010101ULL), dst);
4569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
4579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4589aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void DC8uvNoTop(uint8_t *dst) {  // DC with no top samples
4599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int dc0 = 4;
4609aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
4619aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 8; ++i) {
4629aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    dc0 += dst[-1 + i * BPS];
4639aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
4649aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  Put8x8uv((uint64_t)((dc0 >> 3) * 0x0101010101010101ULL), dst);
4659aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
4669aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4679aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void DC8uvNoTopLeft(uint8_t *dst) {    // DC with nothing
4689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  Put8x8uv(0x8080808080808080ULL, dst);
4699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
4709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
4729aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// default C implementations
4739aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
47403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas AroraVP8PredFunc VP8PredLuma4[NUM_BMODES] = {
4759aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  DC4, TM4, VE4, HE4, RD4, VR4, LD4, VL4, HD4, HU4
4769aea642eefa7a641ab8b89d953251939221d2719Eric Hassold};
4779aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
47803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas AroraVP8PredFunc VP8PredLuma16[NUM_B_DC_MODES] = {
4799aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  DC16, TM16, VE16, HE16,
4809aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  DC16NoTop, DC16NoLeft, DC16NoTopLeft
4819aea642eefa7a641ab8b89d953251939221d2719Eric Hassold};
4829aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
48303d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas AroraVP8PredFunc VP8PredChroma8[NUM_B_DC_MODES] = {
4849aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  DC8uv, TM8uv, VE8uv, HE8uv,
4859aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  DC8uvNoTop, DC8uvNoLeft, DC8uvNoTopLeft
4869aea642eefa7a641ab8b89d953251939221d2719Eric Hassold};
4879aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4889aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
4899aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Edge filtering functions
4909aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
4919aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 4 pixels in, 2 pixels out
4929aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline void do_filter2(uint8_t* p, int step) {
4939aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
4949aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a = 3 * (q0 - p0) + sclip1[1020 + p1 - q1];
4959aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a1 = sclip2[112 + ((a + 4) >> 3)];
4969aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a2 = sclip2[112 + ((a + 3) >> 3)];
4979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[-step] = clip1[255 + p0 + a2];
4989aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[    0] = clip1[255 + q0 - a1];
4999aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5009aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5019aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 4 pixels in, 4 pixels out
5029aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline void do_filter4(uint8_t* p, int step) {
5039aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
5049aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a = 3 * (q0 - p0);
5059aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a1 = sclip2[112 + ((a + 4) >> 3)];
5069aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a2 = sclip2[112 + ((a + 3) >> 3)];
5079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a3 = (a1 + 1) >> 1;
5089aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[-2*step] = clip1[255 + p1 + a3];
5099aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[-  step] = clip1[255 + p0 + a2];
5109aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[      0] = clip1[255 + q0 - a1];
5119aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[   step] = clip1[255 + q1 - a3];
5129aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5139aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5149aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 6 pixels in, 6 pixels out
5159aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline void do_filter6(uint8_t* p, int step) {
5169aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
5179aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int q0 = p[0], q1 = p[step], q2 = p[2*step];
5189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a = sclip1[1020 + 3 * (q0 - p0) + sclip1[1020 + p1 - q1]];
5199aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a1 = (27 * a + 63) >> 7;  // eq. to ((3 * a + 7) * 9) >> 7
5209aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a2 = (18 * a + 63) >> 7;  // eq. to ((2 * a + 7) * 9) >> 7
5219aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int a3 = (9  * a + 63) >> 7;  // eq. to ((1 * a + 7) * 9) >> 7
5229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[-3*step] = clip1[255 + p2 + a3];
5239aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[-2*step] = clip1[255 + p1 + a2];
5249aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[-  step] = clip1[255 + p0 + a1];
5259aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[      0] = clip1[255 + q0 - a1];
5269aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[   step] = clip1[255 + q1 - a2];
5279aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  p[ 2*step] = clip1[255 + q2 - a3];
5289aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5299aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5309aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline int hev(const uint8_t* p, int step, int thresh) {
5319aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
5329aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  return (abs0[255 + p1 - p0] > thresh) || (abs0[255 + q1 - q0] > thresh);
5339aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5349aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5359aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline int needs_filter(const uint8_t* p, int step, int thresh) {
5369aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
5379aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  return (2 * abs0[255 + p0 - q0] + abs1[255 + p1 - q1]) <= thresh;
5389aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5399aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5409aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline int needs_filter2(const uint8_t* p, int step, int t, int it) {
5419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int p3 = p[-4*step], p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
5429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  const int q0 = p[0], q1 = p[step], q2 = p[2*step], q3 = p[3*step];
5439aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  if ((2 * abs0[255 + p0 - q0] + abs1[255 + p1 - q1]) > t)
5449aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    return 0;
5459aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  return abs0[255 + p3 - p2] <= it && abs0[255 + p2 - p1] <= it &&
5469aea642eefa7a641ab8b89d953251939221d2719Eric Hassold         abs0[255 + p1 - p0] <= it && abs0[255 + q3 - q2] <= it &&
5479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold         abs0[255 + q2 - q1] <= it && abs0[255 + q1 - q0] <= it;
5489aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5499aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
5519aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Simple In-loop filtering (Paragraph 15.2)
5529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5539aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void SimpleVFilter16(uint8_t* p, int stride, int thresh) {
5549aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
5559aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 16; ++i) {
5569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    if (needs_filter(p + i, stride, thresh)) {
5579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      do_filter2(p + i, stride);
5589aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
5599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
5609aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5619aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5629aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void SimpleHFilter16(uint8_t* p, int stride, int thresh) {
5639aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int i;
5649aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (i = 0; i < 16; ++i) {
5659aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    if (needs_filter(p + i * stride, 1, thresh)) {
5669aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      do_filter2(p + i * stride, 1);
5679aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
5689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
5699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5719aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void SimpleVFilter16i(uint8_t* p, int stride, int thresh) {
5729aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int k;
5739aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (k = 3; k > 0; --k) {
5749aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    p += 4 * stride;
5759aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    SimpleVFilter16(p, stride, thresh);
5769aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
5779aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5789aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5799aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void SimpleHFilter16i(uint8_t* p, int stride, int thresh) {
5809aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int k;
5819aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (k = 3; k > 0; --k) {
5829aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    p += 4;
5839aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    SimpleHFilter16(p, stride, thresh);
5849aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
5859aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5869aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5879aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
5889aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Complex In-loop filtering (Paragraph 15.3)
5899aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
5909aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline void FilterLoop26(uint8_t* p, int hstride, int vstride, int size,
5919aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                                int thresh, int ithresh, int hev_thresh) {
5929aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  while (size-- > 0) {
5939aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    if (needs_filter2(p, hstride, thresh, ithresh)) {
5949aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      if (hev(p, hstride, hev_thresh)) {
5959aea642eefa7a641ab8b89d953251939221d2719Eric Hassold        do_filter2(p, hstride);
5969aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      } else {
5979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold        do_filter6(p, hstride);
5989aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      }
5999aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
6009aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    p += vstride;
6019aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
6029aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6039aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6049aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic inline void FilterLoop24(uint8_t* p, int hstride, int vstride, int size,
6059aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                                int thresh, int ithresh, int hev_thresh) {
6069aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  while (size-- > 0) {
6079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    if (needs_filter2(p, hstride, thresh, ithresh)) {
6089aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      if (hev(p, hstride, hev_thresh)) {
6099aea642eefa7a641ab8b89d953251939221d2719Eric Hassold        do_filter2(p, hstride);
6109aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      } else {
6119aea642eefa7a641ab8b89d953251939221d2719Eric Hassold        do_filter4(p, hstride);
6129aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      }
6139aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
6149aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    p += vstride;
6159aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
6169aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6179aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// on macroblock edges
6199aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void VFilter16(uint8_t* p, int stride,
6209aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                      int thresh, int ithresh, int hev_thresh) {
6219aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop26(p, stride, 1, 16, thresh, ithresh, hev_thresh);
6229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6239aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6249aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void HFilter16(uint8_t* p, int stride,
6259aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                      int thresh, int ithresh, int hev_thresh) {
6269aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop26(p, 1, stride, 16, thresh, ithresh, hev_thresh);
6279aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6289aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6299aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// on three inner edges
6309aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void VFilter16i(uint8_t* p, int stride,
6319aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                       int thresh, int ithresh, int hev_thresh) {
6329aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int k;
6339aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (k = 3; k > 0; --k) {
6349aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    p += 4 * stride;
6359aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    FilterLoop24(p, stride, 1, 16, thresh, ithresh, hev_thresh);
6369aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
6379aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6389aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6399aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void HFilter16i(uint8_t* p, int stride,
6409aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                       int thresh, int ithresh, int hev_thresh) {
6419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  int k;
6429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  for (k = 3; k > 0; --k) {
6439aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    p += 4;
6449aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    FilterLoop24(p, 1, stride, 16, thresh, ithresh, hev_thresh);
6459aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
6469aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6489aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 8-pixels wide variant, for chroma filtering
6499aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void VFilter8(uint8_t* u, uint8_t* v, int stride,
6509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                     int thresh, int ithresh, int hev_thresh) {
6519aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop26(u, stride, 1, 8, thresh, ithresh, hev_thresh);
6529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop26(v, stride, 1, 8, thresh, ithresh, hev_thresh);
6539aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6549aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6559aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void HFilter8(uint8_t* u, uint8_t* v, int stride,
6569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                     int thresh, int ithresh, int hev_thresh) {
6579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop26(u, 1, stride, 8, thresh, ithresh, hev_thresh);
6589aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop26(v, 1, stride, 8, thresh, ithresh, hev_thresh);
6599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6609aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6619aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void VFilter8i(uint8_t* u, uint8_t* v, int stride,
6629aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                      int thresh, int ithresh, int hev_thresh) {
6639aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop24(u + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
6649aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop24(v + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
6659aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6669aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6679aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldstatic void HFilter8i(uint8_t* u, uint8_t* v, int stride,
6689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold                      int thresh, int ithresh, int hev_thresh) {
6699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop24(u + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
6709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  FilterLoop24(v + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
6719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6729aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6739aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
6749aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6759aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8VFilter16)(uint8_t*, int, int, int, int) = VFilter16;
6769aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8HFilter16)(uint8_t*, int, int, int, int) = HFilter16;
6779aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8VFilter8)(uint8_t*, uint8_t*, int, int, int, int) = VFilter8;
6789aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8HFilter8)(uint8_t*, uint8_t*, int, int, int, int) = HFilter8;
6799aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8VFilter16i)(uint8_t*, int, int, int, int) = VFilter16i;
6809aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8HFilter16i)(uint8_t*, int, int, int, int) = HFilter16i;
6819aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8VFilter8i)(uint8_t*, uint8_t*, int, int, int, int) = VFilter8i;
6829aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8HFilter8i)(uint8_t*, uint8_t*, int, int, int, int) = HFilter8i;
6839aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6849aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8SimpleVFilter16)(uint8_t*, int, int) = SimpleVFilter16;
6859aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8SimpleHFilter16)(uint8_t*, int, int) = SimpleHFilter16;
6869aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8SimpleVFilter16i)(uint8_t*, int, int) = SimpleVFilter16i;
6879aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldvoid (*VP8SimpleHFilter16i)(uint8_t*, int, int) = SimpleHFilter16i;
6889aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
6899aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//-----------------------------------------------------------------------------
690466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// SSE2 detection.
691466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora//
692466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
693466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#if defined(__pic__) && defined(__i386__)
694466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arorastatic inline void GetCPUInfo(int cpu_info[4], int info_type) {
695466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  __asm__ volatile (
696466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    "mov %%ebx, %%edi\n"
697466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    "cpuid\n"
698466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    "xchg %%edi, %%ebx\n"
699466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
700466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    : "a"(info_type));
701466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
702466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#elif defined(__i386__) || defined(__x86_64__)
703466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arorastatic inline void GetCPUInfo(int cpu_info[4], int info_type) {
704466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  __asm__ volatile (
705466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    "cpuid\n"
706466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
707466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    : "a"(info_type));
708466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
709466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#elif defined(_MSC_VER)  // Visual C++
710466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#define GetCPUInfo __cpuid
711466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#endif
712466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
713466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER)
714466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arorastatic int x86CPUInfo(CPUFeature feature) {
715466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  int cpu_info[4];
716466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  GetCPUInfo(cpu_info, 1);
717466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (feature == kSSE2) {
718466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return 0 != (cpu_info[3] & 0x04000000);
719466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
720466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (feature == kSSE3) {
721466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return 0 != (cpu_info[2] & 0x00000001);
722466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
723466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return 0;
724466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
725466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraVP8CPUInfo VP8DecGetCPUInfo = x86CPUInfo;
726466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#else
727466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraVP8CPUInfo VP8DecGetCPUInfo = NULL;
728466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#endif
729466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
730466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora//-----------------------------------------------------------------------------
731466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
732466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Aroraextern void VP8DspInitSSE2(void);
7339aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
73403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Aroravoid VP8DspInit(void) {
735466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
736466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (VP8DecGetCPUInfo) {
737466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    if (VP8DecGetCPUInfo(kSSE2)) {
738466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#if defined(__SSE2__) || defined(_MSC_VER)
739466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora      VP8DspInitSSE2();
740466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#endif
741466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    }
742466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    if (VP8DecGetCPUInfo(kSSE3)) {
743466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora      // later we'll plug some SSE3 variant here
744466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    }
745466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
7469aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
7479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
7489aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#if defined(__cplusplus) || defined(c_plusplus)
7499aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}    // extern "C"
7509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#endif
751