denoising_sse2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2012 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 "vp8/encoder/denoising.h"
  11. #include "vp8/common/reconinter.h"
  12. #include "vpx/vpx_integer.h"
  13. #include "vpx_mem/vpx_mem.h"
  14. #include "vp8_rtcd.h"
  15. #include <emmintrin.h>
  16. #include "vpx_ports/emmintrin_compat.h"
  17. /* Compute the sum of all pixel differences of this MB. */
  18. static INLINE unsigned int abs_sum_diff_16x1(__m128i acc_diff) {
  19. const __m128i k_1 = _mm_set1_epi16(1);
  20. const __m128i acc_diff_lo =
  21. _mm_srai_epi16(_mm_unpacklo_epi8(acc_diff, acc_diff), 8);
  22. const __m128i acc_diff_hi =
  23. _mm_srai_epi16(_mm_unpackhi_epi8(acc_diff, acc_diff), 8);
  24. const __m128i acc_diff_16 = _mm_add_epi16(acc_diff_lo, acc_diff_hi);
  25. const __m128i hg_fe_dc_ba = _mm_madd_epi16(acc_diff_16, k_1);
  26. const __m128i hgfe_dcba =
  27. _mm_add_epi32(hg_fe_dc_ba, _mm_srli_si128(hg_fe_dc_ba, 8));
  28. const __m128i hgfedcba =
  29. _mm_add_epi32(hgfe_dcba, _mm_srli_si128(hgfe_dcba, 4));
  30. unsigned int sum_diff = abs(_mm_cvtsi128_si32(hgfedcba));
  31. return sum_diff;
  32. }
  33. int vp8_denoiser_filter_sse2(unsigned char *mc_running_avg_y,
  34. int mc_avg_y_stride, unsigned char *running_avg_y,
  35. int avg_y_stride, unsigned char *sig,
  36. int sig_stride, unsigned int motion_magnitude,
  37. int increase_denoising) {
  38. unsigned char *running_avg_y_start = running_avg_y;
  39. unsigned char *sig_start = sig;
  40. unsigned int sum_diff_thresh;
  41. int r;
  42. int shift_inc =
  43. (increase_denoising && motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD)
  44. ? 1
  45. : 0;
  46. __m128i acc_diff = _mm_setzero_si128();
  47. const __m128i k_0 = _mm_setzero_si128();
  48. const __m128i k_4 = _mm_set1_epi8(4 + shift_inc);
  49. const __m128i k_8 = _mm_set1_epi8(8);
  50. const __m128i k_16 = _mm_set1_epi8(16);
  51. /* Modify each level's adjustment according to motion_magnitude. */
  52. const __m128i l3 = _mm_set1_epi8(
  53. (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) ? 7 + shift_inc : 6);
  54. /* Difference between level 3 and level 2 is 2. */
  55. const __m128i l32 = _mm_set1_epi8(2);
  56. /* Difference between level 2 and level 1 is 1. */
  57. const __m128i l21 = _mm_set1_epi8(1);
  58. for (r = 0; r < 16; ++r) {
  59. /* Calculate differences */
  60. const __m128i v_sig = _mm_loadu_si128((__m128i *)(&sig[0]));
  61. const __m128i v_mc_running_avg_y =
  62. _mm_loadu_si128((__m128i *)(&mc_running_avg_y[0]));
  63. __m128i v_running_avg_y;
  64. const __m128i pdiff = _mm_subs_epu8(v_mc_running_avg_y, v_sig);
  65. const __m128i ndiff = _mm_subs_epu8(v_sig, v_mc_running_avg_y);
  66. /* Obtain the sign. FF if diff is negative. */
  67. const __m128i diff_sign = _mm_cmpeq_epi8(pdiff, k_0);
  68. /* Clamp absolute difference to 16 to be used to get mask. Doing this
  69. * allows us to use _mm_cmpgt_epi8, which operates on signed byte. */
  70. const __m128i clamped_absdiff =
  71. _mm_min_epu8(_mm_or_si128(pdiff, ndiff), k_16);
  72. /* Get masks for l2 l1 and l0 adjustments */
  73. const __m128i mask2 = _mm_cmpgt_epi8(k_16, clamped_absdiff);
  74. const __m128i mask1 = _mm_cmpgt_epi8(k_8, clamped_absdiff);
  75. const __m128i mask0 = _mm_cmpgt_epi8(k_4, clamped_absdiff);
  76. /* Get adjustments for l2, l1, and l0 */
  77. __m128i adj2 = _mm_and_si128(mask2, l32);
  78. const __m128i adj1 = _mm_and_si128(mask1, l21);
  79. const __m128i adj0 = _mm_and_si128(mask0, clamped_absdiff);
  80. __m128i adj, padj, nadj;
  81. /* Combine the adjustments and get absolute adjustments. */
  82. adj2 = _mm_add_epi8(adj2, adj1);
  83. adj = _mm_sub_epi8(l3, adj2);
  84. adj = _mm_andnot_si128(mask0, adj);
  85. adj = _mm_or_si128(adj, adj0);
  86. /* Restore the sign and get positive and negative adjustments. */
  87. padj = _mm_andnot_si128(diff_sign, adj);
  88. nadj = _mm_and_si128(diff_sign, adj);
  89. /* Calculate filtered value. */
  90. v_running_avg_y = _mm_adds_epu8(v_sig, padj);
  91. v_running_avg_y = _mm_subs_epu8(v_running_avg_y, nadj);
  92. _mm_storeu_si128((__m128i *)running_avg_y, v_running_avg_y);
  93. /* Adjustments <=7, and each element in acc_diff can fit in signed
  94. * char.
  95. */
  96. acc_diff = _mm_adds_epi8(acc_diff, padj);
  97. acc_diff = _mm_subs_epi8(acc_diff, nadj);
  98. /* Update pointers for next iteration. */
  99. sig += sig_stride;
  100. mc_running_avg_y += mc_avg_y_stride;
  101. running_avg_y += avg_y_stride;
  102. }
  103. {
  104. /* Compute the sum of all pixel differences of this MB. */
  105. unsigned int abs_sum_diff = abs_sum_diff_16x1(acc_diff);
  106. sum_diff_thresh = SUM_DIFF_THRESHOLD;
  107. if (increase_denoising) sum_diff_thresh = SUM_DIFF_THRESHOLD_HIGH;
  108. if (abs_sum_diff > sum_diff_thresh) {
  109. // Before returning to copy the block (i.e., apply no denoising),
  110. // check if we can still apply some (weaker) temporal filtering to
  111. // this block, that would otherwise not be denoised at all. Simplest
  112. // is to apply an additional adjustment to running_avg_y to bring it
  113. // closer to sig. The adjustment is capped by a maximum delta, and
  114. // chosen such that in most cases the resulting sum_diff will be
  115. // within the acceptable range given by sum_diff_thresh.
  116. // The delta is set by the excess of absolute pixel diff over the
  117. // threshold.
  118. int delta = ((abs_sum_diff - sum_diff_thresh) >> 8) + 1;
  119. // Only apply the adjustment for max delta up to 3.
  120. if (delta < 4) {
  121. const __m128i k_delta = _mm_set1_epi8(delta);
  122. sig -= sig_stride * 16;
  123. mc_running_avg_y -= mc_avg_y_stride * 16;
  124. running_avg_y -= avg_y_stride * 16;
  125. for (r = 0; r < 16; ++r) {
  126. __m128i v_running_avg_y =
  127. _mm_loadu_si128((__m128i *)(&running_avg_y[0]));
  128. // Calculate differences.
  129. const __m128i v_sig = _mm_loadu_si128((__m128i *)(&sig[0]));
  130. const __m128i v_mc_running_avg_y =
  131. _mm_loadu_si128((__m128i *)(&mc_running_avg_y[0]));
  132. const __m128i pdiff = _mm_subs_epu8(v_mc_running_avg_y, v_sig);
  133. const __m128i ndiff = _mm_subs_epu8(v_sig, v_mc_running_avg_y);
  134. // Obtain the sign. FF if diff is negative.
  135. const __m128i diff_sign = _mm_cmpeq_epi8(pdiff, k_0);
  136. // Clamp absolute difference to delta to get the adjustment.
  137. const __m128i adj = _mm_min_epu8(_mm_or_si128(pdiff, ndiff), k_delta);
  138. // Restore the sign and get positive and negative adjustments.
  139. __m128i padj, nadj;
  140. padj = _mm_andnot_si128(diff_sign, adj);
  141. nadj = _mm_and_si128(diff_sign, adj);
  142. // Calculate filtered value.
  143. v_running_avg_y = _mm_subs_epu8(v_running_avg_y, padj);
  144. v_running_avg_y = _mm_adds_epu8(v_running_avg_y, nadj);
  145. _mm_storeu_si128((__m128i *)running_avg_y, v_running_avg_y);
  146. // Accumulate the adjustments.
  147. acc_diff = _mm_subs_epi8(acc_diff, padj);
  148. acc_diff = _mm_adds_epi8(acc_diff, nadj);
  149. // Update pointers for next iteration.
  150. sig += sig_stride;
  151. mc_running_avg_y += mc_avg_y_stride;
  152. running_avg_y += avg_y_stride;
  153. }
  154. abs_sum_diff = abs_sum_diff_16x1(acc_diff);
  155. if (abs_sum_diff > sum_diff_thresh) {
  156. return COPY_BLOCK;
  157. }
  158. } else {
  159. return COPY_BLOCK;
  160. }
  161. }
  162. }
  163. vp8_copy_mem16x16(running_avg_y_start, avg_y_stride, sig_start, sig_stride);
  164. return FILTER_BLOCK;
  165. }
  166. int vp8_denoiser_filter_uv_sse2(unsigned char *mc_running_avg,
  167. int mc_avg_stride, unsigned char *running_avg,
  168. int avg_stride, unsigned char *sig,
  169. int sig_stride, unsigned int motion_magnitude,
  170. int increase_denoising) {
  171. unsigned char *running_avg_start = running_avg;
  172. unsigned char *sig_start = sig;
  173. unsigned int sum_diff_thresh;
  174. int r;
  175. int shift_inc =
  176. (increase_denoising && motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD_UV)
  177. ? 1
  178. : 0;
  179. __m128i acc_diff = _mm_setzero_si128();
  180. const __m128i k_0 = _mm_setzero_si128();
  181. const __m128i k_4 = _mm_set1_epi8(4 + shift_inc);
  182. const __m128i k_8 = _mm_set1_epi8(8);
  183. const __m128i k_16 = _mm_set1_epi8(16);
  184. /* Modify each level's adjustment according to motion_magnitude. */
  185. const __m128i l3 = _mm_set1_epi8(
  186. (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD_UV) ? 7 + shift_inc : 6);
  187. /* Difference between level 3 and level 2 is 2. */
  188. const __m128i l32 = _mm_set1_epi8(2);
  189. /* Difference between level 2 and level 1 is 1. */
  190. const __m128i l21 = _mm_set1_epi8(1);
  191. {
  192. const __m128i k_1 = _mm_set1_epi16(1);
  193. __m128i vec_sum_block = _mm_setzero_si128();
  194. // Avoid denoising color signal if its close to average level.
  195. for (r = 0; r < 8; ++r) {
  196. const __m128i v_sig = _mm_loadl_epi64((__m128i *)(&sig[0]));
  197. const __m128i v_sig_unpack = _mm_unpacklo_epi8(v_sig, k_0);
  198. vec_sum_block = _mm_add_epi16(vec_sum_block, v_sig_unpack);
  199. sig += sig_stride;
  200. }
  201. sig -= sig_stride * 8;
  202. {
  203. const __m128i hg_fe_dc_ba = _mm_madd_epi16(vec_sum_block, k_1);
  204. const __m128i hgfe_dcba =
  205. _mm_add_epi32(hg_fe_dc_ba, _mm_srli_si128(hg_fe_dc_ba, 8));
  206. const __m128i hgfedcba =
  207. _mm_add_epi32(hgfe_dcba, _mm_srli_si128(hgfe_dcba, 4));
  208. const int sum_block = _mm_cvtsi128_si32(hgfedcba);
  209. if (abs(sum_block - (128 * 8 * 8)) < SUM_DIFF_FROM_AVG_THRESH_UV) {
  210. return COPY_BLOCK;
  211. }
  212. }
  213. }
  214. for (r = 0; r < 4; ++r) {
  215. /* Calculate differences */
  216. const __m128i v_sig_low =
  217. _mm_castpd_si128(_mm_load_sd((double *)(&sig[0])));
  218. const __m128i v_sig = _mm_castpd_si128(_mm_loadh_pd(
  219. _mm_castsi128_pd(v_sig_low), (double *)(&sig[sig_stride])));
  220. const __m128i v_mc_running_avg_low =
  221. _mm_castpd_si128(_mm_load_sd((double *)(&mc_running_avg[0])));
  222. const __m128i v_mc_running_avg = _mm_castpd_si128(
  223. _mm_loadh_pd(_mm_castsi128_pd(v_mc_running_avg_low),
  224. (double *)(&mc_running_avg[mc_avg_stride])));
  225. const __m128i pdiff = _mm_subs_epu8(v_mc_running_avg, v_sig);
  226. const __m128i ndiff = _mm_subs_epu8(v_sig, v_mc_running_avg);
  227. /* Obtain the sign. FF if diff is negative. */
  228. const __m128i diff_sign = _mm_cmpeq_epi8(pdiff, k_0);
  229. /* Clamp absolute difference to 16 to be used to get mask. Doing this
  230. * allows us to use _mm_cmpgt_epi8, which operates on signed byte. */
  231. const __m128i clamped_absdiff =
  232. _mm_min_epu8(_mm_or_si128(pdiff, ndiff), k_16);
  233. /* Get masks for l2 l1 and l0 adjustments */
  234. const __m128i mask2 = _mm_cmpgt_epi8(k_16, clamped_absdiff);
  235. const __m128i mask1 = _mm_cmpgt_epi8(k_8, clamped_absdiff);
  236. const __m128i mask0 = _mm_cmpgt_epi8(k_4, clamped_absdiff);
  237. /* Get adjustments for l2, l1, and l0 */
  238. __m128i adj2 = _mm_and_si128(mask2, l32);
  239. const __m128i adj1 = _mm_and_si128(mask1, l21);
  240. const __m128i adj0 = _mm_and_si128(mask0, clamped_absdiff);
  241. __m128i adj, padj, nadj;
  242. __m128i v_running_avg;
  243. /* Combine the adjustments and get absolute adjustments. */
  244. adj2 = _mm_add_epi8(adj2, adj1);
  245. adj = _mm_sub_epi8(l3, adj2);
  246. adj = _mm_andnot_si128(mask0, adj);
  247. adj = _mm_or_si128(adj, adj0);
  248. /* Restore the sign and get positive and negative adjustments. */
  249. padj = _mm_andnot_si128(diff_sign, adj);
  250. nadj = _mm_and_si128(diff_sign, adj);
  251. /* Calculate filtered value. */
  252. v_running_avg = _mm_adds_epu8(v_sig, padj);
  253. v_running_avg = _mm_subs_epu8(v_running_avg, nadj);
  254. _mm_storel_pd((double *)&running_avg[0], _mm_castsi128_pd(v_running_avg));
  255. _mm_storeh_pd((double *)&running_avg[avg_stride],
  256. _mm_castsi128_pd(v_running_avg));
  257. /* Adjustments <=7, and each element in acc_diff can fit in signed
  258. * char.
  259. */
  260. acc_diff = _mm_adds_epi8(acc_diff, padj);
  261. acc_diff = _mm_subs_epi8(acc_diff, nadj);
  262. /* Update pointers for next iteration. */
  263. sig += sig_stride * 2;
  264. mc_running_avg += mc_avg_stride * 2;
  265. running_avg += avg_stride * 2;
  266. }
  267. {
  268. unsigned int abs_sum_diff = abs_sum_diff_16x1(acc_diff);
  269. sum_diff_thresh = SUM_DIFF_THRESHOLD_UV;
  270. if (increase_denoising) sum_diff_thresh = SUM_DIFF_THRESHOLD_HIGH_UV;
  271. if (abs_sum_diff > sum_diff_thresh) {
  272. // Before returning to copy the block (i.e., apply no denoising),
  273. // check if we can still apply some (weaker) temporal filtering to
  274. // this block, that would otherwise not be denoised at all. Simplest
  275. // is to apply an additional adjustment to running_avg_y to bring it
  276. // closer to sig. The adjustment is capped by a maximum delta, and
  277. // chosen such that in most cases the resulting sum_diff will be
  278. // within the acceptable range given by sum_diff_thresh.
  279. // The delta is set by the excess of absolute pixel diff over the
  280. // threshold.
  281. int delta = ((abs_sum_diff - sum_diff_thresh) >> 8) + 1;
  282. // Only apply the adjustment for max delta up to 3.
  283. if (delta < 4) {
  284. const __m128i k_delta = _mm_set1_epi8(delta);
  285. sig -= sig_stride * 8;
  286. mc_running_avg -= mc_avg_stride * 8;
  287. running_avg -= avg_stride * 8;
  288. for (r = 0; r < 4; ++r) {
  289. // Calculate differences.
  290. const __m128i v_sig_low =
  291. _mm_castpd_si128(_mm_load_sd((double *)(&sig[0])));
  292. const __m128i v_sig = _mm_castpd_si128(_mm_loadh_pd(
  293. _mm_castsi128_pd(v_sig_low), (double *)(&sig[sig_stride])));
  294. const __m128i v_mc_running_avg_low =
  295. _mm_castpd_si128(_mm_load_sd((double *)(&mc_running_avg[0])));
  296. const __m128i v_mc_running_avg = _mm_castpd_si128(
  297. _mm_loadh_pd(_mm_castsi128_pd(v_mc_running_avg_low),
  298. (double *)(&mc_running_avg[mc_avg_stride])));
  299. const __m128i pdiff = _mm_subs_epu8(v_mc_running_avg, v_sig);
  300. const __m128i ndiff = _mm_subs_epu8(v_sig, v_mc_running_avg);
  301. // Obtain the sign. FF if diff is negative.
  302. const __m128i diff_sign = _mm_cmpeq_epi8(pdiff, k_0);
  303. // Clamp absolute difference to delta to get the adjustment.
  304. const __m128i adj = _mm_min_epu8(_mm_or_si128(pdiff, ndiff), k_delta);
  305. // Restore the sign and get positive and negative adjustments.
  306. __m128i padj, nadj;
  307. const __m128i v_running_avg_low =
  308. _mm_castpd_si128(_mm_load_sd((double *)(&running_avg[0])));
  309. __m128i v_running_avg = _mm_castpd_si128(
  310. _mm_loadh_pd(_mm_castsi128_pd(v_running_avg_low),
  311. (double *)(&running_avg[avg_stride])));
  312. padj = _mm_andnot_si128(diff_sign, adj);
  313. nadj = _mm_and_si128(diff_sign, adj);
  314. // Calculate filtered value.
  315. v_running_avg = _mm_subs_epu8(v_running_avg, padj);
  316. v_running_avg = _mm_adds_epu8(v_running_avg, nadj);
  317. _mm_storel_pd((double *)&running_avg[0],
  318. _mm_castsi128_pd(v_running_avg));
  319. _mm_storeh_pd((double *)&running_avg[avg_stride],
  320. _mm_castsi128_pd(v_running_avg));
  321. // Accumulate the adjustments.
  322. acc_diff = _mm_subs_epi8(acc_diff, padj);
  323. acc_diff = _mm_adds_epi8(acc_diff, nadj);
  324. // Update pointers for next iteration.
  325. sig += sig_stride * 2;
  326. mc_running_avg += mc_avg_stride * 2;
  327. running_avg += avg_stride * 2;
  328. }
  329. abs_sum_diff = abs_sum_diff_16x1(acc_diff);
  330. if (abs_sum_diff > sum_diff_thresh) {
  331. return COPY_BLOCK;
  332. }
  333. } else {
  334. return COPY_BLOCK;
  335. }
  336. }
  337. }
  338. vp8_copy_mem8x8(running_avg_start, avg_stride, sig_start, sig_stride);
  339. return FILTER_BLOCK;
  340. }