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
11 #include <smmintrin.h> /* SSE4.1 */
12
13 #include "./vp8_rtcd.h"
14 #include "vp8/encoder/block.h"
15 #include "vp8/common/entropy.h" /* vp8_default_inv_zig_zag */
16
17 #define SELECT_EOB(i, z, x, y, q) \
18 do { \
19 short boost = *zbin_boost_ptr; \
20 short x_z = _mm_extract_epi16(x, z); \
21 short y_z = _mm_extract_epi16(y, z); \
22 int cmp = (x_z < boost) | (y_z == 0); \
23 zbin_boost_ptr++; \
24 if (cmp) break; \
25 q = _mm_insert_epi16(q, y_z, z); \
26 eob = i; \
27 zbin_boost_ptr = b->zrun_zbin_boost; \
28 } while (0)
29
vp8_regular_quantize_b_sse4_1(BLOCK * b,BLOCKD * d)30 void vp8_regular_quantize_b_sse4_1(BLOCK *b, BLOCKD *d) {
31 char eob = 0;
32 short *zbin_boost_ptr = b->zrun_zbin_boost;
33
34 __m128i sz0, x0, sz1, x1, y0, y1, x_minus_zbin0, x_minus_zbin1, dqcoeff0,
35 dqcoeff1;
36 __m128i quant_shift0 = _mm_load_si128((__m128i *)(b->quant_shift));
37 __m128i quant_shift1 = _mm_load_si128((__m128i *)(b->quant_shift + 8));
38 __m128i z0 = _mm_load_si128((__m128i *)(b->coeff));
39 __m128i z1 = _mm_load_si128((__m128i *)(b->coeff + 8));
40 __m128i zbin_extra = _mm_cvtsi32_si128(b->zbin_extra);
41 __m128i zbin0 = _mm_load_si128((__m128i *)(b->zbin));
42 __m128i zbin1 = _mm_load_si128((__m128i *)(b->zbin + 8));
43 __m128i round0 = _mm_load_si128((__m128i *)(b->round));
44 __m128i round1 = _mm_load_si128((__m128i *)(b->round + 8));
45 __m128i quant0 = _mm_load_si128((__m128i *)(b->quant));
46 __m128i quant1 = _mm_load_si128((__m128i *)(b->quant + 8));
47 __m128i dequant0 = _mm_load_si128((__m128i *)(d->dequant));
48 __m128i dequant1 = _mm_load_si128((__m128i *)(d->dequant + 8));
49 __m128i qcoeff0 = _mm_setzero_si128();
50 __m128i qcoeff1 = _mm_setzero_si128();
51
52 /* Duplicate to all lanes. */
53 zbin_extra = _mm_shufflelo_epi16(zbin_extra, 0);
54 zbin_extra = _mm_unpacklo_epi16(zbin_extra, zbin_extra);
55
56 /* Sign of z: z >> 15 */
57 sz0 = _mm_srai_epi16(z0, 15);
58 sz1 = _mm_srai_epi16(z1, 15);
59
60 /* x = abs(z): (z ^ sz) - sz */
61 x0 = _mm_xor_si128(z0, sz0);
62 x1 = _mm_xor_si128(z1, sz1);
63 x0 = _mm_sub_epi16(x0, sz0);
64 x1 = _mm_sub_epi16(x1, sz1);
65
66 /* zbin[] + zbin_extra */
67 zbin0 = _mm_add_epi16(zbin0, zbin_extra);
68 zbin1 = _mm_add_epi16(zbin1, zbin_extra);
69
70 /* In C x is compared to zbin where zbin = zbin[] + boost + extra. Rebalance
71 * the equation because boost is the only value which can change:
72 * x - (zbin[] + extra) >= boost */
73 x_minus_zbin0 = _mm_sub_epi16(x0, zbin0);
74 x_minus_zbin1 = _mm_sub_epi16(x1, zbin1);
75
76 /* All the remaining calculations are valid whether they are done now with
77 * simd or later inside the loop one at a time. */
78 x0 = _mm_add_epi16(x0, round0);
79 x1 = _mm_add_epi16(x1, round1);
80
81 y0 = _mm_mulhi_epi16(x0, quant0);
82 y1 = _mm_mulhi_epi16(x1, quant1);
83
84 y0 = _mm_add_epi16(y0, x0);
85 y1 = _mm_add_epi16(y1, x1);
86
87 /* Instead of shifting each value independently we convert the scaling
88 * factor with 1 << (16 - shift) so we can use multiply/return high half. */
89 y0 = _mm_mulhi_epi16(y0, quant_shift0);
90 y1 = _mm_mulhi_epi16(y1, quant_shift1);
91
92 /* Return the sign: (y ^ sz) - sz */
93 y0 = _mm_xor_si128(y0, sz0);
94 y1 = _mm_xor_si128(y1, sz1);
95 y0 = _mm_sub_epi16(y0, sz0);
96 y1 = _mm_sub_epi16(y1, sz1);
97
98 /* The loop gets unrolled anyway. Avoid the vp8_default_zig_zag1d lookup. */
99 SELECT_EOB(1, 0, x_minus_zbin0, y0, qcoeff0);
100 SELECT_EOB(2, 1, x_minus_zbin0, y0, qcoeff0);
101 SELECT_EOB(3, 4, x_minus_zbin0, y0, qcoeff0);
102 SELECT_EOB(4, 0, x_minus_zbin1, y1, qcoeff1);
103 SELECT_EOB(5, 5, x_minus_zbin0, y0, qcoeff0);
104 SELECT_EOB(6, 2, x_minus_zbin0, y0, qcoeff0);
105 SELECT_EOB(7, 3, x_minus_zbin0, y0, qcoeff0);
106 SELECT_EOB(8, 6, x_minus_zbin0, y0, qcoeff0);
107 SELECT_EOB(9, 1, x_minus_zbin1, y1, qcoeff1);
108 SELECT_EOB(10, 4, x_minus_zbin1, y1, qcoeff1);
109 SELECT_EOB(11, 5, x_minus_zbin1, y1, qcoeff1);
110 SELECT_EOB(12, 2, x_minus_zbin1, y1, qcoeff1);
111 SELECT_EOB(13, 7, x_minus_zbin0, y0, qcoeff0);
112 SELECT_EOB(14, 3, x_minus_zbin1, y1, qcoeff1);
113 SELECT_EOB(15, 6, x_minus_zbin1, y1, qcoeff1);
114 SELECT_EOB(16, 7, x_minus_zbin1, y1, qcoeff1);
115
116 _mm_store_si128((__m128i *)(d->qcoeff), qcoeff0);
117 _mm_store_si128((__m128i *)(d->qcoeff + 8), qcoeff1);
118
119 dqcoeff0 = _mm_mullo_epi16(qcoeff0, dequant0);
120 dqcoeff1 = _mm_mullo_epi16(qcoeff1, dequant1);
121
122 _mm_store_si128((__m128i *)(d->dqcoeff), dqcoeff0);
123 _mm_store_si128((__m128i *)(d->dqcoeff + 8), dqcoeff1);
124
125 *d->eob = eob;
126 }
127