avg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <stdlib.h>
  11. #include "./vpx_dsp_rtcd.h"
  12. #include "vpx_ports/mem.h"
  13. unsigned int vpx_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. }
  19. return (sum + 32) >> 6;
  20. }
  21. unsigned int vpx_avg_4x4_c(const uint8_t *s, int p) {
  22. int i, j;
  23. int sum = 0;
  24. for (i = 0; i < 4; ++i, s += p)
  25. for (j = 0; j < 4; sum += s[j], ++j) {
  26. }
  27. return (sum + 8) >> 4;
  28. }
  29. #if CONFIG_VP9_HIGHBITDEPTH
  30. // src_diff: 13 bit, dynamic range [-4095, 4095]
  31. // coeff: 16 bit
  32. static void hadamard_highbd_col8_first_pass(const int16_t *src_diff,
  33. ptrdiff_t src_stride,
  34. int16_t *coeff) {
  35. int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
  36. int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
  37. int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
  38. int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
  39. int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
  40. int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
  41. int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
  42. int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
  43. int16_t c0 = b0 + b2;
  44. int16_t c1 = b1 + b3;
  45. int16_t c2 = b0 - b2;
  46. int16_t c3 = b1 - b3;
  47. int16_t c4 = b4 + b6;
  48. int16_t c5 = b5 + b7;
  49. int16_t c6 = b4 - b6;
  50. int16_t c7 = b5 - b7;
  51. coeff[0] = c0 + c4;
  52. coeff[7] = c1 + c5;
  53. coeff[3] = c2 + c6;
  54. coeff[4] = c3 + c7;
  55. coeff[2] = c0 - c4;
  56. coeff[6] = c1 - c5;
  57. coeff[1] = c2 - c6;
  58. coeff[5] = c3 - c7;
  59. }
  60. // src_diff: 16 bit, dynamic range [-32760, 32760]
  61. // coeff: 19 bit
  62. static void hadamard_highbd_col8_second_pass(const int16_t *src_diff,
  63. ptrdiff_t src_stride,
  64. int32_t *coeff) {
  65. int32_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
  66. int32_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
  67. int32_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
  68. int32_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
  69. int32_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
  70. int32_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
  71. int32_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
  72. int32_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
  73. int32_t c0 = b0 + b2;
  74. int32_t c1 = b1 + b3;
  75. int32_t c2 = b0 - b2;
  76. int32_t c3 = b1 - b3;
  77. int32_t c4 = b4 + b6;
  78. int32_t c5 = b5 + b7;
  79. int32_t c6 = b4 - b6;
  80. int32_t c7 = b5 - b7;
  81. coeff[0] = c0 + c4;
  82. coeff[7] = c1 + c5;
  83. coeff[3] = c2 + c6;
  84. coeff[4] = c3 + c7;
  85. coeff[2] = c0 - c4;
  86. coeff[6] = c1 - c5;
  87. coeff[1] = c2 - c6;
  88. coeff[5] = c3 - c7;
  89. }
  90. // The order of the output coeff of the hadamard is not important. For
  91. // optimization purposes the final transpose may be skipped.
  92. void vpx_highbd_hadamard_8x8_c(const int16_t *src_diff, ptrdiff_t src_stride,
  93. tran_low_t *coeff) {
  94. int idx;
  95. int16_t buffer[64];
  96. int32_t buffer2[64];
  97. int16_t *tmp_buf = &buffer[0];
  98. for (idx = 0; idx < 8; ++idx) {
  99. // src_diff: 13 bit
  100. // buffer: 16 bit, dynamic range [-32760, 32760]
  101. hadamard_highbd_col8_first_pass(src_diff, src_stride, tmp_buf);
  102. tmp_buf += 8;
  103. ++src_diff;
  104. }
  105. tmp_buf = &buffer[0];
  106. for (idx = 0; idx < 8; ++idx) {
  107. // buffer: 16 bit
  108. // buffer2: 19 bit, dynamic range [-262080, 262080]
  109. hadamard_highbd_col8_second_pass(tmp_buf, 8, buffer2 + 8 * idx);
  110. ++tmp_buf;
  111. }
  112. for (idx = 0; idx < 64; ++idx) coeff[idx] = (tran_low_t)buffer2[idx];
  113. }
  114. // In place 16x16 2D Hadamard transform
  115. void vpx_highbd_hadamard_16x16_c(const int16_t *src_diff, ptrdiff_t src_stride,
  116. tran_low_t *coeff) {
  117. int idx;
  118. for (idx = 0; idx < 4; ++idx) {
  119. // src_diff: 13 bit, dynamic range [-4095, 4095]
  120. const int16_t *src_ptr =
  121. src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
  122. vpx_highbd_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
  123. }
  124. // coeff: 19 bit, dynamic range [-262080, 262080]
  125. for (idx = 0; idx < 64; ++idx) {
  126. tran_low_t a0 = coeff[0];
  127. tran_low_t a1 = coeff[64];
  128. tran_low_t a2 = coeff[128];
  129. tran_low_t a3 = coeff[192];
  130. tran_low_t b0 = (a0 + a1) >> 1;
  131. tran_low_t b1 = (a0 - a1) >> 1;
  132. tran_low_t b2 = (a2 + a3) >> 1;
  133. tran_low_t b3 = (a2 - a3) >> 1;
  134. // new coeff dynamic range: 20 bit
  135. coeff[0] = b0 + b2;
  136. coeff[64] = b1 + b3;
  137. coeff[128] = b0 - b2;
  138. coeff[192] = b1 - b3;
  139. ++coeff;
  140. }
  141. }
  142. void vpx_highbd_hadamard_32x32_c(const int16_t *src_diff, ptrdiff_t src_stride,
  143. tran_low_t *coeff) {
  144. int idx;
  145. for (idx = 0; idx < 4; ++idx) {
  146. // src_diff: 13 bit, dynamic range [-4095, 4095]
  147. const int16_t *src_ptr =
  148. src_diff + (idx >> 1) * 16 * src_stride + (idx & 0x01) * 16;
  149. vpx_highbd_hadamard_16x16_c(src_ptr, src_stride, coeff + idx * 256);
  150. }
  151. // coeff: 20 bit
  152. for (idx = 0; idx < 256; ++idx) {
  153. tran_low_t a0 = coeff[0];
  154. tran_low_t a1 = coeff[256];
  155. tran_low_t a2 = coeff[512];
  156. tran_low_t a3 = coeff[768];
  157. tran_low_t b0 = (a0 + a1) >> 2;
  158. tran_low_t b1 = (a0 - a1) >> 2;
  159. tran_low_t b2 = (a2 + a3) >> 2;
  160. tran_low_t b3 = (a2 - a3) >> 2;
  161. // new coeff dynamic range: 20 bit
  162. coeff[0] = b0 + b2;
  163. coeff[256] = b1 + b3;
  164. coeff[512] = b0 - b2;
  165. coeff[768] = b1 - b3;
  166. ++coeff;
  167. }
  168. }
  169. #endif // CONFIG_VP9_HIGHBITDEPTH
  170. // src_diff: first pass, 9 bit, dynamic range [-255, 255]
  171. // second pass, 12 bit, dynamic range [-2040, 2040]
  172. static void hadamard_col8(const int16_t *src_diff, ptrdiff_t src_stride,
  173. int16_t *coeff) {
  174. int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
  175. int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
  176. int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
  177. int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
  178. int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
  179. int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
  180. int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
  181. int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
  182. int16_t c0 = b0 + b2;
  183. int16_t c1 = b1 + b3;
  184. int16_t c2 = b0 - b2;
  185. int16_t c3 = b1 - b3;
  186. int16_t c4 = b4 + b6;
  187. int16_t c5 = b5 + b7;
  188. int16_t c6 = b4 - b6;
  189. int16_t c7 = b5 - b7;
  190. coeff[0] = c0 + c4;
  191. coeff[7] = c1 + c5;
  192. coeff[3] = c2 + c6;
  193. coeff[4] = c3 + c7;
  194. coeff[2] = c0 - c4;
  195. coeff[6] = c1 - c5;
  196. coeff[1] = c2 - c6;
  197. coeff[5] = c3 - c7;
  198. }
  199. // The order of the output coeff of the hadamard is not important. For
  200. // optimization purposes the final transpose may be skipped.
  201. void vpx_hadamard_8x8_c(const int16_t *src_diff, ptrdiff_t src_stride,
  202. tran_low_t *coeff) {
  203. int idx;
  204. int16_t buffer[64];
  205. int16_t buffer2[64];
  206. int16_t *tmp_buf = &buffer[0];
  207. for (idx = 0; idx < 8; ++idx) {
  208. hadamard_col8(src_diff, src_stride, tmp_buf); // src_diff: 9 bit
  209. // dynamic range [-255, 255]
  210. tmp_buf += 8;
  211. ++src_diff;
  212. }
  213. tmp_buf = &buffer[0];
  214. for (idx = 0; idx < 8; ++idx) {
  215. hadamard_col8(tmp_buf, 8, buffer2 + 8 * idx); // tmp_buf: 12 bit
  216. // dynamic range [-2040, 2040]
  217. // buffer2: 15 bit
  218. // dynamic range [-16320, 16320]
  219. ++tmp_buf;
  220. }
  221. for (idx = 0; idx < 64; ++idx) coeff[idx] = (tran_low_t)buffer2[idx];
  222. }
  223. // In place 16x16 2D Hadamard transform
  224. void vpx_hadamard_16x16_c(const int16_t *src_diff, ptrdiff_t src_stride,
  225. tran_low_t *coeff) {
  226. int idx;
  227. for (idx = 0; idx < 4; ++idx) {
  228. // src_diff: 9 bit, dynamic range [-255, 255]
  229. const int16_t *src_ptr =
  230. src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
  231. vpx_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
  232. }
  233. // coeff: 15 bit, dynamic range [-16320, 16320]
  234. for (idx = 0; idx < 64; ++idx) {
  235. tran_low_t a0 = coeff[0];
  236. tran_low_t a1 = coeff[64];
  237. tran_low_t a2 = coeff[128];
  238. tran_low_t a3 = coeff[192];
  239. tran_low_t b0 = (a0 + a1) >> 1; // (a0 + a1): 16 bit, [-32640, 32640]
  240. tran_low_t b1 = (a0 - a1) >> 1; // b0-b3: 15 bit, dynamic range
  241. tran_low_t b2 = (a2 + a3) >> 1; // [-16320, 16320]
  242. tran_low_t b3 = (a2 - a3) >> 1;
  243. coeff[0] = b0 + b2; // 16 bit, [-32640, 32640]
  244. coeff[64] = b1 + b3;
  245. coeff[128] = b0 - b2;
  246. coeff[192] = b1 - b3;
  247. ++coeff;
  248. }
  249. }
  250. void vpx_hadamard_32x32_c(const int16_t *src_diff, ptrdiff_t src_stride,
  251. tran_low_t *coeff) {
  252. int idx;
  253. for (idx = 0; idx < 4; ++idx) {
  254. // src_diff: 9 bit, dynamic range [-255, 255]
  255. const int16_t *src_ptr =
  256. src_diff + (idx >> 1) * 16 * src_stride + (idx & 0x01) * 16;
  257. vpx_hadamard_16x16_c(src_ptr, src_stride, coeff + idx * 256);
  258. }
  259. // coeff: 15 bit, dynamic range [-16320, 16320]
  260. for (idx = 0; idx < 256; ++idx) {
  261. tran_low_t a0 = coeff[0];
  262. tran_low_t a1 = coeff[256];
  263. tran_low_t a2 = coeff[512];
  264. tran_low_t a3 = coeff[768];
  265. tran_low_t b0 = (a0 + a1) >> 2; // (a0 + a1): 16 bit, [-32640, 32640]
  266. tran_low_t b1 = (a0 - a1) >> 2; // b0-b3: 15 bit, dynamic range
  267. tran_low_t b2 = (a2 + a3) >> 2; // [-16320, 16320]
  268. tran_low_t b3 = (a2 - a3) >> 2;
  269. coeff[0] = b0 + b2; // 16 bit, [-32640, 32640]
  270. coeff[256] = b1 + b3;
  271. coeff[512] = b0 - b2;
  272. coeff[768] = b1 - b3;
  273. ++coeff;
  274. }
  275. }
  276. #if CONFIG_VP9_HIGHBITDEPTH
  277. // coeff: dynamic range 20 bit.
  278. // length: value range {16, 64, 256, 1024}.
  279. int vpx_highbd_satd_c(const tran_low_t *coeff, int length) {
  280. int i;
  281. int satd = 0;
  282. for (i = 0; i < length; ++i) satd += abs(coeff[i]);
  283. // satd: 30 bits
  284. return satd;
  285. }
  286. #endif // CONFIG_VP9_HIGHBITDEPTH
  287. // coeff: 16 bits, dynamic range [-32640, 32640].
  288. // length: value range {16, 64, 256, 1024}.
  289. int vpx_satd_c(const tran_low_t *coeff, int length) {
  290. int i;
  291. int satd = 0;
  292. for (i = 0; i < length; ++i) satd += abs(coeff[i]);
  293. // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
  294. return satd;
  295. }
  296. // Integer projection onto row vectors.
  297. // height: value range {16, 32, 64}.
  298. void vpx_int_pro_row_c(int16_t hbuf[16], const uint8_t *ref,
  299. const int ref_stride, const int height) {
  300. int idx;
  301. const int norm_factor = height >> 1;
  302. if (height == 1) return;
  303. for (idx = 0; idx < 16; ++idx) {
  304. int i;
  305. hbuf[idx] = 0;
  306. // hbuf[idx]: 14 bit, dynamic range [0, 16320].
  307. for (i = 0; i < height; ++i) hbuf[idx] += ref[i * ref_stride];
  308. // hbuf[idx]: 9 bit, dynamic range [0, 510].
  309. hbuf[idx] /= norm_factor;
  310. ++ref;
  311. }
  312. }
  313. // width: value range {16, 32, 64}.
  314. int16_t vpx_int_pro_col_c(const uint8_t *ref, const int width) {
  315. int idx;
  316. int16_t sum = 0;
  317. // sum: 14 bit, dynamic range [0, 16320]
  318. for (idx = 0; idx < width; ++idx) sum += ref[idx];
  319. return sum;
  320. }
  321. // ref: [0 - 510]
  322. // src: [0 - 510]
  323. // bwl: {2, 3, 4}
  324. int vpx_vector_var_c(const int16_t *ref, const int16_t *src, const int bwl) {
  325. int i;
  326. int width = 4 << bwl;
  327. int sse = 0, mean = 0, var;
  328. for (i = 0; i < width; ++i) {
  329. int diff = ref[i] - src[i]; // diff: dynamic range [-510, 510], 10 bits.
  330. mean += diff; // mean: dynamic range 16 bits.
  331. sse += diff * diff; // sse: dynamic range 26 bits.
  332. }
  333. // (mean * mean): dynamic range 31 bits.
  334. var = sse - ((mean * mean) >> (bwl + 2));
  335. return var;
  336. }
  337. void vpx_minmax_8x8_c(const uint8_t *s, int p, const uint8_t *d, int dp,
  338. int *min, int *max) {
  339. int i, j;
  340. *min = 255;
  341. *max = 0;
  342. for (i = 0; i < 8; ++i, s += p, d += dp) {
  343. for (j = 0; j < 8; ++j) {
  344. int diff = abs(s[j] - d[j]);
  345. *min = diff < *min ? diff : *min;
  346. *max = diff > *max ? diff : *max;
  347. }
  348. }
  349. }
  350. #if CONFIG_VP9_HIGHBITDEPTH
  351. unsigned int vpx_highbd_avg_8x8_c(const uint8_t *s8, int p) {
  352. int i, j;
  353. int sum = 0;
  354. const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
  355. for (i = 0; i < 8; ++i, s += p)
  356. for (j = 0; j < 8; sum += s[j], ++j) {
  357. }
  358. return (sum + 32) >> 6;
  359. }
  360. unsigned int vpx_highbd_avg_4x4_c(const uint8_t *s8, int p) {
  361. int i, j;
  362. int sum = 0;
  363. const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
  364. for (i = 0; i < 4; ++i, s += p)
  365. for (j = 0; j < 4; sum += s[j], ++j) {
  366. }
  367. return (sum + 8) >> 4;
  368. }
  369. void vpx_highbd_minmax_8x8_c(const uint8_t *s8, int p, const uint8_t *d8,
  370. int dp, int *min, int *max) {
  371. int i, j;
  372. const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
  373. const uint16_t *d = CONVERT_TO_SHORTPTR(d8);
  374. *min = 255;
  375. *max = 0;
  376. for (i = 0; i < 8; ++i, s += p, d += dp) {
  377. for (j = 0; j < 8; ++j) {
  378. int diff = abs(s[j] - d[j]);
  379. *min = diff < *min ? diff : *min;
  380. *max = diff > *max ? diff : *max;
  381. }
  382. }
  383. }
  384. #endif // CONFIG_VP9_HIGHBITDEPTH