michael@0: /* michael@0: * Copyright (c) 2012 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include "vpx_config.h" michael@0: #include "vp8_rtcd.h" michael@0: #include "vpx_ports/x86.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vp8/encoder/block.h" michael@0: #include "vp8/common/entropy.h" /* vp8_default_inv_zig_zag */ michael@0: michael@0: #include /* MMX */ michael@0: #include /* SSE */ michael@0: #include /* SSE2 */ michael@0: michael@0: #define SELECT_EOB(i, z) \ michael@0: do { \ michael@0: short boost = *zbin_boost_ptr; \ michael@0: int cmp = (x[z] < boost) | (y[z] == 0); \ michael@0: zbin_boost_ptr++; \ michael@0: if (cmp) \ michael@0: goto select_eob_end_##i; \ michael@0: qcoeff_ptr[z] = y[z]; \ michael@0: eob = i; \ michael@0: zbin_boost_ptr = b->zrun_zbin_boost; \ michael@0: select_eob_end_##i:; \ michael@0: } while (0) michael@0: michael@0: void vp8_regular_quantize_b_sse2(BLOCK *b, BLOCKD *d) michael@0: { michael@0: char eob = 0; michael@0: short *zbin_boost_ptr = b->zrun_zbin_boost; michael@0: short *qcoeff_ptr = d->qcoeff; michael@0: DECLARE_ALIGNED_ARRAY(16, short, x, 16); michael@0: DECLARE_ALIGNED_ARRAY(16, short, y, 16); michael@0: michael@0: __m128i sz0, x0, sz1, x1, y0, y1, x_minus_zbin0, x_minus_zbin1; michael@0: __m128i quant_shift0 = _mm_load_si128((__m128i *)(b->quant_shift)); michael@0: __m128i quant_shift1 = _mm_load_si128((__m128i *)(b->quant_shift + 8)); michael@0: __m128i z0 = _mm_load_si128((__m128i *)(b->coeff)); michael@0: __m128i z1 = _mm_load_si128((__m128i *)(b->coeff+8)); michael@0: __m128i zbin_extra = _mm_cvtsi32_si128(b->zbin_extra); michael@0: __m128i zbin0 = _mm_load_si128((__m128i *)(b->zbin)); michael@0: __m128i zbin1 = _mm_load_si128((__m128i *)(b->zbin + 8)); michael@0: __m128i round0 = _mm_load_si128((__m128i *)(b->round)); michael@0: __m128i round1 = _mm_load_si128((__m128i *)(b->round + 8)); michael@0: __m128i quant0 = _mm_load_si128((__m128i *)(b->quant)); michael@0: __m128i quant1 = _mm_load_si128((__m128i *)(b->quant + 8)); michael@0: __m128i dequant0 = _mm_load_si128((__m128i *)(d->dequant)); michael@0: __m128i dequant1 = _mm_load_si128((__m128i *)(d->dequant + 8)); michael@0: michael@0: vpx_memset(qcoeff_ptr, 0, 32); michael@0: michael@0: /* Duplicate to all lanes. */ michael@0: zbin_extra = _mm_shufflelo_epi16(zbin_extra, 0); michael@0: zbin_extra = _mm_unpacklo_epi16(zbin_extra, zbin_extra); michael@0: michael@0: /* Sign of z: z >> 15 */ michael@0: sz0 = _mm_srai_epi16(z0, 15); michael@0: sz1 = _mm_srai_epi16(z1, 15); michael@0: michael@0: /* x = abs(z): (z ^ sz) - sz */ michael@0: x0 = _mm_xor_si128(z0, sz0); michael@0: x1 = _mm_xor_si128(z1, sz1); michael@0: x0 = _mm_sub_epi16(x0, sz0); michael@0: x1 = _mm_sub_epi16(x1, sz1); michael@0: michael@0: /* zbin[] + zbin_extra */ michael@0: zbin0 = _mm_add_epi16(zbin0, zbin_extra); michael@0: zbin1 = _mm_add_epi16(zbin1, zbin_extra); michael@0: michael@0: /* In C x is compared to zbin where zbin = zbin[] + boost + extra. Rebalance michael@0: * the equation because boost is the only value which can change: michael@0: * x - (zbin[] + extra) >= boost */ michael@0: x_minus_zbin0 = _mm_sub_epi16(x0, zbin0); michael@0: x_minus_zbin1 = _mm_sub_epi16(x1, zbin1); michael@0: michael@0: _mm_store_si128((__m128i *)(x), x_minus_zbin0); michael@0: _mm_store_si128((__m128i *)(x + 8), x_minus_zbin1); michael@0: michael@0: /* All the remaining calculations are valid whether they are done now with michael@0: * simd or later inside the loop one at a time. */ michael@0: x0 = _mm_add_epi16(x0, round0); michael@0: x1 = _mm_add_epi16(x1, round1); michael@0: michael@0: y0 = _mm_mulhi_epi16(x0, quant0); michael@0: y1 = _mm_mulhi_epi16(x1, quant1); michael@0: michael@0: y0 = _mm_add_epi16(y0, x0); michael@0: y1 = _mm_add_epi16(y1, x1); michael@0: michael@0: /* Instead of shifting each value independently we convert the scaling michael@0: * factor with 1 << (16 - shift) so we can use multiply/return high half. */ michael@0: y0 = _mm_mulhi_epi16(y0, quant_shift0); michael@0: y1 = _mm_mulhi_epi16(y1, quant_shift1); michael@0: michael@0: /* Return the sign: (y ^ sz) - sz */ michael@0: y0 = _mm_xor_si128(y0, sz0); michael@0: y1 = _mm_xor_si128(y1, sz1); michael@0: y0 = _mm_sub_epi16(y0, sz0); michael@0: y1 = _mm_sub_epi16(y1, sz1); michael@0: michael@0: _mm_store_si128((__m128i *)(y), y0); michael@0: _mm_store_si128((__m128i *)(y + 8), y1); michael@0: michael@0: zbin_boost_ptr = b->zrun_zbin_boost; michael@0: michael@0: /* The loop gets unrolled anyway. Avoid the vp8_default_zig_zag1d lookup. */ michael@0: SELECT_EOB(1, 0); michael@0: SELECT_EOB(2, 1); michael@0: SELECT_EOB(3, 4); michael@0: SELECT_EOB(4, 8); michael@0: SELECT_EOB(5, 5); michael@0: SELECT_EOB(6, 2); michael@0: SELECT_EOB(7, 3); michael@0: SELECT_EOB(8, 6); michael@0: SELECT_EOB(9, 9); michael@0: SELECT_EOB(10, 12); michael@0: SELECT_EOB(11, 13); michael@0: SELECT_EOB(12, 10); michael@0: SELECT_EOB(13, 7); michael@0: SELECT_EOB(14, 11); michael@0: SELECT_EOB(15, 14); michael@0: SELECT_EOB(16, 15); michael@0: michael@0: y0 = _mm_load_si128((__m128i *)(d->qcoeff)); michael@0: y1 = _mm_load_si128((__m128i *)(d->qcoeff + 8)); michael@0: michael@0: /* dqcoeff = qcoeff * dequant */ michael@0: y0 = _mm_mullo_epi16(y0, dequant0); michael@0: y1 = _mm_mullo_epi16(y1, dequant1); michael@0: michael@0: _mm_store_si128((__m128i *)(d->dqcoeff), y0); michael@0: _mm_store_si128((__m128i *)(d->dqcoeff + 8), y1); michael@0: michael@0: *d->eob = eob; michael@0: } michael@0: michael@0: void vp8_fast_quantize_b_sse2(BLOCK *b, BLOCKD *d) michael@0: { michael@0: __m128i z0 = _mm_load_si128((__m128i *)(b->coeff)); michael@0: __m128i z1 = _mm_load_si128((__m128i *)(b->coeff + 8)); michael@0: __m128i round0 = _mm_load_si128((__m128i *)(b->round)); michael@0: __m128i round1 = _mm_load_si128((__m128i *)(b->round + 8)); michael@0: __m128i quant_fast0 = _mm_load_si128((__m128i *)(b->quant_fast)); michael@0: __m128i quant_fast1 = _mm_load_si128((__m128i *)(b->quant_fast + 8)); michael@0: __m128i dequant0 = _mm_load_si128((__m128i *)(d->dequant)); michael@0: __m128i dequant1 = _mm_load_si128((__m128i *)(d->dequant + 8)); michael@0: __m128i inv_zig_zag0 = _mm_load_si128((const __m128i *)(vp8_default_inv_zig_zag)); michael@0: __m128i inv_zig_zag1 = _mm_load_si128((const __m128i *)(vp8_default_inv_zig_zag + 8)); michael@0: michael@0: __m128i sz0, sz1, x0, x1, y0, y1, xdq0, xdq1, zeros, ones; michael@0: michael@0: /* sign of z: z >> 15 */ michael@0: sz0 = _mm_srai_epi16(z0, 15); michael@0: sz1 = _mm_srai_epi16(z1, 15); michael@0: michael@0: /* x = abs(z): (z ^ sz) - sz */ michael@0: x0 = _mm_xor_si128(z0, sz0); michael@0: x1 = _mm_xor_si128(z1, sz1); michael@0: x0 = _mm_sub_epi16(x0, sz0); michael@0: x1 = _mm_sub_epi16(x1, sz1); michael@0: michael@0: /* x += round */ michael@0: x0 = _mm_add_epi16(x0, round0); michael@0: x1 = _mm_add_epi16(x1, round1); michael@0: michael@0: /* y = (x * quant) >> 16 */ michael@0: y0 = _mm_mulhi_epi16(x0, quant_fast0); michael@0: y1 = _mm_mulhi_epi16(x1, quant_fast1); michael@0: michael@0: /* x = abs(y) = (y ^ sz) - sz */ michael@0: y0 = _mm_xor_si128(y0, sz0); michael@0: y1 = _mm_xor_si128(y1, sz1); michael@0: x0 = _mm_sub_epi16(y0, sz0); michael@0: x1 = _mm_sub_epi16(y1, sz1); michael@0: michael@0: /* qcoeff = x */ michael@0: _mm_store_si128((__m128i *)(d->qcoeff), x0); michael@0: _mm_store_si128((__m128i *)(d->qcoeff + 8), x1); michael@0: michael@0: /* x * dequant */ michael@0: xdq0 = _mm_mullo_epi16(x0, dequant0); michael@0: xdq1 = _mm_mullo_epi16(x1, dequant1); michael@0: michael@0: /* dqcoeff = x * dequant */ michael@0: _mm_store_si128((__m128i *)(d->dqcoeff), xdq0); michael@0: _mm_store_si128((__m128i *)(d->dqcoeff + 8), xdq1); michael@0: michael@0: /* build a mask for the zig zag */ michael@0: zeros = _mm_setzero_si128(); michael@0: michael@0: x0 = _mm_cmpeq_epi16(x0, zeros); michael@0: x1 = _mm_cmpeq_epi16(x1, zeros); michael@0: michael@0: ones = _mm_cmpeq_epi16(zeros, zeros); michael@0: michael@0: x0 = _mm_xor_si128(x0, ones); michael@0: x1 = _mm_xor_si128(x1, ones); michael@0: michael@0: x0 = _mm_and_si128(x0, inv_zig_zag0); michael@0: x1 = _mm_and_si128(x1, inv_zig_zag1); michael@0: michael@0: x0 = _mm_max_epi16(x0, x1); michael@0: michael@0: /* now down to 8 */ michael@0: x1 = _mm_shuffle_epi32(x0, 0xE); // 0b00001110 michael@0: michael@0: x0 = _mm_max_epi16(x0, x1); michael@0: michael@0: /* only 4 left */ michael@0: x1 = _mm_shufflelo_epi16(x0, 0xE); // 0b00001110 michael@0: michael@0: x0 = _mm_max_epi16(x0, x1); michael@0: michael@0: /* okay, just 2! */ michael@0: x1 = _mm_shufflelo_epi16(x0, 0x1); // 0b00000001 michael@0: michael@0: x0 = _mm_max_epi16(x0, x1); michael@0: michael@0: *d->eob = 0xFF & _mm_cvtsi128_si32(x0); michael@0: }