avg.c 6.78 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
James Zern's avatar
James Zern committed
10 11 12
#include <stdlib.h>

#include "./vpx_dsp_rtcd.h"
13 14
#include "vpx_ports/mem.h"

James Zern's avatar
James Zern committed
15
unsigned int vpx_avg_8x8_c(const uint8_t *s, int p) {
16 17
  int i, j;
  int sum = 0;
clang-format's avatar
clang-format committed
18 19 20
  for (i = 0; i < 8; ++i, s += p)
    for (j = 0; j < 8; sum += s[j], ++j) {
    }
21 22 23

  return (sum + 32) >> 6;
}
24

James Zern's avatar
James Zern committed
25
unsigned int vpx_avg_4x4_c(const uint8_t *s, int p) {
26 27
  int i, j;
  int sum = 0;
clang-format's avatar
clang-format committed
28 29 30
  for (i = 0; i < 4; ++i, s += p)
    for (j = 0; j < 4; sum += s[j], ++j) {
    }
31 32 33 34

  return (sum + 8) >> 4;
}

35 36
// src_diff: first pass, 9 bit, dynamic range [-255, 255]
//           second pass, 12 bit, dynamic range [-2040, 2040]
37
static void hadamard_col8(const int16_t *src_diff, ptrdiff_t src_stride,
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
                          int16_t *coeff) {
  int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
  int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
  int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
  int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
  int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
  int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
  int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
  int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];

  int16_t c0 = b0 + b2;
  int16_t c1 = b1 + b3;
  int16_t c2 = b0 - b2;
  int16_t c3 = b1 - b3;
  int16_t c4 = b4 + b6;
  int16_t c5 = b5 + b7;
  int16_t c6 = b4 - b6;
  int16_t c7 = b5 - b7;

  coeff[0] = c0 + c4;
  coeff[7] = c1 + c5;
  coeff[3] = c2 + c6;
  coeff[4] = c3 + c7;
  coeff[2] = c0 - c4;
  coeff[6] = c1 - c5;
  coeff[1] = c2 - c6;
  coeff[5] = c3 - c7;
}

Johann's avatar
Johann committed
67 68
// The order of the output coeff of the hadamard is not important. For
// optimization purposes the final transpose may be skipped.
69
void vpx_hadamard_8x8_c(const int16_t *src_diff, ptrdiff_t src_stride,
70
                        tran_low_t *coeff) {
71 72
  int idx;
  int16_t buffer[64];
73
  int16_t buffer2[64];
74 75
  int16_t *tmp_buf = &buffer[0];
  for (idx = 0; idx < 8; ++idx) {
76 77
    hadamard_col8(src_diff, src_stride, tmp_buf);  // src_diff: 9 bit
                                                   // dynamic range [-255, 255]
78 79 80 81 82 83
    tmp_buf += 8;
    ++src_diff;
  }

  tmp_buf = &buffer[0];
  for (idx = 0; idx < 8; ++idx) {
84 85 86 87
    hadamard_col8(tmp_buf, 8, buffer2 + 8 * idx);  // tmp_buf: 12 bit
    // dynamic range [-2040, 2040]
    // buffer2: 15 bit
    // dynamic range [-16320, 16320]
88 89
    ++tmp_buf;
  }
90 91

  for (idx = 0; idx < 64; ++idx) coeff[idx] = (tran_low_t)buffer2[idx];
92 93 94
}

// In place 16x16 2D Hadamard transform
95
void vpx_hadamard_16x16_c(const int16_t *src_diff, ptrdiff_t src_stride,
96
                          tran_low_t *coeff) {
97
  int idx;
98
  for (idx = 0; idx < 4; ++idx) {
99
    // src_diff: 9 bit, dynamic range [-255, 255]
clang-format's avatar
clang-format committed
100 101
    const int16_t *src_ptr =
        src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
James Zern's avatar
James Zern committed
102
    vpx_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
103 104
  }

105
  // coeff: 15 bit, dynamic range [-16320, 16320]
106
  for (idx = 0; idx < 64; ++idx) {
107 108 109 110
    tran_low_t a0 = coeff[0];
    tran_low_t a1 = coeff[64];
    tran_low_t a2 = coeff[128];
    tran_low_t a3 = coeff[192];
111

112 113 114 115
    tran_low_t b0 = (a0 + a1) >> 1;  // (a0 + a1): 16 bit, [-32640, 32640]
    tran_low_t b1 = (a0 - a1) >> 1;  // b0-b3: 15 bit, dynamic range
    tran_low_t b2 = (a2 + a3) >> 1;  // [-16320, 16320]
    tran_low_t b3 = (a2 - a3) >> 1;
116

clang-format's avatar
clang-format committed
117 118
    coeff[0] = b0 + b2;  // 16 bit, [-32640, 32640]
    coeff[64] = b1 + b3;
119 120
    coeff[128] = b0 - b2;
    coeff[192] = b1 - b3;
121 122 123 124 125

    ++coeff;
  }
}

126 127
// coeff: 16 bits, dynamic range [-32640, 32640].
// length: value range {16, 64, 256, 1024}.
128
int vpx_satd_c(const tran_low_t *coeff, int length) {
129 130
  int i;
  int satd = 0;
clang-format's avatar
clang-format committed
131
  for (i = 0; i < length; ++i) satd += abs(coeff[i]);
132

133
  // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
James Zern's avatar
James Zern committed
134
  return satd;
135 136
}

137
// Integer projection onto row vectors.
138
// height: value range {16, 32, 64}.
Johann's avatar
Johann committed
139
void vpx_int_pro_row_c(int16_t hbuf[16], const uint8_t *ref,
140 141
                       const int ref_stride, const int height) {
  int idx;
142
  const int norm_factor = height >> 1;
143 144 145
  for (idx = 0; idx < 16; ++idx) {
    int i;
    hbuf[idx] = 0;
146
    // hbuf[idx]: 14 bit, dynamic range [0, 16320].
clang-format's avatar
clang-format committed
147
    for (i = 0; i < height; ++i) hbuf[idx] += ref[i * ref_stride];
148
    // hbuf[idx]: 9 bit, dynamic range [0, 510].
149
    hbuf[idx] /= norm_factor;
150 151 152 153
    ++ref;
  }
}

154
// width: value range {16, 32, 64}.
Johann's avatar
Johann committed
155
int16_t vpx_int_pro_col_c(const uint8_t *ref, const int width) {
156 157
  int idx;
  int16_t sum = 0;
158
  // sum: 14 bit, dynamic range [0, 16320]
clang-format's avatar
clang-format committed
159
  for (idx = 0; idx < width; ++idx) sum += ref[idx];
160
  return sum;
161 162
}

163 164 165
// ref: [0 - 510]
// src: [0 - 510]
// bwl: {2, 3, 4}
clang-format's avatar
clang-format committed
166
int vpx_vector_var_c(const int16_t *ref, const int16_t *src, const int bwl) {
167
  int i;
168 169 170 171
  int width = 4 << bwl;
  int sse = 0, mean = 0, var;

  for (i = 0; i < width; ++i) {
172 173 174
    int diff = ref[i] - src[i];  // diff: dynamic range [-510, 510], 10 bits.
    mean += diff;                // mean: dynamic range 16 bits.
    sse += diff * diff;          // sse:  dynamic range 26 bits.
175 176
  }

177
  // (mean * mean): dynamic range 31 bits.
178 179
  var = sse - ((mean * mean) >> (bwl + 2));
  return var;
180 181
}

James Zern's avatar
James Zern committed
182
void vpx_minmax_8x8_c(const uint8_t *s, int p, const uint8_t *d, int dp,
183 184 185 186 187 188
                      int *min, int *max) {
  int i, j;
  *min = 255;
  *max = 0;
  for (i = 0; i < 8; ++i, s += p, d += dp) {
    for (j = 0; j < 8; ++j) {
clang-format's avatar
clang-format committed
189
      int diff = abs(s[j] - d[j]);
190 191 192 193 194 195
      *min = diff < *min ? diff : *min;
      *max = diff > *max ? diff : *max;
    }
  }
}

196
#if CONFIG_VP9_HIGHBITDEPTH
James Zern's avatar
James Zern committed
197
unsigned int vpx_highbd_avg_8x8_c(const uint8_t *s8, int p) {
198 199
  int i, j;
  int sum = 0;
clang-format's avatar
clang-format committed
200 201 202 203
  const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
  for (i = 0; i < 8; ++i, s += p)
    for (j = 0; j < 8; sum += s[j], ++j) {
    }
204 205 206

  return (sum + 32) >> 6;
}
207

James Zern's avatar
James Zern committed
208
unsigned int vpx_highbd_avg_4x4_c(const uint8_t *s8, int p) {
209 210
  int i, j;
  int sum = 0;
clang-format's avatar
clang-format committed
211 212 213 214
  const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
  for (i = 0; i < 4; ++i, s += p)
    for (j = 0; j < 4; sum += s[j], ++j) {
    }
215 216 217

  return (sum + 8) >> 4;
}
218

James Zern's avatar
James Zern committed
219
void vpx_highbd_minmax_8x8_c(const uint8_t *s8, int p, const uint8_t *d8,
220 221
                             int dp, int *min, int *max) {
  int i, j;
clang-format's avatar
clang-format committed
222 223
  const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
  const uint16_t *d = CONVERT_TO_SHORTPTR(d8);
Yaowu Xu's avatar
Yaowu Xu committed
224 225
  *min = 255;
  *max = 0;
226 227
  for (i = 0; i < 8; ++i, s += p, d += dp) {
    for (j = 0; j < 8; ++j) {
clang-format's avatar
clang-format committed
228
      int diff = abs(s[j] - d[j]);
229 230 231 232 233
      *min = diff < *min ? diff : *min;
      *max = diff > *max ? diff : *max;
    }
  }
}
234
#endif  // CONFIG_VP9_HIGHBITDEPTH