vp9_avg.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "./vp9_rtcd.h"
  11. #include "vp9/common/vp9_common.h"
  12. #include "vpx_ports/mem.h"
  13. unsigned int vp9_avg_8x8_c(const uint8_t *s, int p) {
  14. int i, j;
  15. int sum = 0;
  16. for (i = 0; i < 8; ++i, s+=p)
  17. for (j = 0; j < 8; sum += s[j], ++j) {}
  18. return (sum + 32) >> 6;
  19. }
  20. unsigned int vp9_avg_4x4_c(const uint8_t *s, int p) {
  21. int i, j;
  22. int sum = 0;
  23. for (i = 0; i < 4; ++i, s+=p)
  24. for (j = 0; j < 4; sum += s[j], ++j) {}
  25. return (sum + 8) >> 4;
  26. }
  27. static void hadamard_col8(const int16_t *src_diff, int src_stride,
  28. int16_t *coeff) {
  29. int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
  30. int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
  31. int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
  32. int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
  33. int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
  34. int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
  35. int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
  36. int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
  37. int16_t c0 = b0 + b2;
  38. int16_t c1 = b1 + b3;
  39. int16_t c2 = b0 - b2;
  40. int16_t c3 = b1 - b3;
  41. int16_t c4 = b4 + b6;
  42. int16_t c5 = b5 + b7;
  43. int16_t c6 = b4 - b6;
  44. int16_t c7 = b5 - b7;
  45. coeff[0] = c0 + c4;
  46. coeff[7] = c1 + c5;
  47. coeff[3] = c2 + c6;
  48. coeff[4] = c3 + c7;
  49. coeff[2] = c0 - c4;
  50. coeff[6] = c1 - c5;
  51. coeff[1] = c2 - c6;
  52. coeff[5] = c3 - c7;
  53. }
  54. void vp9_hadamard_8x8_c(int16_t const *src_diff, int src_stride,
  55. int16_t *coeff) {
  56. int idx;
  57. int16_t buffer[64];
  58. int16_t *tmp_buf = &buffer[0];
  59. for (idx = 0; idx < 8; ++idx) {
  60. hadamard_col8(src_diff, src_stride, tmp_buf);
  61. tmp_buf += 8;
  62. ++src_diff;
  63. }
  64. tmp_buf = &buffer[0];
  65. for (idx = 0; idx < 8; ++idx) {
  66. hadamard_col8(tmp_buf, 8, coeff);
  67. coeff += 8;
  68. ++tmp_buf;
  69. }
  70. }
  71. // In place 16x16 2D Hadamard transform
  72. void vp9_hadamard_16x16_c(int16_t const *src_diff, int src_stride,
  73. int16_t *coeff) {
  74. int idx;
  75. for (idx = 0; idx < 4; ++idx) {
  76. int16_t const *src_ptr = src_diff + (idx >> 1) * 8 * src_stride
  77. + (idx & 0x01) * 8;
  78. vp9_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
  79. }
  80. for (idx = 0; idx < 64; ++idx) {
  81. int16_t a0 = coeff[0];
  82. int16_t a1 = coeff[64];
  83. int16_t a2 = coeff[128];
  84. int16_t a3 = coeff[192];
  85. int16_t b0 = a0 + a1;
  86. int16_t b1 = a0 - a1;
  87. int16_t b2 = a2 + a3;
  88. int16_t b3 = a2 - a3;
  89. coeff[0] = (b0 + b2) >> 1;
  90. coeff[64] = (b1 + b3) >> 1;
  91. coeff[128] = (b0 - b2) >> 1;
  92. coeff[192] = (b1 - b3) >> 1;
  93. ++coeff;
  94. }
  95. }
  96. int16_t vp9_satd_c(const int16_t *coeff, int length) {
  97. int i;
  98. int satd = 0;
  99. for (i = 0; i < length; ++i)
  100. satd += abs(coeff[i]);
  101. return (int16_t)satd;
  102. }
  103. // Integer projection onto row vectors.
  104. void vp9_int_pro_row_c(int16_t *hbuf, uint8_t const *ref,
  105. const int ref_stride, const int height) {
  106. int idx;
  107. const int norm_factor = MAX(8, height >> 1);
  108. for (idx = 0; idx < 16; ++idx) {
  109. int i;
  110. hbuf[idx] = 0;
  111. for (i = 0; i < height; ++i)
  112. hbuf[idx] += ref[i * ref_stride];
  113. hbuf[idx] /= norm_factor;
  114. ++ref;
  115. }
  116. }
  117. int16_t vp9_int_pro_col_c(uint8_t const *ref, const int width) {
  118. int idx;
  119. int16_t sum = 0;
  120. for (idx = 0; idx < width; ++idx)
  121. sum += ref[idx];
  122. return sum;
  123. }
  124. int vp9_vector_var_c(int16_t const *ref, int16_t const *src,
  125. const int bwl) {
  126. int i;
  127. int width = 4 << bwl;
  128. int sse = 0, mean = 0, var;
  129. for (i = 0; i < width; ++i) {
  130. int diff = ref[i] - src[i];
  131. mean += diff;
  132. sse += diff * diff;
  133. }
  134. var = sse - ((mean * mean) >> (bwl + 2));
  135. return var;
  136. }
  137. void vp9_minmax_8x8_c(const uint8_t *s, int p, const uint8_t *d, int dp,
  138. int *min, int *max) {
  139. int i, j;
  140. *min = 255;
  141. *max = 0;
  142. for (i = 0; i < 8; ++i, s += p, d += dp) {
  143. for (j = 0; j < 8; ++j) {
  144. int diff = abs(s[j]-d[j]);
  145. *min = diff < *min ? diff : *min;
  146. *max = diff > *max ? diff : *max;
  147. }
  148. }
  149. }
  150. #if CONFIG_VP9_HIGHBITDEPTH
  151. unsigned int vp9_highbd_avg_8x8_c(const uint8_t *s8, int p) {
  152. int i, j;
  153. int sum = 0;
  154. const uint16_t* s = CONVERT_TO_SHORTPTR(s8);
  155. for (i = 0; i < 8; ++i, s+=p)
  156. for (j = 0; j < 8; sum += s[j], ++j) {}
  157. return (sum + 32) >> 6;
  158. }
  159. unsigned int vp9_highbd_avg_4x4_c(const uint8_t *s8, int p) {
  160. int i, j;
  161. int sum = 0;
  162. const uint16_t* s = CONVERT_TO_SHORTPTR(s8);
  163. for (i = 0; i < 4; ++i, s+=p)
  164. for (j = 0; j < 4; sum += s[j], ++j) {}
  165. return (sum + 8) >> 4;
  166. }
  167. void vp9_highbd_minmax_8x8_c(const uint8_t *s8, int p, const uint8_t *d8,
  168. int dp, int *min, int *max) {
  169. int i, j;
  170. const uint16_t* s = CONVERT_TO_SHORTPTR(s8);
  171. const uint16_t* d = CONVERT_TO_SHORTPTR(d8);
  172. *min = 255;
  173. *max = 0;
  174. for (i = 0; i < 8; ++i, s += p, d += dp) {
  175. for (j = 0; j < 8; ++j) {
  176. int diff = abs(s[j]-d[j]);
  177. *min = diff < *min ? diff : *min;
  178. *max = diff > *max ? diff : *max;
  179. }
  180. }
  181. }
  182. #endif // CONFIG_VP9_HIGHBITDEPTH