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/scale.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "libyuv/cpu_id.h" michael@0: #include "libyuv/planar_functions.h" // For CopyARGB michael@0: #include "libyuv/row.h" michael@0: #include "libyuv/scale_row.h" michael@0: michael@0: #ifdef __cplusplus michael@0: namespace libyuv { michael@0: extern "C" { michael@0: #endif michael@0: michael@0: static __inline int Abs(int v) { michael@0: return v >= 0 ? v : -v; michael@0: } michael@0: michael@0: // ScaleARGB ARGB, 1/2 michael@0: // This is an optimized version for scaling down a ARGB to 1/2 of michael@0: // its original size. michael@0: static void ScaleARGBDown2(int src_width, int src_height, michael@0: int dst_width, int dst_height, michael@0: int src_stride, int dst_stride, michael@0: const uint8* src_argb, uint8* dst_argb, michael@0: int x, int dx, int y, int dy, michael@0: enum FilterMode filtering) { michael@0: int j; michael@0: int row_stride = src_stride * (dy >> 16); michael@0: void (*ScaleARGBRowDown2)(const uint8* src_argb, ptrdiff_t src_stride, michael@0: uint8* dst_argb, int dst_width) = michael@0: filtering == kFilterNone ? ScaleARGBRowDown2_C : michael@0: (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_C : michael@0: ScaleARGBRowDown2Box_C); michael@0: assert(dx == 65536 * 2); // Test scale factor of 2. michael@0: assert((dy & 0x1ffff) == 0); // Test vertical scale is multiple of 2. michael@0: // Advance to odd row, even column. michael@0: if (filtering == kFilterBilinear) { michael@0: src_argb += (y >> 16) * src_stride + (x >> 16) * 4; michael@0: } else { michael@0: src_argb += (y >> 16) * src_stride + ((x >> 16) - 1) * 4; michael@0: } michael@0: michael@0: #if defined(HAS_SCALEARGBROWDOWN2_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 4) && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(row_stride, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleARGBRowDown2 = filtering == kFilterNone ? ScaleARGBRowDown2_SSE2 : michael@0: (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_SSE2 : michael@0: ScaleARGBRowDown2Box_SSE2); michael@0: } michael@0: #elif defined(HAS_SCALEARGBROWDOWN2_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 8) && michael@0: IS_ALIGNED(src_argb, 4) && IS_ALIGNED(row_stride, 4)) { michael@0: ScaleARGBRowDown2 = filtering ? ScaleARGBRowDown2Box_NEON : michael@0: ScaleARGBRowDown2_NEON; michael@0: } michael@0: #endif michael@0: michael@0: if (filtering == kFilterLinear) { michael@0: src_stride = 0; michael@0: } michael@0: for (j = 0; j < dst_height; ++j) { michael@0: ScaleARGBRowDown2(src_argb, src_stride, dst_argb, dst_width); michael@0: src_argb += row_stride; michael@0: dst_argb += dst_stride; michael@0: } michael@0: } michael@0: michael@0: // ScaleARGB ARGB, 1/4 michael@0: // This is an optimized version for scaling down a ARGB to 1/4 of michael@0: // its original size. michael@0: static void ScaleARGBDown4Box(int src_width, int src_height, michael@0: int dst_width, int dst_height, michael@0: int src_stride, int dst_stride, michael@0: const uint8* src_argb, uint8* dst_argb, michael@0: int x, int dx, int y, int dy) { michael@0: int j; michael@0: // Allocate 2 rows of ARGB. michael@0: const int kRowSize = (dst_width * 2 * 4 + 15) & ~15; michael@0: align_buffer_64(row, kRowSize * 2); michael@0: int row_stride = src_stride * (dy >> 16); michael@0: void (*ScaleARGBRowDown2)(const uint8* src_argb, ptrdiff_t src_stride, michael@0: uint8* dst_argb, int dst_width) = ScaleARGBRowDown2Box_C; michael@0: // Advance to odd row, even column. michael@0: src_argb += (y >> 16) * src_stride + (x >> 16) * 4; michael@0: assert(dx == 65536 * 4); // Test scale factor of 4. michael@0: assert((dy & 0x3ffff) == 0); // Test vertical scale is multiple of 4. michael@0: #if defined(HAS_SCALEARGBROWDOWN2_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 4) && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(row_stride, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleARGBRowDown2 = ScaleARGBRowDown2Box_SSE2; michael@0: } michael@0: #elif defined(HAS_SCALEARGBROWDOWN2_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 8) && michael@0: IS_ALIGNED(src_argb, 4) && IS_ALIGNED(row_stride, 4)) { michael@0: ScaleARGBRowDown2 = ScaleARGBRowDown2Box_NEON; michael@0: } michael@0: #endif michael@0: for (j = 0; j < dst_height; ++j) { michael@0: ScaleARGBRowDown2(src_argb, src_stride, row, dst_width * 2); michael@0: ScaleARGBRowDown2(src_argb + src_stride * 2, src_stride, michael@0: row + kRowSize, dst_width * 2); michael@0: ScaleARGBRowDown2(row, kRowSize, dst_argb, dst_width); michael@0: src_argb += row_stride; michael@0: dst_argb += dst_stride; michael@0: } michael@0: free_aligned_buffer_64(row); michael@0: } michael@0: michael@0: // ScaleARGB ARGB Even michael@0: // This is an optimized version for scaling down a ARGB to even michael@0: // multiple of its original size. michael@0: static void ScaleARGBDownEven(int src_width, int src_height, michael@0: int dst_width, int dst_height, michael@0: int src_stride, int dst_stride, michael@0: const uint8* src_argb, uint8* dst_argb, michael@0: int x, int dx, int y, int dy, michael@0: enum FilterMode filtering) { michael@0: int j; michael@0: int col_step = dx >> 16; michael@0: int row_stride = (dy >> 16) * src_stride; michael@0: void (*ScaleARGBRowDownEven)(const uint8* src_argb, ptrdiff_t src_stride, michael@0: int src_step, uint8* dst_argb, int dst_width) = michael@0: filtering ? ScaleARGBRowDownEvenBox_C : ScaleARGBRowDownEven_C; michael@0: assert(IS_ALIGNED(src_width, 2)); michael@0: assert(IS_ALIGNED(src_height, 2)); michael@0: src_argb += (y >> 16) * src_stride + (x >> 16) * 4; michael@0: #if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 4) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_SSE2 : michael@0: ScaleARGBRowDownEven_SSE2; michael@0: } michael@0: #elif defined(HAS_SCALEARGBROWDOWNEVEN_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 4) && michael@0: IS_ALIGNED(src_argb, 4)) { michael@0: ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_NEON : michael@0: ScaleARGBRowDownEven_NEON; michael@0: } michael@0: #endif michael@0: michael@0: if (filtering == kFilterLinear) { michael@0: src_stride = 0; michael@0: } michael@0: for (j = 0; j < dst_height; ++j) { michael@0: ScaleARGBRowDownEven(src_argb, src_stride, col_step, dst_argb, dst_width); michael@0: src_argb += row_stride; michael@0: dst_argb += dst_stride; michael@0: } michael@0: } michael@0: michael@0: // Scale ARGB down with bilinear interpolation. michael@0: static void ScaleARGBBilinearDown(int src_width, int src_height, michael@0: int dst_width, int dst_height, michael@0: int src_stride, int dst_stride, michael@0: const uint8* src_argb, uint8* dst_argb, michael@0: int x, int dx, int y, int dy, michael@0: enum FilterMode filtering) { michael@0: int j; michael@0: int64 xlast = x + (int64)(dst_width - 1) * dx; michael@0: int64 xl = (dx >= 0) ? x : xlast; michael@0: int64 xr = (dx >= 0) ? xlast : x; michael@0: int clip_src_width; michael@0: xl = (xl >> 16) & ~3; // Left edge aligned. michael@0: xr = (xr >> 16) + 1; // Right most pixel used. michael@0: clip_src_width = (((xr - xl) + 1 + 3) & ~3) * 4; // Width aligned to 4. michael@0: src_argb += xl * 4; michael@0: x -= (int)(xl << 16); michael@0: void (*InterpolateRow)(uint8* dst_argb, const uint8* src_argb, michael@0: ptrdiff_t src_stride, int dst_width, int source_y_fraction) = michael@0: InterpolateRow_C; michael@0: #if defined(HAS_INTERPOLATEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && clip_src_width >= 16) { michael@0: InterpolateRow = InterpolateRow_Any_SSE2; michael@0: if (IS_ALIGNED(clip_src_width, 16)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSE2; michael@0: if (IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride, 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) && clip_src_width >= 16) { michael@0: InterpolateRow = InterpolateRow_Any_SSSE3; michael@0: if (IS_ALIGNED(clip_src_width, 16)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride, 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) && clip_src_width >= 32) { michael@0: InterpolateRow = InterpolateRow_Any_AVX2; michael@0: if (IS_ALIGNED(clip_src_width, 32)) { michael@0: InterpolateRow = InterpolateRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_INTERPOLATEROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && clip_src_width >= 16) { michael@0: InterpolateRow = InterpolateRow_Any_NEON; michael@0: if (IS_ALIGNED(clip_src_width, 16)) { 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) && clip_src_width >= 4 && michael@0: IS_ALIGNED(src_argb, 4) && IS_ALIGNED(src_stride, 4)) { michael@0: InterpolateRow = InterpolateRow_Any_MIPS_DSPR2; michael@0: if (IS_ALIGNED(clip_src_width, 4)) { michael@0: InterpolateRow = InterpolateRow_MIPS_DSPR2; michael@0: } michael@0: } michael@0: #endif michael@0: void (*ScaleARGBFilterCols)(uint8* dst_argb, const uint8* src_argb, michael@0: int dst_width, int x, int dx) = michael@0: (src_width >= 32768) ? ScaleARGBFilterCols64_C : ScaleARGBFilterCols_C; michael@0: #if defined(HAS_SCALEARGBFILTERCOLS_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { michael@0: ScaleARGBFilterCols = ScaleARGBFilterCols_SSSE3; michael@0: } michael@0: #endif michael@0: // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear. michael@0: // Allocate a row of ARGB. michael@0: align_buffer_64(row, clip_src_width * 4); michael@0: michael@0: const int max_y = (src_height - 1) << 16; michael@0: for (j = 0; j < dst_height; ++j) { michael@0: if (y > max_y) { michael@0: y = max_y; michael@0: } michael@0: int yi = y >> 16; michael@0: const uint8* src = src_argb + yi * src_stride; michael@0: if (filtering == kFilterLinear) { michael@0: ScaleARGBFilterCols(dst_argb, src, dst_width, x, dx); michael@0: } else { michael@0: int yf = (y >> 8) & 255; michael@0: InterpolateRow(row, src, src_stride, clip_src_width, yf); michael@0: ScaleARGBFilterCols(dst_argb, row, dst_width, x, dx); michael@0: } michael@0: dst_argb += dst_stride; michael@0: y += dy; michael@0: } michael@0: free_aligned_buffer_64(row); michael@0: } michael@0: michael@0: // Scale ARGB up with bilinear interpolation. michael@0: static void ScaleARGBBilinearUp(int src_width, int src_height, michael@0: int dst_width, int dst_height, michael@0: int src_stride, int dst_stride, michael@0: const uint8* src_argb, uint8* dst_argb, michael@0: int x, int dx, int y, int dy, michael@0: enum FilterMode filtering) { michael@0: int j; michael@0: void (*InterpolateRow)(uint8* dst_argb, const uint8* src_argb, michael@0: ptrdiff_t src_stride, int dst_width, int source_y_fraction) = michael@0: InterpolateRow_C; michael@0: void (*ScaleARGBFilterCols)(uint8* dst_argb, const uint8* src_argb, michael@0: int dst_width, int x, int dx) = michael@0: filtering ? ScaleARGBFilterCols_C : ScaleARGBCols_C; michael@0: #if defined(HAS_INTERPOLATEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && dst_width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_SSE2; michael@0: if (IS_ALIGNED(dst_width, 4)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSE2; michael@0: if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride, 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) && dst_width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_SSSE3; michael@0: if (IS_ALIGNED(dst_width, 4)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride, 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) && dst_width >= 8) { michael@0: InterpolateRow = InterpolateRow_Any_AVX2; michael@0: if (IS_ALIGNED(dst_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) && dst_width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_NEON; michael@0: if (IS_ALIGNED(dst_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) && dst_width >= 1 && michael@0: IS_ALIGNED(dst_argb, 4) && IS_ALIGNED(dst_stride, 4)) { michael@0: InterpolateRow = InterpolateRow_MIPS_DSPR2; michael@0: } michael@0: #endif michael@0: if (src_width >= 32768) { michael@0: ScaleARGBFilterCols = filtering ? michael@0: ScaleARGBFilterCols64_C : ScaleARGBCols64_C; michael@0: } michael@0: #if defined(HAS_SCALEARGBFILTERCOLS_SSSE3) michael@0: if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { michael@0: ScaleARGBFilterCols = ScaleARGBFilterCols_SSSE3; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SCALEARGBCOLS_SSE2) michael@0: if (!filtering && TestCpuFlag(kCpuHasSSE2) && src_width < 32768) { michael@0: ScaleARGBFilterCols = ScaleARGBCols_SSE2; michael@0: } michael@0: #endif michael@0: if (!filtering && src_width * 2 == dst_width && x < 0x8000) { michael@0: ScaleARGBFilterCols = ScaleARGBColsUp2_C; michael@0: #if defined(HAS_SCALEARGBCOLSUP2_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8) && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleARGBFilterCols = ScaleARGBColsUp2_SSE2; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: const int max_y = (src_height - 1) << 16; michael@0: if (y > max_y) { michael@0: y = max_y; michael@0: } michael@0: int yi = y >> 16; michael@0: const uint8* src = src_argb + yi * src_stride; michael@0: michael@0: // Allocate 2 rows of ARGB. michael@0: const int kRowSize = (dst_width * 4 + 15) & ~15; michael@0: align_buffer_64(row, kRowSize * 2); michael@0: michael@0: uint8* rowptr = row; michael@0: int rowstride = kRowSize; michael@0: int lasty = yi; michael@0: michael@0: ScaleARGBFilterCols(rowptr, src, dst_width, x, dx); michael@0: if (src_height > 1) { michael@0: src += src_stride; michael@0: } michael@0: ScaleARGBFilterCols(rowptr + rowstride, src, dst_width, x, dx); michael@0: src += src_stride; michael@0: michael@0: for (j = 0; j < dst_height; ++j) { michael@0: yi = y >> 16; michael@0: if (yi != lasty) { michael@0: if (y > max_y) { michael@0: y = max_y; michael@0: yi = y >> 16; michael@0: src = src_argb + yi * src_stride; michael@0: } michael@0: if (yi != lasty) { michael@0: ScaleARGBFilterCols(rowptr, src, dst_width, x, dx); michael@0: rowptr += rowstride; michael@0: rowstride = -rowstride; michael@0: lasty = yi; michael@0: src += src_stride; michael@0: } michael@0: } michael@0: if (filtering == kFilterLinear) { michael@0: InterpolateRow(dst_argb, rowptr, 0, dst_width * 4, 0); michael@0: } else { michael@0: int yf = (y >> 8) & 255; michael@0: InterpolateRow(dst_argb, rowptr, rowstride, dst_width * 4, yf); michael@0: } michael@0: dst_argb += dst_stride; michael@0: y += dy; michael@0: } michael@0: free_aligned_buffer_64(row); michael@0: } michael@0: michael@0: #ifdef YUVSCALEUP michael@0: // Scale YUV to ARGB up with bilinear interpolation. michael@0: static void ScaleYUVToARGBBilinearUp(int src_width, int src_height, michael@0: int dst_width, int dst_height, michael@0: int src_stride_y, michael@0: int src_stride_u, michael@0: int src_stride_v, michael@0: int dst_stride_argb, michael@0: const uint8* src_y, michael@0: const uint8* src_u, michael@0: const uint8* src_v, michael@0: uint8* dst_argb, michael@0: int x, int dx, int y, int dy, michael@0: enum FilterMode filtering) { michael@0: int j; michael@0: void (*I422ToARGBRow)(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) = I422ToARGBRow_C; michael@0: #if defined(HAS_I422TOARGBROW_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && src_width >= 8) { michael@0: I422ToARGBRow = I422ToARGBRow_Any_SSSE3; michael@0: if (IS_ALIGNED(src_width, 8)) { michael@0: I422ToARGBRow = I422ToARGBRow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { michael@0: I422ToARGBRow = I422ToARGBRow_SSSE3; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_I422TOARGBROW_AVX2) michael@0: if (TestCpuFlag(kCpuHasAVX2) && src_width >= 16) { michael@0: I422ToARGBRow = I422ToARGBRow_Any_AVX2; michael@0: if (IS_ALIGNED(src_width, 16)) { michael@0: I422ToARGBRow = I422ToARGBRow_AVX2; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_I422TOARGBROW_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && src_width >= 8) { michael@0: I422ToARGBRow = I422ToARGBRow_Any_NEON; michael@0: if (IS_ALIGNED(src_width, 8)) { michael@0: I422ToARGBRow = I422ToARGBRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_I422TOARGBROW_MIPS_DSPR2) michael@0: if (TestCpuFlag(kCpuHasMIPS_DSPR2) && IS_ALIGNED(src_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_argb, 4) && IS_ALIGNED(dst_stride_argb, 4)) { michael@0: I422ToARGBRow = I422ToARGBRow_MIPS_DSPR2; michael@0: } michael@0: #endif michael@0: michael@0: void (*InterpolateRow)(uint8* dst_argb, const uint8* src_argb, michael@0: ptrdiff_t src_stride, int dst_width, int source_y_fraction) = michael@0: InterpolateRow_C; michael@0: #if defined(HAS_INTERPOLATEROW_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && dst_width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_SSE2; michael@0: if (IS_ALIGNED(dst_width, 4)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSE2; michael@0: if (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) && dst_width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_SSSE3; michael@0: if (IS_ALIGNED(dst_width, 4)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSSE3; michael@0: if (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) && dst_width >= 8) { michael@0: InterpolateRow = InterpolateRow_Any_AVX2; michael@0: if (IS_ALIGNED(dst_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) && dst_width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_NEON; michael@0: if (IS_ALIGNED(dst_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) && dst_width >= 1 && michael@0: IS_ALIGNED(dst_argb, 4) && IS_ALIGNED(dst_stride_argb, 4)) { michael@0: InterpolateRow = InterpolateRow_MIPS_DSPR2; michael@0: } michael@0: #endif michael@0: michael@0: void (*ScaleARGBFilterCols)(uint8* dst_argb, const uint8* src_argb, michael@0: int dst_width, int x, int dx) = michael@0: filtering ? ScaleARGBFilterCols_C : ScaleARGBCols_C; michael@0: if (src_width >= 32768) { michael@0: ScaleARGBFilterCols = filtering ? michael@0: ScaleARGBFilterCols64_C : ScaleARGBCols64_C; michael@0: } michael@0: #if defined(HAS_SCALEARGBFILTERCOLS_SSSE3) michael@0: if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { michael@0: ScaleARGBFilterCols = ScaleARGBFilterCols_SSSE3; michael@0: } michael@0: #endif michael@0: #if defined(HAS_SCALEARGBCOLS_SSE2) michael@0: if (!filtering && TestCpuFlag(kCpuHasSSE2) && src_width < 32768) { michael@0: ScaleARGBFilterCols = ScaleARGBCols_SSE2; michael@0: } michael@0: #endif michael@0: if (!filtering && src_width * 2 == dst_width && x < 0x8000) { michael@0: ScaleARGBFilterCols = ScaleARGBColsUp2_C; michael@0: #if defined(HAS_SCALEARGBCOLSUP2_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8) && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleARGBFilterCols = ScaleARGBColsUp2_SSE2; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: const int max_y = (src_height - 1) << 16; michael@0: if (y > max_y) { michael@0: y = max_y; michael@0: } michael@0: const int kYShift = 1; // Shift Y by 1 to convert Y plane to UV coordinate. michael@0: int yi = y >> 16; michael@0: int uv_yi = yi >> kYShift; michael@0: const uint8* src_row_y = src_y + yi * src_stride_y; michael@0: const uint8* src_row_u = src_u + uv_yi * src_stride_u; michael@0: const uint8* src_row_v = src_v + uv_yi * src_stride_v; michael@0: michael@0: // Allocate 2 rows of ARGB. michael@0: const int kRowSize = (dst_width * 4 + 15) & ~15; michael@0: align_buffer_64(row, kRowSize * 2); michael@0: michael@0: // Allocate 1 row of ARGB for source conversion. michael@0: align_buffer_64(argb_row, src_width * 4); michael@0: michael@0: uint8* rowptr = row; michael@0: int rowstride = kRowSize; michael@0: int lasty = yi; michael@0: michael@0: // TODO(fbarchard): Convert first 2 rows of YUV to ARGB. michael@0: ScaleARGBFilterCols(rowptr, src_row_y, dst_width, x, dx); michael@0: if (src_height > 1) { michael@0: src_row_y += src_stride_y; michael@0: if (yi & 1) { michael@0: src_row_u += src_stride_u; michael@0: src_row_v += src_stride_v; michael@0: } michael@0: } michael@0: ScaleARGBFilterCols(rowptr + rowstride, src_row_y, dst_width, x, dx); michael@0: if (src_height > 2) { michael@0: src_row_y += src_stride_y; michael@0: if (!(yi & 1)) { michael@0: src_row_u += src_stride_u; michael@0: src_row_v += src_stride_v; michael@0: } michael@0: } michael@0: michael@0: for (j = 0; j < dst_height; ++j) { michael@0: yi = y >> 16; michael@0: if (yi != lasty) { michael@0: if (y > max_y) { michael@0: y = max_y; michael@0: yi = y >> 16; michael@0: uv_yi = yi >> kYShift; michael@0: src_row_y = src_y + yi * src_stride_y; michael@0: src_row_u = src_u + uv_yi * src_stride_u; michael@0: src_row_v = src_v + uv_yi * src_stride_v; michael@0: } michael@0: if (yi != lasty) { michael@0: // TODO(fbarchard): Convert the clipped region of row. michael@0: I422ToARGBRow(src_row_y, src_row_u, src_row_v, argb_row, src_width); michael@0: ScaleARGBFilterCols(rowptr, argb_row, dst_width, x, dx); michael@0: rowptr += rowstride; michael@0: rowstride = -rowstride; michael@0: lasty = yi; michael@0: src_row_y += src_stride_y; michael@0: if (yi & 1) { michael@0: src_row_u += src_stride_u; michael@0: src_row_v += src_stride_v; michael@0: } michael@0: } michael@0: } michael@0: if (filtering == kFilterLinear) { michael@0: InterpolateRow(dst_argb, rowptr, 0, dst_width * 4, 0); michael@0: } else { michael@0: int yf = (y >> 8) & 255; michael@0: InterpolateRow(dst_argb, rowptr, rowstride, dst_width * 4, yf); michael@0: } michael@0: dst_argb += dst_stride_argb; michael@0: y += dy; michael@0: } michael@0: free_aligned_buffer_64(row); michael@0: free_aligned_buffer_64(row_argb); michael@0: } michael@0: #endif michael@0: michael@0: // Scale ARGB to/from any dimensions, without interpolation. michael@0: // Fixed point math is used for performance: The upper 16 bits michael@0: // of x and dx is the integer part of the source position and michael@0: // the lower 16 bits are the fixed decimal part. michael@0: michael@0: static void ScaleARGBSimple(int src_width, int src_height, michael@0: int dst_width, int dst_height, michael@0: int src_stride, int dst_stride, michael@0: const uint8* src_argb, uint8* dst_argb, michael@0: int x, int dx, int y, int dy) { michael@0: int j; michael@0: void (*ScaleARGBCols)(uint8* dst_argb, const uint8* src_argb, michael@0: int dst_width, int x, int dx) = michael@0: (src_width >= 32768) ? ScaleARGBCols64_C : ScaleARGBCols_C; michael@0: #if defined(HAS_SCALEARGBCOLS_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && src_width < 32768) { michael@0: ScaleARGBCols = ScaleARGBCols_SSE2; michael@0: } michael@0: #endif michael@0: if (src_width * 2 == dst_width && x < 0x8000) { michael@0: ScaleARGBCols = ScaleARGBColsUp2_C; michael@0: #if defined(HAS_SCALEARGBCOLSUP2_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8) && michael@0: IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride, 16) && michael@0: IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleARGBCols = ScaleARGBColsUp2_SSE2; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: for (j = 0; j < dst_height; ++j) { michael@0: ScaleARGBCols(dst_argb, src_argb + (y >> 16) * src_stride, michael@0: dst_width, x, dx); michael@0: dst_argb += dst_stride; michael@0: y += dy; michael@0: } michael@0: } michael@0: michael@0: // ScaleARGB a ARGB. michael@0: // This function in turn calls a scaling function michael@0: // suitable for handling the desired resolutions. michael@0: static void ScaleARGB(const uint8* src, int src_stride, michael@0: int src_width, int src_height, michael@0: uint8* dst, int dst_stride, michael@0: int dst_width, int dst_height, michael@0: int clip_x, int clip_y, int clip_width, int clip_height, michael@0: enum FilterMode filtering) { michael@0: // Initial source x/y coordinate and step values as 16.16 fixed point. michael@0: int x = 0; michael@0: int y = 0; michael@0: int dx = 0; michael@0: int dy = 0; michael@0: // ARGB does not support box filter yet, but allow the user to pass it. michael@0: // Simplify filtering when possible. michael@0: filtering = ScaleFilterReduce(src_width, src_height, michael@0: dst_width, dst_height, michael@0: filtering); michael@0: michael@0: // Negative src_height means invert the image. michael@0: if (src_height < 0) { michael@0: src_height = -src_height; michael@0: src = src + (src_height - 1) * src_stride; michael@0: src_stride = -src_stride; michael@0: } michael@0: ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, michael@0: &x, &y, &dx, &dy); michael@0: src_width = Abs(src_width); michael@0: if (clip_x) { michael@0: int64 clipf = (int64)(clip_x) * dx; michael@0: x += (clipf & 0xffff); michael@0: src += (clipf >> 16) * 4; michael@0: dst += clip_x * 4; michael@0: } michael@0: if (clip_y) { michael@0: int64 clipf = (int64)(clip_y) * dy; michael@0: y += (clipf & 0xffff); michael@0: src += (clipf >> 16) * src_stride; michael@0: dst += clip_y * dst_stride; michael@0: } michael@0: michael@0: // Special case for integer step values. michael@0: if (((dx | dy) & 0xffff) == 0) { michael@0: if (!dx || !dy) { // 1 pixel wide and/or tall. michael@0: filtering = kFilterNone; michael@0: } else { michael@0: // Optimized even scale down. ie 2, 4, 6, 8, 10x. michael@0: if (!(dx & 0x10000) && !(dy & 0x10000)) { michael@0: if (dx == 0x20000) { michael@0: // Optimized 1/2 downsample. michael@0: ScaleARGBDown2(src_width, src_height, michael@0: clip_width, clip_height, michael@0: src_stride, dst_stride, src, dst, michael@0: x, dx, y, dy, filtering); michael@0: return; michael@0: } michael@0: if (dx == 0x40000 && filtering == kFilterBox) { michael@0: // Optimized 1/4 box downsample. michael@0: ScaleARGBDown4Box(src_width, src_height, michael@0: clip_width, clip_height, michael@0: src_stride, dst_stride, src, dst, michael@0: x, dx, y, dy); michael@0: return; michael@0: } michael@0: ScaleARGBDownEven(src_width, src_height, michael@0: clip_width, clip_height, michael@0: src_stride, dst_stride, src, dst, michael@0: x, dx, y, dy, filtering); michael@0: return; michael@0: } michael@0: // Optimized odd scale down. ie 3, 5, 7, 9x. michael@0: if ((dx & 0x10000) && (dy & 0x10000)) { michael@0: filtering = kFilterNone; michael@0: if (dx == 0x10000 && dy == 0x10000) { michael@0: // Straight copy. michael@0: ARGBCopy(src + (y >> 16) * src_stride + (x >> 16) * 4, src_stride, michael@0: dst, dst_stride, clip_width, clip_height); michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: if (dx == 0x10000 && (x & 0xffff) == 0) { michael@0: // Arbitrary scale vertically, but unscaled vertically. michael@0: ScalePlaneVertical(src_height, michael@0: clip_width, clip_height, michael@0: src_stride, dst_stride, src, dst, michael@0: x, y, dy, 4, filtering); michael@0: return; michael@0: } michael@0: if (filtering && dy < 65536) { michael@0: ScaleARGBBilinearUp(src_width, src_height, michael@0: clip_width, clip_height, michael@0: src_stride, dst_stride, src, dst, michael@0: x, dx, y, dy, filtering); michael@0: return; michael@0: } michael@0: if (filtering) { michael@0: ScaleARGBBilinearDown(src_width, src_height, michael@0: clip_width, clip_height, michael@0: src_stride, dst_stride, src, dst, michael@0: x, dx, y, dy, filtering); michael@0: return; michael@0: } michael@0: ScaleARGBSimple(src_width, src_height, clip_width, clip_height, michael@0: src_stride, dst_stride, src, dst, michael@0: x, dx, y, dy); michael@0: } michael@0: michael@0: LIBYUV_API michael@0: int ARGBScaleClip(const uint8* src_argb, int src_stride_argb, michael@0: int src_width, int src_height, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int dst_width, int dst_height, michael@0: int clip_x, int clip_y, int clip_width, int clip_height, michael@0: enum FilterMode filtering) { michael@0: if (!src_argb || src_width == 0 || src_height == 0 || michael@0: !dst_argb || dst_width <= 0 || dst_height <= 0 || michael@0: clip_x < 0 || clip_y < 0 || michael@0: (clip_x + clip_width) > dst_width || michael@0: (clip_y + clip_height) > dst_height) { michael@0: return -1; michael@0: } michael@0: ScaleARGB(src_argb, src_stride_argb, src_width, src_height, michael@0: dst_argb, dst_stride_argb, dst_width, dst_height, michael@0: clip_x, clip_y, clip_width, clip_height, filtering); michael@0: return 0; michael@0: } michael@0: michael@0: // Scale an ARGB image. michael@0: LIBYUV_API michael@0: int ARGBScale(const uint8* src_argb, int src_stride_argb, michael@0: int src_width, int src_height, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int dst_width, int dst_height, michael@0: enum FilterMode filtering) { michael@0: if (!src_argb || src_width == 0 || src_height == 0 || michael@0: !dst_argb || dst_width <= 0 || dst_height <= 0) { michael@0: return -1; michael@0: } michael@0: ScaleARGB(src_argb, src_stride_argb, src_width, src_height, michael@0: dst_argb, dst_stride_argb, dst_width, dst_height, michael@0: 0, 0, dst_width, dst_height, filtering); michael@0: return 0; michael@0: } michael@0: michael@0: #ifdef __cplusplus michael@0: } // extern "C" michael@0: } // namespace libyuv michael@0: #endif