media/libvpx/vp8/encoder/x86/denoising_sse2.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11 #include "vp8/encoder/denoising.h"
michael@0 12 #include "vp8/common/reconinter.h"
michael@0 13 #include "vpx/vpx_integer.h"
michael@0 14 #include "vpx_mem/vpx_mem.h"
michael@0 15 #include "vp8_rtcd.h"
michael@0 16
michael@0 17 #include <emmintrin.h>
michael@0 18 #include "vpx_ports/emmintrin_compat.h"
michael@0 19
michael@0 20 union sum_union {
michael@0 21 __m128i v;
michael@0 22 signed char e[16];
michael@0 23 };
michael@0 24
michael@0 25 int vp8_denoiser_filter_sse2(YV12_BUFFER_CONFIG *mc_running_avg,
michael@0 26 YV12_BUFFER_CONFIG *running_avg,
michael@0 27 MACROBLOCK *signal, unsigned int motion_magnitude,
michael@0 28 int y_offset, int uv_offset)
michael@0 29 {
michael@0 30 unsigned char *sig = signal->thismb;
michael@0 31 int sig_stride = 16;
michael@0 32 unsigned char *mc_running_avg_y = mc_running_avg->y_buffer + y_offset;
michael@0 33 int mc_avg_y_stride = mc_running_avg->y_stride;
michael@0 34 unsigned char *running_avg_y = running_avg->y_buffer + y_offset;
michael@0 35 int avg_y_stride = running_avg->y_stride;
michael@0 36 int r;
michael@0 37 __m128i acc_diff = _mm_setzero_si128();
michael@0 38 const __m128i k_0 = _mm_setzero_si128();
michael@0 39 const __m128i k_4 = _mm_set1_epi8(4);
michael@0 40 const __m128i k_8 = _mm_set1_epi8(8);
michael@0 41 const __m128i k_16 = _mm_set1_epi8(16);
michael@0 42 /* Modify each level's adjustment according to motion_magnitude. */
michael@0 43 const __m128i l3 = _mm_set1_epi8(
michael@0 44 (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) ? 7 : 6);
michael@0 45 /* Difference between level 3 and level 2 is 2. */
michael@0 46 const __m128i l32 = _mm_set1_epi8(2);
michael@0 47 /* Difference between level 2 and level 1 is 1. */
michael@0 48 const __m128i l21 = _mm_set1_epi8(1);
michael@0 49
michael@0 50 for (r = 0; r < 16; ++r)
michael@0 51 {
michael@0 52 /* Calculate differences */
michael@0 53 const __m128i v_sig = _mm_loadu_si128((__m128i *)(&sig[0]));
michael@0 54 const __m128i v_mc_running_avg_y = _mm_loadu_si128(
michael@0 55 (__m128i *)(&mc_running_avg_y[0]));
michael@0 56 __m128i v_running_avg_y;
michael@0 57 const __m128i pdiff = _mm_subs_epu8(v_mc_running_avg_y, v_sig);
michael@0 58 const __m128i ndiff = _mm_subs_epu8(v_sig, v_mc_running_avg_y);
michael@0 59 /* Obtain the sign. FF if diff is negative. */
michael@0 60 const __m128i diff_sign = _mm_cmpeq_epi8(pdiff, k_0);
michael@0 61 /* Clamp absolute difference to 16 to be used to get mask. Doing this
michael@0 62 * allows us to use _mm_cmpgt_epi8, which operates on signed byte. */
michael@0 63 const __m128i clamped_absdiff = _mm_min_epu8(
michael@0 64 _mm_or_si128(pdiff, ndiff), k_16);
michael@0 65 /* Get masks for l2 l1 and l0 adjustments */
michael@0 66 const __m128i mask2 = _mm_cmpgt_epi8(k_16, clamped_absdiff);
michael@0 67 const __m128i mask1 = _mm_cmpgt_epi8(k_8, clamped_absdiff);
michael@0 68 const __m128i mask0 = _mm_cmpgt_epi8(k_4, clamped_absdiff);
michael@0 69 /* Get adjustments for l2, l1, and l0 */
michael@0 70 __m128i adj2 = _mm_and_si128(mask2, l32);
michael@0 71 const __m128i adj1 = _mm_and_si128(mask1, l21);
michael@0 72 const __m128i adj0 = _mm_and_si128(mask0, clamped_absdiff);
michael@0 73 __m128i adj, padj, nadj;
michael@0 74
michael@0 75 /* Combine the adjustments and get absolute adjustments. */
michael@0 76 adj2 = _mm_add_epi8(adj2, adj1);
michael@0 77 adj = _mm_sub_epi8(l3, adj2);
michael@0 78 adj = _mm_andnot_si128(mask0, adj);
michael@0 79 adj = _mm_or_si128(adj, adj0);
michael@0 80
michael@0 81 /* Restore the sign and get positive and negative adjustments. */
michael@0 82 padj = _mm_andnot_si128(diff_sign, adj);
michael@0 83 nadj = _mm_and_si128(diff_sign, adj);
michael@0 84
michael@0 85 /* Calculate filtered value. */
michael@0 86 v_running_avg_y = _mm_adds_epu8(v_sig, padj);
michael@0 87 v_running_avg_y = _mm_subs_epu8(v_running_avg_y, nadj);
michael@0 88 _mm_storeu_si128((__m128i *)running_avg_y, v_running_avg_y);
michael@0 89
michael@0 90 /* Adjustments <=7, and each element in acc_diff can fit in signed
michael@0 91 * char.
michael@0 92 */
michael@0 93 acc_diff = _mm_adds_epi8(acc_diff, padj);
michael@0 94 acc_diff = _mm_subs_epi8(acc_diff, nadj);
michael@0 95
michael@0 96 /* Update pointers for next iteration. */
michael@0 97 sig += sig_stride;
michael@0 98 mc_running_avg_y += mc_avg_y_stride;
michael@0 99 running_avg_y += avg_y_stride;
michael@0 100 }
michael@0 101
michael@0 102 {
michael@0 103 /* Compute the sum of all pixel differences of this MB. */
michael@0 104 union sum_union s;
michael@0 105 int sum_diff = 0;
michael@0 106 s.v = acc_diff;
michael@0 107 sum_diff = s.e[0] + s.e[1] + s.e[2] + s.e[3] + s.e[4] + s.e[5]
michael@0 108 + s.e[6] + s.e[7] + s.e[8] + s.e[9] + s.e[10] + s.e[11]
michael@0 109 + s.e[12] + s.e[13] + s.e[14] + s.e[15];
michael@0 110
michael@0 111 if (abs(sum_diff) > SUM_DIFF_THRESHOLD)
michael@0 112 {
michael@0 113 return COPY_BLOCK;
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 vp8_copy_mem16x16(running_avg->y_buffer + y_offset, avg_y_stride,
michael@0 118 signal->thismb, sig_stride);
michael@0 119 return FILTER_BLOCK;
michael@0 120 }

mercurial