michael@0: /* michael@0: * Copyright 2012 The LibYuv 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: #include "libyuv/rotate.h" michael@0: michael@0: #include "libyuv/cpu_id.h" michael@0: #include "libyuv/convert.h" michael@0: #include "libyuv/planar_functions.h" michael@0: #include "libyuv/row.h" michael@0: michael@0: #ifdef __cplusplus michael@0: namespace libyuv { michael@0: extern "C" { michael@0: #endif michael@0: michael@0: // ARGBScale has a function to copy pixels to a row, striding each source michael@0: // pixel by a constant. michael@0: #if !defined(LIBYUV_DISABLE_X86) && \ michael@0: (defined(_M_IX86) || \ michael@0: (defined(__x86_64__) && !defined(__native_client__)) || defined(__i386__)) michael@0: #define HAS_SCALEARGBROWDOWNEVEN_SSE2 michael@0: void ScaleARGBRowDownEven_SSE2(const uint8* src_ptr, int src_stride, michael@0: int src_stepx, michael@0: uint8* dst_ptr, int dst_width); michael@0: #endif michael@0: #if !defined(LIBYUV_DISABLE_NEON) && !defined(__native_client__) && \ michael@0: (defined(__ARM_NEON__) || defined(LIBYUV_NEON)) michael@0: #define HAS_SCALEARGBROWDOWNEVEN_NEON michael@0: void ScaleARGBRowDownEven_NEON(const uint8* src_ptr, int src_stride, michael@0: int src_stepx, michael@0: uint8* dst_ptr, int dst_width); michael@0: #endif michael@0: michael@0: void ScaleARGBRowDownEven_C(const uint8* src_ptr, int, michael@0: int src_stepx, michael@0: uint8* dst_ptr, int dst_width); michael@0: michael@0: static void ARGBTranspose(const uint8* src, int src_stride, michael@0: uint8* dst, int dst_stride, michael@0: int width, int height) { michael@0: int i; michael@0: int src_pixel_step = src_stride >> 2; michael@0: void (*ScaleARGBRowDownEven)(const uint8* src_ptr, int src_stride, michael@0: int src_step, uint8* dst_ptr, int dst_width) = ScaleARGBRowDownEven_C; michael@0: #if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(height, 4) && // Width of dest. michael@0: IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2; michael@0: } michael@0: #elif defined(HAS_SCALEARGBROWDOWNEVEN_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(height, 4) && // Width of dest. michael@0: IS_ALIGNED(src, 4)) { michael@0: ScaleARGBRowDownEven = ScaleARGBRowDownEven_NEON; michael@0: } michael@0: #endif michael@0: michael@0: for (i = 0; i < width; ++i) { // column of source to row of dest. michael@0: ScaleARGBRowDownEven(src, 0, src_pixel_step, dst, height); michael@0: dst += dst_stride; michael@0: src += 4; michael@0: } michael@0: } michael@0: michael@0: void ARGBRotate90(const uint8* src, int src_stride, michael@0: uint8* dst, int dst_stride, michael@0: int width, int height) { michael@0: // Rotate by 90 is a ARGBTranspose with the source read michael@0: // from bottom to top. So set the source pointer to the end michael@0: // of the buffer and flip the sign of the source stride. michael@0: src += src_stride * (height - 1); michael@0: src_stride = -src_stride; michael@0: ARGBTranspose(src, src_stride, dst, dst_stride, width, height); michael@0: } michael@0: michael@0: void ARGBRotate270(const uint8* src, int src_stride, michael@0: uint8* dst, int dst_stride, michael@0: int width, int height) { michael@0: // Rotate by 270 is a ARGBTranspose with the destination written michael@0: // from bottom to top. So set the destination pointer to the end michael@0: // of the buffer and flip the sign of the destination stride. michael@0: dst += dst_stride * (width - 1); michael@0: dst_stride = -dst_stride; michael@0: ARGBTranspose(src, src_stride, dst, dst_stride, width, height); michael@0: } michael@0: michael@0: void ARGBRotate180(const uint8* src, int src_stride, michael@0: uint8* dst, int dst_stride, michael@0: int width, int height) { michael@0: // Swap first and last row and mirror the content. Uses a temporary row. michael@0: align_buffer_64(row, width * 4); michael@0: const uint8* src_bot = src + src_stride * (height - 1); michael@0: uint8* dst_bot = dst + dst_stride * (height - 1); michael@0: int half_height = (height + 1) >> 1; michael@0: int y; michael@0: void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) = michael@0: ARGBMirrorRow_C; michael@0: void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C; michael@0: #if defined(HAS_ARGBMIRRORROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4) && michael@0: IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) && michael@0: IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ARGBMirrorRow = ARGBMirrorRow_SSSE3; michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBMIRRORROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 8)) { michael@0: ARGBMirrorRow = ARGBMirrorRow_AVX2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBMIRRORROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 4)) { michael@0: ARGBMirrorRow = ARGBMirrorRow_NEON; michael@0: } michael@0: #endif michael@0: #if defined(HAS_COPYROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width * 4, 32)) { michael@0: CopyRow = CopyRow_NEON; michael@0: } michael@0: #endif michael@0: #if defined(HAS_COPYROW_X86) michael@0: if (TestCpuFlag(kCpuHasX86)) { michael@0: CopyRow = CopyRow_X86; michael@0: } michael@0: #endif michael@0: #if defined(HAS_COPYROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width * 4, 32) && michael@0: IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) && michael@0: IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: CopyRow = CopyRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_COPYROW_ERMS) michael@0: if (TestCpuFlag(kCpuHasERMS)) { michael@0: CopyRow = CopyRow_ERMS; michael@0: } michael@0: #endif michael@0: #if defined(HAS_COPYROW_MIPS) michael@0: if (TestCpuFlag(kCpuHasMIPS)) { michael@0: CopyRow = CopyRow_MIPS; michael@0: } michael@0: #endif michael@0: michael@0: // Odd height will harmlessly mirror the middle row twice. michael@0: for (y = 0; y < half_height; ++y) { michael@0: ARGBMirrorRow(src, row, width); // Mirror first row into a buffer michael@0: ARGBMirrorRow(src_bot, dst, width); // Mirror last row into first row michael@0: CopyRow(row, dst_bot, width * 4); // Copy first mirrored row into last michael@0: src += src_stride; michael@0: dst += dst_stride; michael@0: src_bot -= src_stride; michael@0: dst_bot -= dst_stride; michael@0: } michael@0: free_aligned_buffer_64(row); michael@0: } michael@0: michael@0: LIBYUV_API michael@0: int ARGBRotate(const uint8* src_argb, int src_stride_argb, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int width, int height, michael@0: enum RotationMode mode) { michael@0: if (!src_argb || width <= 0 || height == 0 || !dst_argb) { michael@0: return -1; michael@0: } michael@0: michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_argb = src_argb + (height - 1) * src_stride_argb; michael@0: src_stride_argb = -src_stride_argb; michael@0: } michael@0: michael@0: switch (mode) { michael@0: case kRotate0: michael@0: // copy frame michael@0: return ARGBCopy(src_argb, src_stride_argb, michael@0: dst_argb, dst_stride_argb, michael@0: width, height); michael@0: case kRotate90: michael@0: ARGBRotate90(src_argb, src_stride_argb, michael@0: dst_argb, dst_stride_argb, michael@0: width, height); michael@0: return 0; michael@0: case kRotate270: michael@0: ARGBRotate270(src_argb, src_stride_argb, michael@0: dst_argb, dst_stride_argb, michael@0: width, height); michael@0: return 0; michael@0: case kRotate180: michael@0: ARGBRotate180(src_argb, src_stride_argb, michael@0: dst_argb, dst_stride_argb, michael@0: width, height); michael@0: return 0; michael@0: default: michael@0: break; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: #ifdef __cplusplus michael@0: } // extern "C" michael@0: } // namespace libyuv michael@0: #endif