OLD | NEW |
(Empty) | |
| 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 |
| 12 #include "vpx_config.h" |
| 13 #include "vp8_rtcd.h" |
| 14 #include "vpx_ports/x86.h" |
| 15 #include "vpx_mem/vpx_mem.h" |
| 16 #include "vp8/encoder/block.h" |
| 17 #include "vp8/common/entropy.h" /* vp8_default_inv_zig_zag */ |
| 18 |
| 19 #include <mmintrin.h> /* MMX */ |
| 20 #include <xmmintrin.h> /* SSE */ |
| 21 #include <emmintrin.h> /* SSE2 */ |
| 22 |
| 23 #define SELECT_EOB(i, z) \ |
| 24 do { \ |
| 25 __label__ select_eob_end; \ |
| 26 short boost = *zbin_boost_ptr; \ |
| 27 int cmp = (x[z] < boost) | (y[z] == 0); \ |
| 28 zbin_boost_ptr++; \ |
| 29 if (cmp) \ |
| 30 goto select_eob_end; \ |
| 31 qcoeff_ptr[z] = y[z]; \ |
| 32 eob = i; \ |
| 33 zbin_boost_ptr = b->zrun_zbin_boost; \ |
| 34 select_eob_end:; \ |
| 35 } while (0) |
| 36 |
| 37 void vp8_regular_quantize_b_sse2(BLOCK *b, BLOCKD *d) |
| 38 { |
| 39 char eob = 0; |
| 40 short *zbin_boost_ptr = b->zrun_zbin_boost; |
| 41 short *qcoeff_ptr = d->qcoeff; |
| 42 DECLARE_ALIGNED_ARRAY(16, short, x, 16); |
| 43 DECLARE_ALIGNED_ARRAY(16, short, y, 16); |
| 44 |
| 45 __m128i sz0, x0, sz1, x1, y0, y1, x_minus_zbin0, x_minus_zbin1; |
| 46 __m128i quant_shift0 = _mm_load_si128((__m128i *)(b->quant_shift)); |
| 47 __m128i quant_shift1 = _mm_load_si128((__m128i *)(b->quant_shift + 8)); |
| 48 __m128i z0 = _mm_load_si128((__m128i *)(b->coeff)); |
| 49 __m128i z1 = _mm_load_si128((__m128i *)(b->coeff+8)); |
| 50 __m128i zbin_extra = _mm_cvtsi32_si128(b->zbin_extra); |
| 51 __m128i zbin0 = _mm_load_si128((__m128i *)(b->zbin)); |
| 52 __m128i zbin1 = _mm_load_si128((__m128i *)(b->zbin + 8)); |
| 53 __m128i round0 = _mm_load_si128((__m128i *)(b->round)); |
| 54 __m128i round1 = _mm_load_si128((__m128i *)(b->round + 8)); |
| 55 __m128i quant0 = _mm_load_si128((__m128i *)(b->quant)); |
| 56 __m128i quant1 = _mm_load_si128((__m128i *)(b->quant + 8)); |
| 57 __m128i dequant0 = _mm_load_si128((__m128i *)(d->dequant)); |
| 58 __m128i dequant1 = _mm_load_si128((__m128i *)(d->dequant + 8)); |
| 59 |
| 60 vpx_memset(qcoeff_ptr, 0, 32); |
| 61 |
| 62 /* Duplicate to all lanes. */ |
| 63 zbin_extra = _mm_shufflelo_epi16(zbin_extra, 0); |
| 64 zbin_extra = _mm_unpacklo_epi16(zbin_extra, zbin_extra); |
| 65 |
| 66 /* Sign of z: z >> 15 */ |
| 67 sz0 = _mm_srai_epi16(z0, 15); |
| 68 sz1 = _mm_srai_epi16(z1, 15); |
| 69 |
| 70 /* x = abs(z): (z ^ sz) - sz */ |
| 71 x0 = _mm_xor_si128(z0, sz0); |
| 72 x1 = _mm_xor_si128(z1, sz1); |
| 73 x0 = _mm_sub_epi16(x0, sz0); |
| 74 x1 = _mm_sub_epi16(x1, sz1); |
| 75 |
| 76 /* zbin[] + zbin_extra */ |
| 77 zbin0 = _mm_add_epi16(zbin0, zbin_extra); |
| 78 zbin1 = _mm_add_epi16(zbin1, zbin_extra); |
| 79 |
| 80 /* In C x is compared to zbin where zbin = zbin[] + boost + extra. Rebalance |
| 81 * the equation because boost is the only value which can change: |
| 82 * x - (zbin[] + extra) >= boost */ |
| 83 x_minus_zbin0 = _mm_sub_epi16(x0, zbin0); |
| 84 x_minus_zbin1 = _mm_sub_epi16(x1, zbin1); |
| 85 |
| 86 _mm_store_si128((__m128i *)(x), x_minus_zbin0); |
| 87 _mm_store_si128((__m128i *)(x + 8), x_minus_zbin1); |
| 88 |
| 89 /* All the remaining calculations are valid whether they are done now with |
| 90 * simd or later inside the loop one at a time. */ |
| 91 x0 = _mm_add_epi16(x0, round0); |
| 92 x1 = _mm_add_epi16(x1, round1); |
| 93 |
| 94 y0 = _mm_mulhi_epi16(x0, quant0); |
| 95 y1 = _mm_mulhi_epi16(x1, quant1); |
| 96 |
| 97 y0 = _mm_add_epi16(y0, x0); |
| 98 y1 = _mm_add_epi16(y1, x1); |
| 99 |
| 100 /* Instead of shifting each value independently we convert the scaling |
| 101 * factor with 1 << (16 - shift) so we can use multiply/return high half. */ |
| 102 y0 = _mm_mulhi_epi16(y0, quant_shift0); |
| 103 y1 = _mm_mulhi_epi16(y1, quant_shift1); |
| 104 |
| 105 /* Return the sign: (y ^ sz) - sz */ |
| 106 y0 = _mm_xor_si128(y0, sz0); |
| 107 y1 = _mm_xor_si128(y1, sz1); |
| 108 y0 = _mm_sub_epi16(y0, sz0); |
| 109 y1 = _mm_sub_epi16(y1, sz1); |
| 110 |
| 111 _mm_store_si128((__m128i *)(y), y0); |
| 112 _mm_store_si128((__m128i *)(y + 8), y1); |
| 113 |
| 114 zbin_boost_ptr = b->zrun_zbin_boost; |
| 115 |
| 116 /* The loop gets unrolled anyway. Avoid the vp8_default_zig_zag1d lookup. */ |
| 117 SELECT_EOB(1, 0); |
| 118 SELECT_EOB(2, 1); |
| 119 SELECT_EOB(3, 4); |
| 120 SELECT_EOB(4, 8); |
| 121 SELECT_EOB(5, 5); |
| 122 SELECT_EOB(6, 2); |
| 123 SELECT_EOB(7, 3); |
| 124 SELECT_EOB(8, 6); |
| 125 SELECT_EOB(9, 9); |
| 126 SELECT_EOB(10, 12); |
| 127 SELECT_EOB(11, 13); |
| 128 SELECT_EOB(12, 10); |
| 129 SELECT_EOB(13, 7); |
| 130 SELECT_EOB(14, 11); |
| 131 SELECT_EOB(15, 14); |
| 132 SELECT_EOB(16, 15); |
| 133 |
| 134 y0 = _mm_load_si128((__m128i *)(d->qcoeff)); |
| 135 y1 = _mm_load_si128((__m128i *)(d->qcoeff + 8)); |
| 136 |
| 137 /* dqcoeff = qcoeff * dequant */ |
| 138 y0 = _mm_mullo_epi16(y0, dequant0); |
| 139 y1 = _mm_mullo_epi16(y1, dequant1); |
| 140 |
| 141 _mm_store_si128((__m128i *)(d->dqcoeff), y0); |
| 142 _mm_store_si128((__m128i *)(d->dqcoeff + 8), y1); |
| 143 |
| 144 *d->eob = eob; |
| 145 } |
| 146 |
| 147 void vp8_fast_quantize_b_sse2(BLOCK *b, BLOCKD *d) |
| 148 { |
| 149 __m128i z0 = _mm_load_si128((__m128i *)(b->coeff)); |
| 150 __m128i z1 = _mm_load_si128((__m128i *)(b->coeff + 8)); |
| 151 __m128i round0 = _mm_load_si128((__m128i *)(b->round)); |
| 152 __m128i round1 = _mm_load_si128((__m128i *)(b->round + 8)); |
| 153 __m128i quant_fast0 = _mm_load_si128((__m128i *)(b->quant_fast)); |
| 154 __m128i quant_fast1 = _mm_load_si128((__m128i *)(b->quant_fast + 8)); |
| 155 __m128i dequant0 = _mm_load_si128((__m128i *)(d->dequant)); |
| 156 __m128i dequant1 = _mm_load_si128((__m128i *)(d->dequant + 8)); |
| 157 __m128i inv_zig_zag0 = _mm_load_si128((const __m128i *)(vp8_default_inv_zig_za
g)); |
| 158 __m128i inv_zig_zag1 = _mm_load_si128((const __m128i *)(vp8_default_inv_zig_za
g + 8)); |
| 159 |
| 160 __m128i sz0, sz1, x0, x1, y0, y1, xdq0, xdq1, zeros, ones; |
| 161 |
| 162 /* sign of z: z >> 15 */ |
| 163 sz0 = _mm_srai_epi16(z0, 15); |
| 164 sz1 = _mm_srai_epi16(z1, 15); |
| 165 |
| 166 /* x = abs(z): (z ^ sz) - sz */ |
| 167 x0 = _mm_xor_si128(z0, sz0); |
| 168 x1 = _mm_xor_si128(z1, sz1); |
| 169 x0 = _mm_sub_epi16(x0, sz0); |
| 170 x1 = _mm_sub_epi16(x1, sz1); |
| 171 |
| 172 /* x += round */ |
| 173 x0 = _mm_add_epi16(x0, round0); |
| 174 x1 = _mm_add_epi16(x1, round1); |
| 175 |
| 176 /* y = (x * quant) >> 16 */ |
| 177 y0 = _mm_mulhi_epi16(x0, quant_fast0); |
| 178 y1 = _mm_mulhi_epi16(x1, quant_fast1); |
| 179 |
| 180 /* x = abs(y) = (y ^ sz) - sz */ |
| 181 y0 = _mm_xor_si128(y0, sz0); |
| 182 y1 = _mm_xor_si128(y1, sz1); |
| 183 x0 = _mm_sub_epi16(y0, sz0); |
| 184 x1 = _mm_sub_epi16(y1, sz1); |
| 185 |
| 186 /* qcoeff = x */ |
| 187 _mm_store_si128((__m128i *)(d->qcoeff), x0); |
| 188 _mm_store_si128((__m128i *)(d->qcoeff + 8), x1); |
| 189 |
| 190 /* x * dequant */ |
| 191 xdq0 = _mm_mullo_epi16(x0, dequant0); |
| 192 xdq1 = _mm_mullo_epi16(x1, dequant1); |
| 193 |
| 194 /* dqcoeff = x * dequant */ |
| 195 _mm_store_si128((__m128i *)(d->dqcoeff), xdq0); |
| 196 _mm_store_si128((__m128i *)(d->dqcoeff + 8), xdq1); |
| 197 |
| 198 /* build a mask for the zig zag */ |
| 199 zeros = _mm_setzero_si128(); |
| 200 |
| 201 x0 = _mm_cmpeq_epi16(x0, zeros); |
| 202 x1 = _mm_cmpeq_epi16(x1, zeros); |
| 203 |
| 204 ones = _mm_cmpeq_epi16(zeros, zeros); |
| 205 |
| 206 x0 = _mm_xor_si128(x0, ones); |
| 207 x1 = _mm_xor_si128(x1, ones); |
| 208 |
| 209 x0 = _mm_and_si128(x0, inv_zig_zag0); |
| 210 x1 = _mm_and_si128(x1, inv_zig_zag1); |
| 211 |
| 212 x0 = _mm_max_epi16(x0, x1); |
| 213 |
| 214 /* now down to 8 */ |
| 215 x1 = _mm_shuffle_epi32(x0, 0xE); // 0b00001110 |
| 216 |
| 217 x0 = _mm_max_epi16(x0, x1); |
| 218 |
| 219 /* only 4 left */ |
| 220 x1 = _mm_shufflelo_epi16(x0, 0xE); // 0b00001110 |
| 221 |
| 222 x0 = _mm_max_epi16(x0, x1); |
| 223 |
| 224 /* okay, just 2! */ |
| 225 x1 = _mm_shufflelo_epi16(x0, 0x1); // 0b00000001 |
| 226 |
| 227 x0 = _mm_max_epi16(x0, x1); |
| 228 |
| 229 *d->eob = 0xFF & _mm_cvtsi128_si32(x0); |
| 230 } |
OLD | NEW |