michael@0: /* michael@0: * Copyright 2011 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/planar_functions.h" michael@0: michael@0: #include // for memset() michael@0: michael@0: #include "libyuv/cpu_id.h" michael@0: #ifdef HAVE_JPEG michael@0: #include "libyuv/mjpeg_decoder.h" michael@0: #endif 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: // Copy a plane of data michael@0: LIBYUV_API michael@0: void CopyPlane(const uint8* src_y, int src_stride_y, michael@0: uint8* dst_y, int dst_stride_y, michael@0: int width, int height) { michael@0: int y; michael@0: void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C; michael@0: // Coalesce rows. michael@0: if (src_stride_y == width && michael@0: dst_stride_y == width) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_y = dst_stride_y = 0; michael@0: } michael@0: #if defined(HAS_COPYROW_X86) michael@0: if (TestCpuFlag(kCpuHasX86) && IS_ALIGNED(width, 4)) { 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, 32) && michael@0: IS_ALIGNED(src_y, 16) && IS_ALIGNED(src_stride_y, 16) && michael@0: IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 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_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 32)) { michael@0: CopyRow = CopyRow_NEON; 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: // Copy plane michael@0: for (y = 0; y < height; ++y) { michael@0: CopyRow(src_y, dst_y, width); michael@0: src_y += src_stride_y; michael@0: dst_y += dst_stride_y; michael@0: } michael@0: } michael@0: michael@0: // Copy I422. michael@0: LIBYUV_API michael@0: int I422Copy(const uint8* src_y, int src_stride_y, michael@0: const uint8* src_u, int src_stride_u, michael@0: const uint8* src_v, int src_stride_v, michael@0: uint8* dst_y, int dst_stride_y, michael@0: uint8* dst_u, int dst_stride_u, michael@0: uint8* dst_v, int dst_stride_v, michael@0: int width, int height) { michael@0: int halfwidth = (width + 1) >> 1; michael@0: if (!src_y || !src_u || !src_v || michael@0: !dst_y || !dst_u || !dst_v || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_y = src_y + (height - 1) * src_stride_y; michael@0: src_u = src_u + (height - 1) * src_stride_u; michael@0: src_v = src_v + (height - 1) * src_stride_v; michael@0: src_stride_y = -src_stride_y; michael@0: src_stride_u = -src_stride_u; michael@0: src_stride_v = -src_stride_v; michael@0: } michael@0: CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); michael@0: CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height); michael@0: CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height); michael@0: return 0; michael@0: } michael@0: michael@0: // Copy I444. michael@0: LIBYUV_API michael@0: int I444Copy(const uint8* src_y, int src_stride_y, michael@0: const uint8* src_u, int src_stride_u, michael@0: const uint8* src_v, int src_stride_v, michael@0: uint8* dst_y, int dst_stride_y, michael@0: uint8* dst_u, int dst_stride_u, michael@0: uint8* dst_v, int dst_stride_v, michael@0: int width, int height) { michael@0: if (!src_y || !src_u || !src_v || michael@0: !dst_y || !dst_u || !dst_v || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_y = src_y + (height - 1) * src_stride_y; michael@0: src_u = src_u + (height - 1) * src_stride_u; michael@0: src_v = src_v + (height - 1) * src_stride_v; michael@0: src_stride_y = -src_stride_y; michael@0: src_stride_u = -src_stride_u; michael@0: src_stride_v = -src_stride_v; michael@0: } michael@0: michael@0: CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); michael@0: CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height); michael@0: CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height); michael@0: return 0; michael@0: } michael@0: michael@0: // Copy I400. michael@0: LIBYUV_API michael@0: int I400ToI400(const uint8* src_y, int src_stride_y, michael@0: uint8* dst_y, int dst_stride_y, michael@0: int width, int height) { michael@0: if (!src_y || !dst_y || width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_y = src_y + (height - 1) * src_stride_y; michael@0: src_stride_y = -src_stride_y; michael@0: } michael@0: CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); michael@0: return 0; michael@0: } michael@0: michael@0: // Convert I420 to I400. michael@0: LIBYUV_API michael@0: int I420ToI400(const uint8* src_y, int src_stride_y, michael@0: const uint8* src_u, int src_stride_u, michael@0: const uint8* src_v, int src_stride_v, michael@0: uint8* dst_y, int dst_stride_y, michael@0: int width, int height) { michael@0: if (!src_y || !dst_y || width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_y = src_y + (height - 1) * src_stride_y; michael@0: src_stride_y = -src_stride_y; michael@0: } michael@0: CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); michael@0: return 0; michael@0: } michael@0: michael@0: // Mirror a plane of data. michael@0: void MirrorPlane(const uint8* src_y, int src_stride_y, michael@0: uint8* dst_y, int dst_stride_y, michael@0: int width, int height) { michael@0: int y; michael@0: void (*MirrorRow)(const uint8* src, uint8* dst, int width) = MirrorRow_C; michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_y = src_y + (height - 1) * src_stride_y; michael@0: src_stride_y = -src_stride_y; michael@0: } michael@0: #if defined(HAS_MIRRORROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16)) { michael@0: MirrorRow = MirrorRow_NEON; michael@0: } michael@0: #endif michael@0: #if defined(HAS_MIRRORROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16)) { michael@0: MirrorRow = MirrorRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_MIRRORROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 16) && michael@0: IS_ALIGNED(src_y, 16) && IS_ALIGNED(src_stride_y, 16) && michael@0: IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) { michael@0: MirrorRow = MirrorRow_SSSE3; michael@0: } michael@0: #endif michael@0: #if defined(HAS_MIRRORROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 32)) { michael@0: MirrorRow = MirrorRow_AVX2; michael@0: } michael@0: #endif michael@0: michael@0: // Mirror plane michael@0: for (y = 0; y < height; ++y) { michael@0: MirrorRow(src_y, dst_y, width); michael@0: src_y += src_stride_y; michael@0: dst_y += dst_stride_y; michael@0: } michael@0: } michael@0: michael@0: // Convert YUY2 to I422. michael@0: LIBYUV_API michael@0: int YUY2ToI422(const uint8* src_yuy2, int src_stride_yuy2, michael@0: uint8* dst_y, int dst_stride_y, michael@0: uint8* dst_u, int dst_stride_u, michael@0: uint8* dst_v, int dst_stride_v, michael@0: int width, int height) { michael@0: int y; michael@0: void (*YUY2ToUV422Row)(const uint8* src_yuy2, michael@0: uint8* dst_u, uint8* dst_v, int pix) = michael@0: YUY2ToUV422Row_C; michael@0: void (*YUY2ToYRow)(const uint8* src_yuy2, uint8* dst_y, int pix) = michael@0: YUY2ToYRow_C; michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2; michael@0: src_stride_yuy2 = -src_stride_yuy2; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_yuy2 == width * 2 && michael@0: dst_stride_y == width && michael@0: dst_stride_u * 2 == width && michael@0: dst_stride_v * 2 == width) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_yuy2 = dst_stride_y = dst_stride_u = dst_stride_v = 0; michael@0: } michael@0: #if defined(HAS_YUY2TOYROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 16) { michael@0: YUY2ToUV422Row = YUY2ToUV422Row_Any_SSE2; michael@0: YUY2ToYRow = YUY2ToYRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 16)) { michael@0: YUY2ToUV422Row = YUY2ToUV422Row_Unaligned_SSE2; michael@0: YUY2ToYRow = YUY2ToYRow_Unaligned_SSE2; michael@0: if (IS_ALIGNED(src_yuy2, 16) && IS_ALIGNED(src_stride_yuy2, 16)) { michael@0: YUY2ToUV422Row = YUY2ToUV422Row_SSE2; michael@0: if (IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) { michael@0: YUY2ToYRow = YUY2ToYRow_SSE2; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_YUY2TOYROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && width >= 32) { michael@0: YUY2ToUV422Row = YUY2ToUV422Row_Any_AVX2; michael@0: YUY2ToYRow = YUY2ToYRow_Any_AVX2; michael@0: if (IS_ALIGNED(width, 32)) { michael@0: YUY2ToUV422Row = YUY2ToUV422Row_AVX2; michael@0: YUY2ToYRow = YUY2ToYRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_YUY2TOYROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 8) { michael@0: YUY2ToYRow = YUY2ToYRow_Any_NEON; michael@0: if (width >= 16) { michael@0: YUY2ToUV422Row = YUY2ToUV422Row_Any_NEON; michael@0: } michael@0: if (IS_ALIGNED(width, 16)) { michael@0: YUY2ToYRow = YUY2ToYRow_NEON; michael@0: YUY2ToUV422Row = YUY2ToUV422Row_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: YUY2ToUV422Row(src_yuy2, dst_u, dst_v, width); michael@0: YUY2ToYRow(src_yuy2, dst_y, width); michael@0: src_yuy2 += src_stride_yuy2; michael@0: dst_y += dst_stride_y; michael@0: dst_u += dst_stride_u; michael@0: dst_v += dst_stride_v; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Convert UYVY to I422. michael@0: LIBYUV_API michael@0: int UYVYToI422(const uint8* src_uyvy, int src_stride_uyvy, michael@0: uint8* dst_y, int dst_stride_y, michael@0: uint8* dst_u, int dst_stride_u, michael@0: uint8* dst_v, int dst_stride_v, michael@0: int width, int height) { michael@0: int y; michael@0: void (*UYVYToUV422Row)(const uint8* src_uyvy, michael@0: uint8* dst_u, uint8* dst_v, int pix) = michael@0: UYVYToUV422Row_C; michael@0: void (*UYVYToYRow)(const uint8* src_uyvy, michael@0: uint8* dst_y, int pix) = UYVYToYRow_C; michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy; michael@0: src_stride_uyvy = -src_stride_uyvy; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_uyvy == width * 2 && michael@0: dst_stride_y == width && michael@0: dst_stride_u * 2 == width && michael@0: dst_stride_v * 2 == width) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_uyvy = dst_stride_y = dst_stride_u = dst_stride_v = 0; michael@0: } michael@0: #if defined(HAS_UYVYTOYROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 16) { michael@0: UYVYToUV422Row = UYVYToUV422Row_Any_SSE2; michael@0: UYVYToYRow = UYVYToYRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 16)) { michael@0: UYVYToUV422Row = UYVYToUV422Row_Unaligned_SSE2; michael@0: UYVYToYRow = UYVYToYRow_Unaligned_SSE2; michael@0: if (IS_ALIGNED(src_uyvy, 16) && IS_ALIGNED(src_stride_uyvy, 16)) { michael@0: UYVYToUV422Row = UYVYToUV422Row_SSE2; michael@0: if (IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) { michael@0: UYVYToYRow = UYVYToYRow_SSE2; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_UYVYTOYROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && width >= 32) { michael@0: UYVYToUV422Row = UYVYToUV422Row_Any_AVX2; michael@0: UYVYToYRow = UYVYToYRow_Any_AVX2; michael@0: if (IS_ALIGNED(width, 32)) { michael@0: UYVYToUV422Row = UYVYToUV422Row_AVX2; michael@0: UYVYToYRow = UYVYToYRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_UYVYTOYROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 8) { michael@0: UYVYToYRow = UYVYToYRow_Any_NEON; michael@0: if (width >= 16) { michael@0: UYVYToUV422Row = UYVYToUV422Row_Any_NEON; michael@0: } michael@0: if (IS_ALIGNED(width, 16)) { michael@0: UYVYToYRow = UYVYToYRow_NEON; michael@0: UYVYToUV422Row = UYVYToUV422Row_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: UYVYToUV422Row(src_uyvy, dst_u, dst_v, width); michael@0: UYVYToYRow(src_uyvy, dst_y, width); michael@0: src_uyvy += src_stride_uyvy; michael@0: dst_y += dst_stride_y; michael@0: dst_u += dst_stride_u; michael@0: dst_v += dst_stride_v; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Mirror I400 with optional flipping michael@0: LIBYUV_API michael@0: int I400Mirror(const uint8* src_y, int src_stride_y, michael@0: uint8* dst_y, int dst_stride_y, michael@0: int width, int height) { michael@0: if (!src_y || !dst_y || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_y = src_y + (height - 1) * src_stride_y; michael@0: src_stride_y = -src_stride_y; michael@0: } michael@0: michael@0: MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); michael@0: return 0; michael@0: } michael@0: michael@0: // Mirror I420 with optional flipping michael@0: LIBYUV_API michael@0: int I420Mirror(const uint8* src_y, int src_stride_y, michael@0: const uint8* src_u, int src_stride_u, michael@0: const uint8* src_v, int src_stride_v, michael@0: uint8* dst_y, int dst_stride_y, michael@0: uint8* dst_u, int dst_stride_u, michael@0: uint8* dst_v, int dst_stride_v, michael@0: int width, int height) { michael@0: int halfwidth = (width + 1) >> 1; michael@0: int halfheight = (height + 1) >> 1; michael@0: if (!src_y || !src_u || !src_v || !dst_y || !dst_u || !dst_v || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: halfheight = (height + 1) >> 1; michael@0: src_y = src_y + (height - 1) * src_stride_y; michael@0: src_u = src_u + (halfheight - 1) * src_stride_u; michael@0: src_v = src_v + (halfheight - 1) * src_stride_v; michael@0: src_stride_y = -src_stride_y; michael@0: src_stride_u = -src_stride_u; michael@0: src_stride_v = -src_stride_v; michael@0: } michael@0: michael@0: if (dst_y) { michael@0: MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); michael@0: } michael@0: MirrorPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, halfheight); michael@0: MirrorPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, halfheight); michael@0: return 0; michael@0: } michael@0: michael@0: // ARGB mirror. michael@0: LIBYUV_API michael@0: int ARGBMirror(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: int y; michael@0: void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) = michael@0: ARGBMirrorRow_C; michael@0: if (!src_argb || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; 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: #if defined(HAS_ARGBMIRRORROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4) && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 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: michael@0: // Mirror plane michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBMirrorRow(src_argb, dst_argb, width); michael@0: src_argb += src_stride_argb; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Get a blender that optimized for the CPU, alignment and pixel count. michael@0: // As there are 6 blenders to choose from, the caller should try to use michael@0: // the same blend function for all pixels if possible. michael@0: LIBYUV_API michael@0: ARGBBlendRow GetARGBBlend() { michael@0: void (*ARGBBlendRow)(const uint8* src_argb, const uint8* src_argb1, michael@0: uint8* dst_argb, int width) = ARGBBlendRow_C; michael@0: #if defined(HAS_ARGBBLENDROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3)) { michael@0: ARGBBlendRow = ARGBBlendRow_SSSE3; michael@0: return ARGBBlendRow; michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBBLENDROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2)) { michael@0: ARGBBlendRow = ARGBBlendRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBBLENDROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON)) { michael@0: ARGBBlendRow = ARGBBlendRow_NEON; michael@0: } michael@0: #endif michael@0: return ARGBBlendRow; michael@0: } michael@0: michael@0: // Alpha Blend 2 ARGB images and store to destination. michael@0: LIBYUV_API michael@0: int ARGBBlend(const uint8* src_argb0, int src_stride_argb0, michael@0: const uint8* src_argb1, int src_stride_argb1, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ARGBBlendRow)(const uint8* src_argb, const uint8* src_argb1, michael@0: uint8* dst_argb, int width) = GetARGBBlend(); michael@0: if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_argb = dst_argb + (height - 1) * dst_stride_argb; michael@0: dst_stride_argb = -dst_stride_argb; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_argb0 == width * 4 && michael@0: src_stride_argb1 == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; michael@0: } michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBBlendRow(src_argb0, src_argb1, dst_argb, width); michael@0: src_argb0 += src_stride_argb0; michael@0: src_argb1 += src_stride_argb1; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Multiply 2 ARGB images and store to destination. michael@0: LIBYUV_API michael@0: int ARGBMultiply(const uint8* src_argb0, int src_stride_argb0, michael@0: const uint8* src_argb1, int src_stride_argb1, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ARGBMultiplyRow)(const uint8* src0, const uint8* src1, uint8* dst, michael@0: int width) = ARGBMultiplyRow_C; michael@0: if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_argb = dst_argb + (height - 1) * dst_stride_argb; michael@0: dst_stride_argb = -dst_stride_argb; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_argb0 == width * 4 && michael@0: src_stride_argb1 == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBMULTIPLYROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 4) { michael@0: ARGBMultiplyRow = ARGBMultiplyRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: ARGBMultiplyRow = ARGBMultiplyRow_SSE2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBMULTIPLYROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && width >= 8) { michael@0: ARGBMultiplyRow = ARGBMultiplyRow_Any_AVX2; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBMultiplyRow = ARGBMultiplyRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBMULTIPLYROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 8) { michael@0: ARGBMultiplyRow = ARGBMultiplyRow_Any_NEON; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBMultiplyRow = ARGBMultiplyRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: // Multiply plane michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBMultiplyRow(src_argb0, src_argb1, dst_argb, width); michael@0: src_argb0 += src_stride_argb0; michael@0: src_argb1 += src_stride_argb1; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Add 2 ARGB images and store to destination. michael@0: LIBYUV_API michael@0: int ARGBAdd(const uint8* src_argb0, int src_stride_argb0, michael@0: const uint8* src_argb1, int src_stride_argb1, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ARGBAddRow)(const uint8* src0, const uint8* src1, uint8* dst, michael@0: int width) = ARGBAddRow_C; michael@0: if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_argb = dst_argb + (height - 1) * dst_stride_argb; michael@0: dst_stride_argb = -dst_stride_argb; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_argb0 == width * 4 && michael@0: src_stride_argb1 == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBADDROW_SSE2) && defined(_MSC_VER) michael@0: if (TestCpuFlag(kCpuHasSSE2)) { michael@0: ARGBAddRow = ARGBAddRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBADDROW_SSE2) && !defined(_MSC_VER) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 4) { michael@0: ARGBAddRow = ARGBAddRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: ARGBAddRow = ARGBAddRow_SSE2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBADDROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && width >= 8) { michael@0: ARGBAddRow = ARGBAddRow_Any_AVX2; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBAddRow = ARGBAddRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBADDROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 8) { michael@0: ARGBAddRow = ARGBAddRow_Any_NEON; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBAddRow = ARGBAddRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: // Add plane michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBAddRow(src_argb0, src_argb1, dst_argb, width); michael@0: src_argb0 += src_stride_argb0; michael@0: src_argb1 += src_stride_argb1; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Subtract 2 ARGB images and store to destination. michael@0: LIBYUV_API michael@0: int ARGBSubtract(const uint8* src_argb0, int src_stride_argb0, michael@0: const uint8* src_argb1, int src_stride_argb1, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ARGBSubtractRow)(const uint8* src0, const uint8* src1, uint8* dst, michael@0: int width) = ARGBSubtractRow_C; michael@0: if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_argb = dst_argb + (height - 1) * dst_stride_argb; michael@0: dst_stride_argb = -dst_stride_argb; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_argb0 == width * 4 && michael@0: src_stride_argb1 == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBSUBTRACTROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 4) { michael@0: ARGBSubtractRow = ARGBSubtractRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: ARGBSubtractRow = ARGBSubtractRow_SSE2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBSUBTRACTROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && width >= 8) { michael@0: ARGBSubtractRow = ARGBSubtractRow_Any_AVX2; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBSubtractRow = ARGBSubtractRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBSUBTRACTROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 8) { michael@0: ARGBSubtractRow = ARGBSubtractRow_Any_NEON; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBSubtractRow = ARGBSubtractRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: // Subtract plane michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBSubtractRow(src_argb0, src_argb1, dst_argb, width); michael@0: src_argb0 += src_stride_argb0; michael@0: src_argb1 += src_stride_argb1; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Convert I422 to BGRA. michael@0: LIBYUV_API michael@0: int I422ToBGRA(const uint8* src_y, int src_stride_y, michael@0: const uint8* src_u, int src_stride_u, michael@0: const uint8* src_v, int src_stride_v, michael@0: uint8* dst_bgra, int dst_stride_bgra, michael@0: int width, int height) { michael@0: int y; michael@0: void (*I422ToBGRARow)(const uint8* y_buf, michael@0: const uint8* u_buf, michael@0: const uint8* v_buf, michael@0: uint8* rgb_buf, michael@0: int width) = I422ToBGRARow_C; michael@0: if (!src_y || !src_u || !src_v || michael@0: !dst_bgra || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_bgra = dst_bgra + (height - 1) * dst_stride_bgra; michael@0: dst_stride_bgra = -dst_stride_bgra; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_y == width && michael@0: src_stride_u * 2 == width && michael@0: src_stride_v * 2 == width && michael@0: dst_stride_bgra == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_y = src_stride_u = src_stride_v = dst_stride_bgra = 0; michael@0: } michael@0: #if defined(HAS_I422TOBGRAROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON)) { michael@0: I422ToBGRARow = I422ToBGRARow_Any_NEON; michael@0: if (IS_ALIGNED(width, 16)) { michael@0: I422ToBGRARow = I422ToBGRARow_NEON; michael@0: } michael@0: } michael@0: #elif defined(HAS_I422TOBGRAROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) { michael@0: I422ToBGRARow = I422ToBGRARow_Any_SSSE3; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: I422ToBGRARow = I422ToBGRARow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(dst_bgra, 16) && IS_ALIGNED(dst_stride_bgra, 16)) { michael@0: I422ToBGRARow = I422ToBGRARow_SSSE3; michael@0: } michael@0: } michael@0: } michael@0: #elif defined(HAS_I422TOBGRAROW_MIPS_DSPR2) michael@0: if (TestCpuFlag(kCpuHasMIPS_DSPR2) && IS_ALIGNED(width, 4) && michael@0: IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) && michael@0: IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) && michael@0: IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2) && michael@0: IS_ALIGNED(dst_bgra, 4) && IS_ALIGNED(dst_stride_bgra, 4)) { michael@0: I422ToBGRARow = I422ToBGRARow_MIPS_DSPR2; michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: I422ToBGRARow(src_y, src_u, src_v, dst_bgra, width); michael@0: dst_bgra += dst_stride_bgra; michael@0: src_y += src_stride_y; michael@0: src_u += src_stride_u; michael@0: src_v += src_stride_v; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Convert I422 to ABGR. michael@0: LIBYUV_API michael@0: int I422ToABGR(const uint8* src_y, int src_stride_y, michael@0: const uint8* src_u, int src_stride_u, michael@0: const uint8* src_v, int src_stride_v, michael@0: uint8* dst_abgr, int dst_stride_abgr, michael@0: int width, int height) { michael@0: int y; michael@0: void (*I422ToABGRRow)(const uint8* y_buf, michael@0: const uint8* u_buf, michael@0: const uint8* v_buf, michael@0: uint8* rgb_buf, michael@0: int width) = I422ToABGRRow_C; michael@0: if (!src_y || !src_u || !src_v || michael@0: !dst_abgr || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_abgr = dst_abgr + (height - 1) * dst_stride_abgr; michael@0: dst_stride_abgr = -dst_stride_abgr; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_y == width && michael@0: src_stride_u * 2 == width && michael@0: src_stride_v * 2 == width && michael@0: dst_stride_abgr == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_y = src_stride_u = src_stride_v = dst_stride_abgr = 0; michael@0: } michael@0: #if defined(HAS_I422TOABGRROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON)) { michael@0: I422ToABGRRow = I422ToABGRRow_Any_NEON; michael@0: if (IS_ALIGNED(width, 16)) { michael@0: I422ToABGRRow = I422ToABGRRow_NEON; michael@0: } michael@0: } michael@0: #elif defined(HAS_I422TOABGRROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) { michael@0: I422ToABGRRow = I422ToABGRRow_Any_SSSE3; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: I422ToABGRRow = I422ToABGRRow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(dst_abgr, 16) && IS_ALIGNED(dst_stride_abgr, 16)) { michael@0: I422ToABGRRow = I422ToABGRRow_SSSE3; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: I422ToABGRRow(src_y, src_u, src_v, dst_abgr, width); michael@0: dst_abgr += dst_stride_abgr; michael@0: src_y += src_stride_y; michael@0: src_u += src_stride_u; michael@0: src_v += src_stride_v; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Convert I422 to RGBA. michael@0: LIBYUV_API michael@0: int I422ToRGBA(const uint8* src_y, int src_stride_y, michael@0: const uint8* src_u, int src_stride_u, michael@0: const uint8* src_v, int src_stride_v, michael@0: uint8* dst_rgba, int dst_stride_rgba, michael@0: int width, int height) { michael@0: int y; michael@0: void (*I422ToRGBARow)(const uint8* y_buf, michael@0: const uint8* u_buf, michael@0: const uint8* v_buf, michael@0: uint8* rgb_buf, michael@0: int width) = I422ToRGBARow_C; michael@0: if (!src_y || !src_u || !src_v || michael@0: !dst_rgba || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_rgba = dst_rgba + (height - 1) * dst_stride_rgba; michael@0: dst_stride_rgba = -dst_stride_rgba; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_y == width && michael@0: src_stride_u * 2 == width && michael@0: src_stride_v * 2 == width && michael@0: dst_stride_rgba == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_y = src_stride_u = src_stride_v = dst_stride_rgba = 0; michael@0: } michael@0: #if defined(HAS_I422TORGBAROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON)) { michael@0: I422ToRGBARow = I422ToRGBARow_Any_NEON; michael@0: if (IS_ALIGNED(width, 16)) { michael@0: I422ToRGBARow = I422ToRGBARow_NEON; michael@0: } michael@0: } michael@0: #elif defined(HAS_I422TORGBAROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) { michael@0: I422ToRGBARow = I422ToRGBARow_Any_SSSE3; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: I422ToRGBARow = I422ToRGBARow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(dst_rgba, 16) && IS_ALIGNED(dst_stride_rgba, 16)) { michael@0: I422ToRGBARow = I422ToRGBARow_SSSE3; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: I422ToRGBARow(src_y, src_u, src_v, dst_rgba, width); michael@0: dst_rgba += dst_stride_rgba; michael@0: src_y += src_stride_y; michael@0: src_u += src_stride_u; michael@0: src_v += src_stride_v; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Convert NV12 to RGB565. michael@0: LIBYUV_API michael@0: int NV12ToRGB565(const uint8* src_y, int src_stride_y, michael@0: const uint8* src_uv, int src_stride_uv, michael@0: uint8* dst_rgb565, int dst_stride_rgb565, michael@0: int width, int height) { michael@0: int y; michael@0: void (*NV12ToRGB565Row)(const uint8* y_buf, michael@0: const uint8* uv_buf, michael@0: uint8* rgb_buf, michael@0: int width) = NV12ToRGB565Row_C; michael@0: if (!src_y || !src_uv || !dst_rgb565 || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565; michael@0: dst_stride_rgb565 = -dst_stride_rgb565; michael@0: } michael@0: #if defined(HAS_NV12TORGB565ROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) { michael@0: NV12ToRGB565Row = NV12ToRGB565Row_Any_SSSE3; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: NV12ToRGB565Row = NV12ToRGB565Row_SSSE3; michael@0: } michael@0: } michael@0: #elif defined(HAS_NV12TORGB565ROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 8) { michael@0: NV12ToRGB565Row = NV12ToRGB565Row_Any_NEON; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: NV12ToRGB565Row = NV12ToRGB565Row_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: NV12ToRGB565Row(src_y, src_uv, dst_rgb565, width); michael@0: dst_rgb565 += dst_stride_rgb565; michael@0: src_y += src_stride_y; michael@0: if (y & 1) { michael@0: src_uv += src_stride_uv; michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Convert NV21 to RGB565. michael@0: LIBYUV_API michael@0: int NV21ToRGB565(const uint8* src_y, int src_stride_y, michael@0: const uint8* src_vu, int src_stride_vu, michael@0: uint8* dst_rgb565, int dst_stride_rgb565, michael@0: int width, int height) { michael@0: int y; michael@0: void (*NV21ToRGB565Row)(const uint8* y_buf, michael@0: const uint8* src_vu, michael@0: uint8* rgb_buf, michael@0: int width) = NV21ToRGB565Row_C; michael@0: if (!src_y || !src_vu || !dst_rgb565 || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565; michael@0: dst_stride_rgb565 = -dst_stride_rgb565; michael@0: } michael@0: #if defined(HAS_NV21TORGB565ROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) { michael@0: NV21ToRGB565Row = NV21ToRGB565Row_Any_SSSE3; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: NV21ToRGB565Row = NV21ToRGB565Row_SSSE3; michael@0: } michael@0: } michael@0: #elif defined(HAS_NV21TORGB565ROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 8) { michael@0: NV21ToRGB565Row = NV21ToRGB565Row_Any_NEON; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: NV21ToRGB565Row = NV21ToRGB565Row_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: NV21ToRGB565Row(src_y, src_vu, dst_rgb565, width); michael@0: dst_rgb565 += dst_stride_rgb565; michael@0: src_y += src_stride_y; michael@0: if (y & 1) { michael@0: src_vu += src_stride_vu; michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: LIBYUV_API michael@0: void SetPlane(uint8* dst_y, int dst_stride_y, michael@0: int width, int height, michael@0: uint32 value) { michael@0: int y; michael@0: uint32 v32 = value | (value << 8) | (value << 16) | (value << 24); michael@0: void (*SetRow)(uint8* dst, uint32 value, int pix) = SetRow_C; michael@0: // Coalesce rows. michael@0: if (dst_stride_y == width) { michael@0: width *= height; michael@0: height = 1; michael@0: dst_stride_y = 0; michael@0: } michael@0: #if defined(HAS_SETROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && michael@0: IS_ALIGNED(width, 16) && michael@0: IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) { michael@0: SetRow = SetRow_NEON; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SETROW_X86) michael@0: if (TestCpuFlag(kCpuHasX86) && IS_ALIGNED(width, 4)) { michael@0: SetRow = SetRow_X86; michael@0: } michael@0: #endif michael@0: michael@0: // Set plane michael@0: for (y = 0; y < height; ++y) { michael@0: SetRow(dst_y, v32, width); michael@0: dst_y += dst_stride_y; michael@0: } michael@0: } michael@0: michael@0: // Draw a rectangle into I420 michael@0: LIBYUV_API michael@0: int I420Rect(uint8* dst_y, int dst_stride_y, michael@0: uint8* dst_u, int dst_stride_u, michael@0: uint8* dst_v, int dst_stride_v, michael@0: int x, int y, michael@0: int width, int height, michael@0: int value_y, int value_u, int value_v) { michael@0: int halfwidth = (width + 1) >> 1; michael@0: int halfheight = (height + 1) >> 1; michael@0: uint8* start_y = dst_y + y * dst_stride_y + x; michael@0: uint8* start_u = dst_u + (y / 2) * dst_stride_u + (x / 2); michael@0: uint8* start_v = dst_v + (y / 2) * dst_stride_v + (x / 2); michael@0: if (!dst_y || !dst_u || !dst_v || michael@0: width <= 0 || height <= 0 || michael@0: x < 0 || y < 0 || michael@0: value_y < 0 || value_y > 255 || michael@0: value_u < 0 || value_u > 255 || michael@0: value_v < 0 || value_v > 255) { michael@0: return -1; michael@0: } michael@0: michael@0: SetPlane(start_y, dst_stride_y, width, height, value_y); michael@0: SetPlane(start_u, dst_stride_u, halfwidth, halfheight, value_u); michael@0: SetPlane(start_v, dst_stride_v, halfwidth, halfheight, value_v); michael@0: return 0; michael@0: } michael@0: michael@0: // Draw a rectangle into ARGB michael@0: LIBYUV_API michael@0: int ARGBRect(uint8* dst_argb, int dst_stride_argb, michael@0: int dst_x, int dst_y, michael@0: int width, int height, michael@0: uint32 value) { michael@0: if (!dst_argb || michael@0: width <= 0 || height <= 0 || michael@0: dst_x < 0 || dst_y < 0) { michael@0: return -1; michael@0: } michael@0: dst_argb += dst_y * dst_stride_argb + dst_x * 4; michael@0: // Coalesce rows. michael@0: if (dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_SETROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: ARGBSetRows_NEON(dst_argb, value, width, dst_stride_argb, height); michael@0: return 0; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SETROW_X86) michael@0: if (TestCpuFlag(kCpuHasX86)) { michael@0: ARGBSetRows_X86(dst_argb, value, width, dst_stride_argb, height); michael@0: return 0; michael@0: } michael@0: #endif michael@0: ARGBSetRows_C(dst_argb, value, width, dst_stride_argb, height); michael@0: return 0; michael@0: } michael@0: michael@0: // Convert unattentuated ARGB to preattenuated ARGB. michael@0: // An unattenutated ARGB alpha blend uses the formula michael@0: // p = a * f + (1 - a) * b michael@0: // where michael@0: // p is output pixel michael@0: // f is foreground pixel michael@0: // b is background pixel michael@0: // a is alpha value from foreground pixel michael@0: // An preattenutated ARGB alpha blend uses the formula michael@0: // p = f + (1 - a) * b michael@0: // where michael@0: // f is foreground pixel premultiplied by alpha michael@0: michael@0: LIBYUV_API michael@0: int ARGBAttenuate(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: int y; michael@0: void (*ARGBAttenuateRow)(const uint8* src_argb, uint8* dst_argb, michael@0: int width) = ARGBAttenuateRow_C; michael@0: if (!src_argb || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } 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: // Coalesce rows. michael@0: if (src_stride_argb == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBATTENUATEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 4 && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: ARGBAttenuateRow = ARGBAttenuateRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: ARGBAttenuateRow = ARGBAttenuateRow_SSE2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBATTENUATEROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && width >= 4) { michael@0: ARGBAttenuateRow = ARGBAttenuateRow_Any_SSSE3; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: ARGBAttenuateRow = ARGBAttenuateRow_SSSE3; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBATTENUATEROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && width >= 8) { michael@0: ARGBAttenuateRow = ARGBAttenuateRow_Any_AVX2; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBAttenuateRow = ARGBAttenuateRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBATTENUATEROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 8) { michael@0: ARGBAttenuateRow = ARGBAttenuateRow_Any_NEON; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBAttenuateRow = ARGBAttenuateRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBAttenuateRow(src_argb, dst_argb, width); michael@0: src_argb += src_stride_argb; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Convert preattentuated ARGB to unattenuated ARGB. michael@0: LIBYUV_API michael@0: int ARGBUnattenuate(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: int y; michael@0: void (*ARGBUnattenuateRow)(const uint8* src_argb, uint8* dst_argb, michael@0: int width) = ARGBUnattenuateRow_C; michael@0: if (!src_argb || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } 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: // Coalesce rows. michael@0: if (src_stride_argb == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBUNATTENUATEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 4) { michael@0: ARGBUnattenuateRow = ARGBUnattenuateRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: ARGBUnattenuateRow = ARGBUnattenuateRow_SSE2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBUNATTENUATEROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && width >= 8) { michael@0: ARGBUnattenuateRow = ARGBUnattenuateRow_Any_AVX2; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBUnattenuateRow = ARGBUnattenuateRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: // TODO(fbarchard): Neon version. michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBUnattenuateRow(src_argb, dst_argb, width); michael@0: src_argb += src_stride_argb; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Convert ARGB to Grayed ARGB. michael@0: LIBYUV_API michael@0: int ARGBGrayTo(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: int y; michael@0: void (*ARGBGrayRow)(const uint8* src_argb, uint8* dst_argb, michael@0: int width) = ARGBGrayRow_C; michael@0: if (!src_argb || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } 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: // Coalesce rows. michael@0: if (src_stride_argb == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBGRAYROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8) && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: ARGBGrayRow = ARGBGrayRow_SSSE3; michael@0: } michael@0: #elif defined(HAS_ARGBGRAYROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { michael@0: ARGBGrayRow = ARGBGrayRow_NEON; michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBGrayRow(src_argb, dst_argb, width); michael@0: src_argb += src_stride_argb; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Make a rectangle of ARGB gray scale. michael@0: LIBYUV_API michael@0: int ARGBGray(uint8* dst_argb, int dst_stride_argb, michael@0: int dst_x, int dst_y, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ARGBGrayRow)(const uint8* src_argb, uint8* dst_argb, michael@0: int width) = ARGBGrayRow_C; michael@0: uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; michael@0: if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) { michael@0: return -1; michael@0: } michael@0: // Coalesce rows. michael@0: if (dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBGRAYROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: ARGBGrayRow = ARGBGrayRow_SSSE3; michael@0: } michael@0: #elif defined(HAS_ARGBGRAYROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { michael@0: ARGBGrayRow = ARGBGrayRow_NEON; michael@0: } michael@0: #endif michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBGrayRow(dst, dst, width); michael@0: dst += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Make a rectangle of ARGB Sepia tone. michael@0: LIBYUV_API michael@0: int ARGBSepia(uint8* dst_argb, int dst_stride_argb, michael@0: int dst_x, int dst_y, int width, int height) { michael@0: int y; michael@0: void (*ARGBSepiaRow)(uint8* dst_argb, int width) = ARGBSepiaRow_C; michael@0: uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; michael@0: if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) { michael@0: return -1; michael@0: } michael@0: // Coalesce rows. michael@0: if (dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBSEPIAROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: ARGBSepiaRow = ARGBSepiaRow_SSSE3; michael@0: } michael@0: #elif defined(HAS_ARGBSEPIAROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { michael@0: ARGBSepiaRow = ARGBSepiaRow_NEON; michael@0: } michael@0: #endif michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBSepiaRow(dst, width); michael@0: dst += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Apply a 4x4 matrix to each ARGB pixel. michael@0: // Note: Normally for shading, but can be used to swizzle or invert. michael@0: LIBYUV_API michael@0: int ARGBColorMatrix(const uint8* src_argb, int src_stride_argb, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: const int8* matrix_argb, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ARGBColorMatrixRow)(const uint8* src_argb, uint8* dst_argb, michael@0: const int8* matrix_argb, int width) = ARGBColorMatrixRow_C; michael@0: if (!src_argb || !dst_argb || !matrix_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } 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: // Coalesce rows. michael@0: if (src_stride_argb == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBCOLORMATRIXROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: ARGBColorMatrixRow = ARGBColorMatrixRow_SSSE3; michael@0: } michael@0: #elif defined(HAS_ARGBCOLORMATRIXROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { michael@0: ARGBColorMatrixRow = ARGBColorMatrixRow_NEON; michael@0: } michael@0: #endif michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBColorMatrixRow(src_argb, dst_argb, matrix_argb, width); michael@0: src_argb += src_stride_argb; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Apply a 4x3 matrix to each ARGB pixel. michael@0: // Deprecated. michael@0: LIBYUV_API michael@0: int RGBColorMatrix(uint8* dst_argb, int dst_stride_argb, michael@0: const int8* matrix_rgb, michael@0: int dst_x, int dst_y, int width, int height) { michael@0: SIMD_ALIGNED(int8 matrix_argb[16]); michael@0: uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; michael@0: if (!dst_argb || !matrix_rgb || width <= 0 || height <= 0 || michael@0: dst_x < 0 || dst_y < 0) { michael@0: return -1; michael@0: } michael@0: michael@0: // Convert 4x3 7 bit matrix to 4x4 6 bit matrix. michael@0: matrix_argb[0] = matrix_rgb[0] / 2; michael@0: matrix_argb[1] = matrix_rgb[1] / 2; michael@0: matrix_argb[2] = matrix_rgb[2] / 2; michael@0: matrix_argb[3] = matrix_rgb[3] / 2; michael@0: matrix_argb[4] = matrix_rgb[4] / 2; michael@0: matrix_argb[5] = matrix_rgb[5] / 2; michael@0: matrix_argb[6] = matrix_rgb[6] / 2; michael@0: matrix_argb[7] = matrix_rgb[7] / 2; michael@0: matrix_argb[8] = matrix_rgb[8] / 2; michael@0: matrix_argb[9] = matrix_rgb[9] / 2; michael@0: matrix_argb[10] = matrix_rgb[10] / 2; michael@0: matrix_argb[11] = matrix_rgb[11] / 2; michael@0: matrix_argb[14] = matrix_argb[13] = matrix_argb[12] = 0; michael@0: matrix_argb[15] = 64; // 1.0 michael@0: michael@0: return ARGBColorMatrix((const uint8*)(dst), dst_stride_argb, michael@0: dst, dst_stride_argb, michael@0: &matrix_argb[0], width, height); michael@0: } michael@0: michael@0: // Apply a color table each ARGB pixel. michael@0: // Table contains 256 ARGB values. michael@0: LIBYUV_API michael@0: int ARGBColorTable(uint8* dst_argb, int dst_stride_argb, michael@0: const uint8* table_argb, michael@0: int dst_x, int dst_y, int width, int height) { michael@0: int y; michael@0: void (*ARGBColorTableRow)(uint8* dst_argb, const uint8* table_argb, michael@0: int width) = ARGBColorTableRow_C; michael@0: uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; michael@0: if (!dst_argb || !table_argb || width <= 0 || height <= 0 || michael@0: dst_x < 0 || dst_y < 0) { michael@0: return -1; michael@0: } michael@0: // Coalesce rows. michael@0: if (dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBCOLORTABLEROW_X86) michael@0: if (TestCpuFlag(kCpuHasX86)) { michael@0: ARGBColorTableRow = ARGBColorTableRow_X86; michael@0: } michael@0: #endif michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBColorTableRow(dst, table_argb, width); michael@0: dst += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Apply a color table each ARGB pixel but preserve destination alpha. michael@0: // Table contains 256 ARGB values. michael@0: LIBYUV_API michael@0: int RGBColorTable(uint8* dst_argb, int dst_stride_argb, michael@0: const uint8* table_argb, michael@0: int dst_x, int dst_y, int width, int height) { michael@0: int y; michael@0: void (*RGBColorTableRow)(uint8* dst_argb, const uint8* table_argb, michael@0: int width) = RGBColorTableRow_C; michael@0: uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; michael@0: if (!dst_argb || !table_argb || width <= 0 || height <= 0 || michael@0: dst_x < 0 || dst_y < 0) { michael@0: return -1; michael@0: } michael@0: // Coalesce rows. michael@0: if (dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_RGBCOLORTABLEROW_X86) michael@0: if (TestCpuFlag(kCpuHasX86)) { michael@0: RGBColorTableRow = RGBColorTableRow_X86; michael@0: } michael@0: #endif michael@0: for (y = 0; y < height; ++y) { michael@0: RGBColorTableRow(dst, table_argb, width); michael@0: dst += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // ARGBQuantize is used to posterize art. michael@0: // e.g. rgb / qvalue * qvalue + qvalue / 2 michael@0: // But the low levels implement efficiently with 3 parameters, and could be michael@0: // used for other high level operations. michael@0: // dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset; michael@0: // where scale is 1 / interval_size as a fixed point value. michael@0: // The divide is replaces with a multiply by reciprocal fixed point multiply. michael@0: // Caveat - although SSE2 saturates, the C function does not and should be used michael@0: // with care if doing anything but quantization. michael@0: LIBYUV_API michael@0: int ARGBQuantize(uint8* dst_argb, int dst_stride_argb, michael@0: int scale, int interval_size, int interval_offset, michael@0: int dst_x, int dst_y, int width, int height) { michael@0: int y; michael@0: void (*ARGBQuantizeRow)(uint8* dst_argb, int scale, int interval_size, michael@0: int interval_offset, int width) = ARGBQuantizeRow_C; michael@0: uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; michael@0: if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0 || michael@0: interval_size < 1 || interval_size > 255) { michael@0: return -1; michael@0: } michael@0: // Coalesce rows. michael@0: if (dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBQUANTIZEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: ARGBQuantizeRow = ARGBQuantizeRow_SSE2; michael@0: } michael@0: #elif defined(HAS_ARGBQUANTIZEROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { michael@0: ARGBQuantizeRow = ARGBQuantizeRow_NEON; michael@0: } michael@0: #endif michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBQuantizeRow(dst, scale, interval_size, interval_offset, width); michael@0: dst += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Computes table of cumulative sum for image where the value is the sum michael@0: // of all values above and to the left of the entry. Used by ARGBBlur. michael@0: LIBYUV_API michael@0: int ARGBComputeCumulativeSum(const uint8* src_argb, int src_stride_argb, michael@0: int32* dst_cumsum, int dst_stride32_cumsum, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ComputeCumulativeSumRow)(const uint8* row, int32* cumsum, michael@0: const int32* previous_cumsum, int width) = ComputeCumulativeSumRow_C; michael@0: int32* previous_cumsum = dst_cumsum; michael@0: if (!dst_cumsum || !src_argb || width <= 0 || height <= 0) { michael@0: return -1; michael@0: } michael@0: #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2)) { michael@0: ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2; michael@0: } michael@0: #endif michael@0: memset(dst_cumsum, 0, width * sizeof(dst_cumsum[0]) * 4); // 4 int per pixel. michael@0: for (y = 0; y < height; ++y) { michael@0: ComputeCumulativeSumRow(src_argb, dst_cumsum, previous_cumsum, width); michael@0: previous_cumsum = dst_cumsum; michael@0: dst_cumsum += dst_stride32_cumsum; michael@0: src_argb += src_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Blur ARGB image. michael@0: // Caller should allocate CumulativeSum table of width * height * 16 bytes michael@0: // aligned to 16 byte boundary. height can be radius * 2 + 2 to save memory michael@0: // as the buffer is treated as circular. michael@0: LIBYUV_API michael@0: int ARGBBlur(const uint8* src_argb, int src_stride_argb, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int32* dst_cumsum, int dst_stride32_cumsum, michael@0: int width, int height, int radius) { michael@0: int y; michael@0: void (*ComputeCumulativeSumRow)(const uint8 *row, int32 *cumsum, michael@0: const int32* previous_cumsum, int width) = ComputeCumulativeSumRow_C; michael@0: void (*CumulativeSumToAverageRow)(const int32* topleft, const int32* botleft, michael@0: int width, int area, uint8* dst, int count) = CumulativeSumToAverageRow_C; michael@0: int32* cumsum_bot_row; michael@0: int32* max_cumsum_bot_row; michael@0: int32* cumsum_top_row; michael@0: michael@0: if (!src_argb || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } 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: if (radius > height) { michael@0: radius = height; michael@0: } michael@0: if (radius > (width / 2 - 1)) { michael@0: radius = width / 2 - 1; michael@0: } michael@0: if (radius <= 0) { michael@0: return -1; michael@0: } michael@0: #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2)) { michael@0: ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2; michael@0: CumulativeSumToAverageRow = CumulativeSumToAverageRow_SSE2; michael@0: } michael@0: #endif michael@0: // Compute enough CumulativeSum for first row to be blurred. After this michael@0: // one row of CumulativeSum is updated at a time. michael@0: ARGBComputeCumulativeSum(src_argb, src_stride_argb, michael@0: dst_cumsum, dst_stride32_cumsum, michael@0: width, radius); michael@0: michael@0: src_argb = src_argb + radius * src_stride_argb; michael@0: cumsum_bot_row = &dst_cumsum[(radius - 1) * dst_stride32_cumsum]; michael@0: michael@0: max_cumsum_bot_row = &dst_cumsum[(radius * 2 + 2) * dst_stride32_cumsum]; michael@0: cumsum_top_row = &dst_cumsum[0]; michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: int top_y = ((y - radius - 1) >= 0) ? (y - radius - 1) : 0; michael@0: int bot_y = ((y + radius) < height) ? (y + radius) : (height - 1); michael@0: int area = radius * (bot_y - top_y); michael@0: int boxwidth = radius * 4; michael@0: int x; michael@0: int n; michael@0: michael@0: // Increment cumsum_top_row pointer with circular buffer wrap around. michael@0: if (top_y) { michael@0: cumsum_top_row += dst_stride32_cumsum; michael@0: if (cumsum_top_row >= max_cumsum_bot_row) { michael@0: cumsum_top_row = dst_cumsum; michael@0: } michael@0: } michael@0: // Increment cumsum_bot_row pointer with circular buffer wrap around and michael@0: // then fill in a row of CumulativeSum. michael@0: if ((y + radius) < height) { michael@0: const int32* prev_cumsum_bot_row = cumsum_bot_row; michael@0: cumsum_bot_row += dst_stride32_cumsum; michael@0: if (cumsum_bot_row >= max_cumsum_bot_row) { michael@0: cumsum_bot_row = dst_cumsum; michael@0: } michael@0: ComputeCumulativeSumRow(src_argb, cumsum_bot_row, prev_cumsum_bot_row, michael@0: width); michael@0: src_argb += src_stride_argb; michael@0: } michael@0: michael@0: // Left clipped. michael@0: for (x = 0; x < radius + 1; ++x) { michael@0: CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, michael@0: boxwidth, area, &dst_argb[x * 4], 1); michael@0: area += (bot_y - top_y); michael@0: boxwidth += 4; michael@0: } michael@0: michael@0: // Middle unclipped. michael@0: n = (width - 1) - radius - x + 1; michael@0: CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, michael@0: boxwidth, area, &dst_argb[x * 4], n); michael@0: michael@0: // Right clipped. michael@0: for (x += n; x <= width - 1; ++x) { michael@0: area -= (bot_y - top_y); michael@0: boxwidth -= 4; michael@0: CumulativeSumToAverageRow(cumsum_top_row + (x - radius - 1) * 4, michael@0: cumsum_bot_row + (x - radius - 1) * 4, michael@0: boxwidth, area, &dst_argb[x * 4], 1); michael@0: } michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Multiply ARGB image by a specified ARGB value. michael@0: LIBYUV_API michael@0: int ARGBShade(const uint8* src_argb, int src_stride_argb, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int width, int height, uint32 value) { michael@0: int y; michael@0: void (*ARGBShadeRow)(const uint8* src_argb, uint8* dst_argb, michael@0: int width, uint32 value) = ARGBShadeRow_C; michael@0: if (!src_argb || !dst_argb || width <= 0 || height == 0 || value == 0u) { michael@0: return -1; michael@0: } 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: // Coalesce rows. michael@0: if (src_stride_argb == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBSHADEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4) && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: ARGBShadeRow = ARGBShadeRow_SSE2; michael@0: } michael@0: #elif defined(HAS_ARGBSHADEROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { michael@0: ARGBShadeRow = ARGBShadeRow_NEON; michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBShadeRow(src_argb, dst_argb, width, value); michael@0: src_argb += src_stride_argb; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Interpolate 2 ARGB images by specified amount (0 to 255). michael@0: LIBYUV_API michael@0: int ARGBInterpolate(const uint8* src_argb0, int src_stride_argb0, michael@0: const uint8* src_argb1, int src_stride_argb1, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int width, int height, int interpolation) { michael@0: int y; michael@0: void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr, michael@0: ptrdiff_t src_stride, int dst_width, michael@0: int source_y_fraction) = InterpolateRow_C; michael@0: if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: dst_argb = dst_argb + (height - 1) * dst_stride_argb; michael@0: dst_stride_argb = -dst_stride_argb; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_argb0 == width * 4 && michael@0: src_stride_argb1 == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_INTERPOLATEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSE2; michael@0: if (IS_ALIGNED(src_argb0, 16) && IS_ALIGNED(src_stride_argb0, 16) && michael@0: IS_ALIGNED(src_argb1, 16) && IS_ALIGNED(src_stride_argb1, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: InterpolateRow = InterpolateRow_SSE2; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_INTERPOLATEROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_SSSE3; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(src_argb0, 16) && IS_ALIGNED(src_stride_argb0, 16) && michael@0: IS_ALIGNED(src_argb1, 16) && IS_ALIGNED(src_stride_argb1, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: InterpolateRow = InterpolateRow_SSSE3; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_INTERPOLATEROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && width >= 8) { michael@0: InterpolateRow = InterpolateRow_Any_AVX2; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: InterpolateRow = InterpolateRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_INTERPOLATEROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_NEON; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: InterpolateRow = InterpolateRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_INTERPOLATEROWS_MIPS_DSPR2) michael@0: if (TestCpuFlag(kCpuHasMIPS_DSPR2) && width >= 1 && michael@0: IS_ALIGNED(src_argb0, 4) && IS_ALIGNED(src_stride_argb0, 4) && michael@0: IS_ALIGNED(src_argb1, 4) && IS_ALIGNED(src_stride_argb1, 4) && michael@0: IS_ALIGNED(dst_argb, 4) && IS_ALIGNED(dst_stride_argb, 4)) { michael@0: ScaleARGBFilterRows = InterpolateRow_MIPS_DSPR2; michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: InterpolateRow(dst_argb, src_argb0, src_argb1 - src_argb0, michael@0: width * 4, interpolation); michael@0: src_argb0 += src_stride_argb0; michael@0: src_argb1 += src_stride_argb1; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Shuffle ARGB channel order. e.g. BGRA to ARGB. michael@0: LIBYUV_API michael@0: int ARGBShuffle(const uint8* src_bgra, int src_stride_bgra, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: const uint8* shuffler, int width, int height) { michael@0: int y; michael@0: void (*ARGBShuffleRow)(const uint8* src_bgra, uint8* dst_argb, michael@0: const uint8* shuffler, int pix) = ARGBShuffleRow_C; michael@0: if (!src_bgra || !dst_argb || michael@0: width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_bgra = src_bgra + (height - 1) * src_stride_bgra; michael@0: src_stride_bgra = -src_stride_bgra; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_bgra == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_bgra = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBSHUFFLEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 4) { michael@0: ARGBShuffleRow = ARGBShuffleRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: ARGBShuffleRow = ARGBShuffleRow_SSE2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBSHUFFLEROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) { michael@0: ARGBShuffleRow = ARGBShuffleRow_Any_SSSE3; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBShuffleRow = ARGBShuffleRow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(src_bgra, 16) && IS_ALIGNED(src_stride_bgra, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: ARGBShuffleRow = ARGBShuffleRow_SSSE3; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBSHUFFLEROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && width >= 16) { michael@0: ARGBShuffleRow = ARGBShuffleRow_Any_AVX2; michael@0: if (IS_ALIGNED(width, 16)) { michael@0: ARGBShuffleRow = ARGBShuffleRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBSHUFFLEROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 4) { michael@0: ARGBShuffleRow = ARGBShuffleRow_Any_NEON; michael@0: if (IS_ALIGNED(width, 4)) { michael@0: ARGBShuffleRow = ARGBShuffleRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBShuffleRow(src_bgra, dst_argb, shuffler, width); michael@0: src_bgra += src_stride_bgra; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Sobel ARGB effect. michael@0: static int ARGBSobelize(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: void (*SobelRow)(const uint8* src_sobelx, michael@0: const uint8* src_sobely, michael@0: uint8* dst, int width)) { michael@0: int y; michael@0: void (*ARGBToBayerRow)(const uint8* src_argb, uint8* dst_bayer, michael@0: uint32 selector, int pix) = ARGBToBayerGGRow_C; michael@0: void (*SobelYRow)(const uint8* src_y0, const uint8* src_y1, michael@0: uint8* dst_sobely, int width) = SobelYRow_C; michael@0: void (*SobelXRow)(const uint8* src_y0, const uint8* src_y1, michael@0: const uint8* src_y2, uint8* dst_sobely, int width) = michael@0: SobelXRow_C; michael@0: const int kEdge = 16; // Extra pixels at start of row for extrude/align. michael@0: if (!src_argb || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; 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: // ARGBToBayer used to select G channel from ARGB. michael@0: #if defined(HAS_ARGBTOBAYERGGROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && width >= 8 && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16)) { michael@0: ARGBToBayerRow = ARGBToBayerGGRow_Any_SSE2; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBToBayerRow = ARGBToBayerGGRow_SSE2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBTOBAYERROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && width >= 8 && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16)) { michael@0: ARGBToBayerRow = ARGBToBayerRow_Any_SSSE3; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBToBayerRow = ARGBToBayerRow_SSSE3; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBTOBAYERGGROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && width >= 8) { michael@0: ARGBToBayerRow = ARGBToBayerGGRow_Any_NEON; michael@0: if (IS_ALIGNED(width, 8)) { michael@0: ARGBToBayerRow = ARGBToBayerGGRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_SOBELYROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2)) { michael@0: SobelYRow = SobelYRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SOBELYROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON)) { michael@0: SobelYRow = SobelYRow_NEON; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SOBELXROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2)) { michael@0: SobelXRow = SobelXRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SOBELXROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON)) { michael@0: SobelXRow = SobelXRow_NEON; michael@0: } michael@0: #endif michael@0: { michael@0: // 3 rows with edges before/after. michael@0: const int kRowSize = (width + kEdge + 15) & ~15; michael@0: align_buffer_64(rows, kRowSize * 2 + (kEdge + kRowSize * 3 + kEdge)); michael@0: uint8* row_sobelx = rows; michael@0: uint8* row_sobely = rows + kRowSize; michael@0: uint8* row_y = rows + kRowSize * 2; michael@0: michael@0: // Convert first row. michael@0: uint8* row_y0 = row_y + kEdge; michael@0: uint8* row_y1 = row_y0 + kRowSize; michael@0: uint8* row_y2 = row_y1 + kRowSize; michael@0: ARGBToBayerRow(src_argb, row_y0, 0x0d090501, width); michael@0: row_y0[-1] = row_y0[0]; michael@0: memset(row_y0 + width, row_y0[width - 1], 16); // Extrude 16 for valgrind. michael@0: ARGBToBayerRow(src_argb, row_y1, 0x0d090501, width); michael@0: row_y1[-1] = row_y1[0]; michael@0: memset(row_y1 + width, row_y1[width - 1], 16); michael@0: memset(row_y2 + width, 0, 16); michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: // Convert next row of ARGB to Y. michael@0: if (y < (height - 1)) { michael@0: src_argb += src_stride_argb; michael@0: } michael@0: ARGBToBayerRow(src_argb, row_y2, 0x0d090501, width); michael@0: row_y2[-1] = row_y2[0]; michael@0: row_y2[width] = row_y2[width - 1]; michael@0: michael@0: SobelXRow(row_y0 - 1, row_y1 - 1, row_y2 - 1, row_sobelx, width); michael@0: SobelYRow(row_y0 - 1, row_y2 - 1, row_sobely, width); michael@0: SobelRow(row_sobelx, row_sobely, dst_argb, width); michael@0: michael@0: // Cycle thru circular queue of 3 row_y buffers. michael@0: { michael@0: uint8* row_yt = row_y0; michael@0: row_y0 = row_y1; michael@0: row_y1 = row_y2; michael@0: row_y2 = row_yt; michael@0: } michael@0: michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: free_aligned_buffer_64(rows); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Sobel ARGB effect. michael@0: LIBYUV_API michael@0: int ARGBSobel(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: void (*SobelRow)(const uint8* src_sobelx, const uint8* src_sobely, michael@0: uint8* dst_argb, int width) = SobelRow_C; michael@0: #if defined(HAS_SOBELROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: SobelRow = SobelRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SOBELROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { michael@0: SobelRow = SobelRow_NEON; michael@0: } michael@0: #endif michael@0: return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb, michael@0: width, height, SobelRow); michael@0: } michael@0: michael@0: // Sobel ARGB effect with planar output. michael@0: LIBYUV_API michael@0: int ARGBSobelToPlane(const uint8* src_argb, int src_stride_argb, michael@0: uint8* dst_y, int dst_stride_y, michael@0: int width, int height) { michael@0: void (*SobelToPlaneRow)(const uint8* src_sobelx, const uint8* src_sobely, michael@0: uint8* dst_, int width) = SobelToPlaneRow_C; michael@0: #if defined(HAS_SOBELTOPLANEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16) && michael@0: IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) { michael@0: SobelToPlaneRow = SobelToPlaneRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SOBELTOPLANEROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16)) { michael@0: SobelToPlaneRow = SobelToPlaneRow_NEON; michael@0: } michael@0: #endif michael@0: return ARGBSobelize(src_argb, src_stride_argb, dst_y, dst_stride_y, michael@0: width, height, SobelToPlaneRow); michael@0: } michael@0: michael@0: // SobelXY ARGB effect. michael@0: // Similar to Sobel, but also stores Sobel X in R and Sobel Y in B. G = Sobel. michael@0: LIBYUV_API michael@0: int ARGBSobelXY(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: void (*SobelXYRow)(const uint8* src_sobelx, const uint8* src_sobely, michael@0: uint8* dst_argb, int width) = SobelXYRow_C; michael@0: #if defined(HAS_SOBELXYROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: SobelXYRow = SobelXYRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SOBELXYROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { michael@0: SobelXYRow = SobelXYRow_NEON; michael@0: } michael@0: #endif michael@0: return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb, michael@0: width, height, SobelXYRow); michael@0: } michael@0: michael@0: // Apply a 4x4 polynomial to each ARGB pixel. michael@0: LIBYUV_API michael@0: int ARGBPolynomial(const uint8* src_argb, int src_stride_argb, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: const float* poly, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ARGBPolynomialRow)(const uint8* src_argb, michael@0: uint8* dst_argb, const float* poly, michael@0: int width) = ARGBPolynomialRow_C; michael@0: if (!src_argb || !dst_argb || !poly || width <= 0 || height == 0) { michael@0: return -1; 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: // Coalesce rows. michael@0: if (src_stride_argb == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBPOLYNOMIALROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 2)) { michael@0: ARGBPolynomialRow = ARGBPolynomialRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBPOLYNOMIALROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasFMA3) && michael@0: IS_ALIGNED(width, 2)) { michael@0: ARGBPolynomialRow = ARGBPolynomialRow_AVX2; michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBPolynomialRow(src_argb, dst_argb, poly, width); michael@0: src_argb += src_stride_argb; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Apply a lumacolortable to each ARGB pixel. michael@0: LIBYUV_API michael@0: int ARGBLumaColorTable(const uint8* src_argb, int src_stride_argb, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: const uint8* luma, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ARGBLumaColorTableRow)(const uint8* src_argb, uint8* dst_argb, michael@0: int width, const uint8* luma, const uint32 lumacoeff) = michael@0: ARGBLumaColorTableRow_C; michael@0: if (!src_argb || !dst_argb || !luma || width <= 0 || height == 0) { michael@0: return -1; 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: // Coalesce rows. michael@0: if (src_stride_argb == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBLUMACOLORTABLEROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4)) { michael@0: ARGBLumaColorTableRow = ARGBLumaColorTableRow_SSSE3; michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBLumaColorTableRow(src_argb, dst_argb, width, luma, 0x00264b0f); michael@0: src_argb += src_stride_argb; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Copy Alpha from one ARGB image to another. michael@0: LIBYUV_API michael@0: int ARGBCopyAlpha(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: int y; michael@0: void (*ARGBCopyAlphaRow)(const uint8* src_argb, uint8* dst_argb, int width) = michael@0: ARGBCopyAlphaRow_C; michael@0: if (!src_argb || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; 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: // Coalesce rows. michael@0: if (src_stride_argb == width * 4 && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_argb = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBCOPYALPHAROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16) && michael@0: IS_ALIGNED(width, 8)) { michael@0: ARGBCopyAlphaRow = ARGBCopyAlphaRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBCOPYALPHAROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 16)) { michael@0: ARGBCopyAlphaRow = ARGBCopyAlphaRow_AVX2; michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBCopyAlphaRow(src_argb, dst_argb, width); michael@0: src_argb += src_stride_argb; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Copy a planar Y channel to the alpha channel of a destination ARGB image. michael@0: LIBYUV_API michael@0: int ARGBCopyYToAlpha(const uint8* src_y, int src_stride_y, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int width, int height) { michael@0: int y; michael@0: void (*ARGBCopyYToAlphaRow)(const uint8* src_y, uint8* dst_argb, int width) = michael@0: ARGBCopyYToAlphaRow_C; michael@0: if (!src_y || !dst_argb || width <= 0 || height == 0) { michael@0: return -1; michael@0: } michael@0: // Negative height means invert the image. michael@0: if (height < 0) { michael@0: height = -height; michael@0: src_y = src_y + (height - 1) * src_stride_y; michael@0: src_stride_y = -src_stride_y; michael@0: } michael@0: // Coalesce rows. michael@0: if (src_stride_y == width && michael@0: dst_stride_argb == width * 4) { michael@0: width *= height; michael@0: height = 1; michael@0: src_stride_y = dst_stride_argb = 0; michael@0: } michael@0: #if defined(HAS_ARGBCOPYYTOALPHAROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && michael@0: IS_ALIGNED(src_y, 16) && IS_ALIGNED(src_stride_y, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16) && michael@0: IS_ALIGNED(width, 8)) { michael@0: ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_SSE2; michael@0: } michael@0: #endif michael@0: #if defined(HAS_ARGBCOPYYTOALPHAROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 16)) { michael@0: ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_AVX2; michael@0: } michael@0: #endif michael@0: michael@0: for (y = 0; y < height; ++y) { michael@0: ARGBCopyYToAlphaRow(src_y, dst_argb, width); michael@0: src_y += src_stride_y; michael@0: dst_argb += dst_stride_argb; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: #ifdef __cplusplus michael@0: } // extern "C" michael@0: } // namespace libyuv michael@0: #endif