/src/libwebp/src/dsp/cost_sse2.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2015 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style license |
4 | | // that can be found in the COPYING file in the root of the source |
5 | | // tree. An additional intellectual property rights grant can be found |
6 | | // in the file PATENTS. All contributing project authors may |
7 | | // be found in the AUTHORS file in the root of the source tree. |
8 | | // ----------------------------------------------------------------------------- |
9 | | // |
10 | | // SSE2 version of cost functions |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include "src/dsp/dsp.h" |
15 | | |
16 | | #if defined(WEBP_USE_SSE2) |
17 | | #include <emmintrin.h> |
18 | | |
19 | | #include <assert.h> |
20 | | |
21 | | #include "src/webp/types.h" |
22 | | #include "src/dsp/cpu.h" |
23 | | #include "src/enc/cost_enc.h" |
24 | | #include "src/enc/vp8i_enc.h" |
25 | | #include "src/utils/utils.h" |
26 | | |
27 | | //------------------------------------------------------------------------------ |
28 | | |
29 | | static void SetResidualCoeffs_SSE2(const int16_t* WEBP_RESTRICT const coeffs, |
30 | 0 | VP8Residual* WEBP_RESTRICT const res) { |
31 | 0 | const __m128i c0 = _mm_loadu_si128((const __m128i*)(coeffs + 0)); |
32 | 0 | const __m128i c1 = _mm_loadu_si128((const __m128i*)(coeffs + 8)); |
33 | | // Use SSE2 to compare 16 values with a single instruction. |
34 | 0 | const __m128i zero = _mm_setzero_si128(); |
35 | 0 | const __m128i m0 = _mm_packs_epi16(c0, c1); |
36 | 0 | const __m128i m1 = _mm_cmpeq_epi8(m0, zero); |
37 | | // Get the comparison results as a bitmask into 16bits. Negate the mask to get |
38 | | // the position of entries that are not equal to zero. We don't need to mask |
39 | | // out least significant bits according to res->first, since coeffs[0] is 0 |
40 | | // if res->first > 0. |
41 | 0 | const uint32_t mask = 0x0000ffffu ^ (uint32_t)_mm_movemask_epi8(m1); |
42 | | // The position of the most significant non-zero bit indicates the position of |
43 | | // the last non-zero value. |
44 | 0 | assert(res->first == 0 || coeffs[0] == 0); |
45 | 0 | res->last = mask ? BitsLog2Floor(mask) : -1; |
46 | 0 | res->coeffs = coeffs; |
47 | 0 | } |
48 | | |
49 | 0 | static int GetResidualCost_SSE2(int ctx0, const VP8Residual* const res) { |
50 | 0 | uint8_t levels[16], ctxs[16]; |
51 | 0 | uint16_t abs_levels[16]; |
52 | 0 | int n = res->first; |
53 | | // should be prob[VP8EncBands[n]], but it's equivalent for n=0 or 1 |
54 | 0 | const int p0 = res->prob[n][ctx0][0]; |
55 | 0 | CostArrayPtr const costs = res->costs; |
56 | 0 | const uint16_t* t = costs[n][ctx0]; |
57 | | // bit_cost(1, p0) is already incorporated in t[] tables, but only if ctx != 0 |
58 | | // (as required by the syntax). For ctx0 == 0, we need to add it here or it'll |
59 | | // be missing during the loop. |
60 | 0 | int cost = (ctx0 == 0) ? VP8BitCost(1, p0) : 0; |
61 | |
|
62 | 0 | if (res->last < 0) { |
63 | 0 | return VP8BitCost(0, p0); |
64 | 0 | } |
65 | | |
66 | 0 | { // precompute clamped levels and contexts, packed to 8b. |
67 | 0 | const __m128i zero = _mm_setzero_si128(); |
68 | 0 | const __m128i kCst2 = _mm_set1_epi8(2); |
69 | 0 | const __m128i kCst67 = _mm_set1_epi8(MAX_VARIABLE_LEVEL); |
70 | 0 | const __m128i c0 = _mm_loadu_si128((const __m128i*)&res->coeffs[0]); |
71 | 0 | const __m128i c1 = _mm_loadu_si128((const __m128i*)&res->coeffs[8]); |
72 | 0 | const __m128i D0 = _mm_sub_epi16(zero, c0); |
73 | 0 | const __m128i D1 = _mm_sub_epi16(zero, c1); |
74 | 0 | const __m128i E0 = _mm_max_epi16(c0, D0); // abs(v), 16b |
75 | 0 | const __m128i E1 = _mm_max_epi16(c1, D1); |
76 | 0 | const __m128i F = _mm_packs_epi16(E0, E1); |
77 | 0 | const __m128i G = _mm_min_epu8(F, kCst2); // context = 0,1,2 |
78 | 0 | const __m128i H = _mm_min_epu8(F, kCst67); // clamp_level in [0..67] |
79 | |
|
80 | 0 | _mm_storeu_si128((__m128i*)&ctxs[0], G); |
81 | 0 | _mm_storeu_si128((__m128i*)&levels[0], H); |
82 | |
|
83 | 0 | _mm_storeu_si128((__m128i*)&abs_levels[0], E0); |
84 | 0 | _mm_storeu_si128((__m128i*)&abs_levels[8], E1); |
85 | 0 | } |
86 | 0 | for (; n < res->last; ++n) { |
87 | 0 | const int ctx = ctxs[n]; |
88 | 0 | const int level = levels[n]; |
89 | 0 | const int flevel = abs_levels[n]; // full level |
90 | 0 | cost += VP8LevelFixedCosts[flevel] + t[level]; // simplified VP8LevelCost() |
91 | 0 | t = costs[n + 1][ctx]; |
92 | 0 | } |
93 | | // Last coefficient is always non-zero |
94 | 0 | { |
95 | 0 | const int level = levels[n]; |
96 | 0 | const int flevel = abs_levels[n]; |
97 | 0 | assert(flevel != 0); |
98 | 0 | cost += VP8LevelFixedCosts[flevel] + t[level]; |
99 | 0 | if (n < 15) { |
100 | 0 | const int b = VP8EncBands[n + 1]; |
101 | 0 | const int ctx = ctxs[n]; |
102 | 0 | const int last_p0 = res->prob[b][ctx][0]; |
103 | 0 | cost += VP8BitCost(0, last_p0); |
104 | 0 | } |
105 | 0 | } |
106 | 0 | return cost; |
107 | 0 | } |
108 | | |
109 | | //------------------------------------------------------------------------------ |
110 | | // Entry point |
111 | | |
112 | | extern void VP8EncDspCostInitSSE2(void); |
113 | | |
114 | 0 | WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspCostInitSSE2(void) { |
115 | 0 | VP8SetResidualCoeffs = SetResidualCoeffs_SSE2; |
116 | 0 | VP8GetResidualCost = GetResidualCost_SSE2; |
117 | 0 | } |
118 | | |
119 | | #else // !WEBP_USE_SSE2 |
120 | | |
121 | | WEBP_DSP_INIT_STUB(VP8EncDspCostInitSSE2) |
122 | | |
123 | | #endif // WEBP_USE_SSE2 |