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 CopyPlane 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: // Remove this macro if OVERREAD is safe. michael@0: #define AVOID_OVERREAD 1 michael@0: michael@0: static __inline int Abs(int v) { michael@0: return v >= 0 ? v : -v; michael@0: } michael@0: michael@0: #define SUBSAMPLE(v, a, s) (v < 0) ? (-((-v + a) >> s)) : ((v + a) >> s) michael@0: michael@0: // Scale plane, 1/2 michael@0: // This is an optimized version for scaling down a plane to 1/2 of michael@0: // its original size. michael@0: michael@0: static void ScalePlaneDown2(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_ptr, uint8* dst_ptr, michael@0: enum FilterMode filtering) { michael@0: void (*ScaleRowDown2)(const uint8* src_ptr, ptrdiff_t src_stride, michael@0: uint8* dst_ptr, int dst_width) = michael@0: filtering == kFilterNone ? ScaleRowDown2_C : michael@0: (filtering == kFilterLinear ? ScaleRowDown2Linear_C : michael@0: ScaleRowDown2Box_C); michael@0: int row_stride = src_stride << 1; michael@0: if (!filtering) { michael@0: src_ptr += src_stride; // Point to odd rows. michael@0: src_stride = 0; michael@0: } michael@0: michael@0: #if defined(HAS_SCALEROWDOWN2_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 16)) { michael@0: ScaleRowDown2 = filtering ? ScaleRowDown2Box_NEON : ScaleRowDown2_NEON; michael@0: } michael@0: #elif defined(HAS_SCALEROWDOWN2_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 16)) { michael@0: ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_Unaligned_SSE2 : michael@0: (filtering == kFilterLinear ? ScaleRowDown2Linear_Unaligned_SSE2 : michael@0: ScaleRowDown2Box_Unaligned_SSE2); michael@0: if (IS_ALIGNED(src_ptr, 16) && michael@0: IS_ALIGNED(src_stride, 16) && IS_ALIGNED(row_stride, 16) && michael@0: IS_ALIGNED(dst_ptr, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_SSE2 : michael@0: (filtering == kFilterLinear ? ScaleRowDown2Linear_SSE2 : michael@0: ScaleRowDown2Box_SSE2); michael@0: } michael@0: } michael@0: #elif defined(HAS_SCALEROWDOWN2_MIPS_DSPR2) michael@0: if (TestCpuFlag(kCpuHasMIPS_DSPR2) && IS_ALIGNED(src_ptr, 4) && michael@0: IS_ALIGNED(src_stride, 4) && IS_ALIGNED(row_stride, 4) && michael@0: IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { michael@0: ScaleRowDown2 = filtering ? michael@0: ScaleRowDown2Box_MIPS_DSPR2 : ScaleRowDown2_MIPS_DSPR2; michael@0: } michael@0: #endif michael@0: michael@0: if (filtering == kFilterLinear) { michael@0: src_stride = 0; michael@0: } michael@0: // TODO(fbarchard): Loop through source height to allow odd height. michael@0: int y; michael@0: for (y = 0; y < dst_height; ++y) { michael@0: ScaleRowDown2(src_ptr, src_stride, dst_ptr, dst_width); michael@0: src_ptr += row_stride; michael@0: dst_ptr += dst_stride; michael@0: } michael@0: } michael@0: michael@0: // Scale plane, 1/4 michael@0: // This is an optimized version for scaling down a plane to 1/4 of michael@0: // its original size. michael@0: michael@0: static void ScalePlaneDown4(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_ptr, uint8* dst_ptr, michael@0: enum FilterMode filtering) { michael@0: void (*ScaleRowDown4)(const uint8* src_ptr, ptrdiff_t src_stride, michael@0: uint8* dst_ptr, int dst_width) = michael@0: filtering ? ScaleRowDown4Box_C : ScaleRowDown4_C; michael@0: int row_stride = src_stride << 2; michael@0: if (!filtering) { michael@0: src_ptr += src_stride * 2; // Point to row 2. michael@0: src_stride = 0; michael@0: } michael@0: #if defined(HAS_SCALEROWDOWN4_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 8)) { michael@0: ScaleRowDown4 = filtering ? ScaleRowDown4Box_NEON : ScaleRowDown4_NEON; michael@0: } michael@0: #elif defined(HAS_SCALEROWDOWN4_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && michael@0: IS_ALIGNED(dst_width, 8) && IS_ALIGNED(row_stride, 16) && michael@0: IS_ALIGNED(src_ptr, 16) && IS_ALIGNED(src_stride, 16)) { michael@0: ScaleRowDown4 = filtering ? ScaleRowDown4Box_SSE2 : ScaleRowDown4_SSE2; michael@0: } michael@0: #elif defined(HAS_SCALEROWDOWN4_MIPS_DSPR2) michael@0: if (TestCpuFlag(kCpuHasMIPS_DSPR2) && IS_ALIGNED(row_stride, 4) && michael@0: IS_ALIGNED(src_ptr, 4) && IS_ALIGNED(src_stride, 4) && michael@0: IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { michael@0: ScaleRowDown4 = filtering ? michael@0: ScaleRowDown4Box_MIPS_DSPR2 : ScaleRowDown4_MIPS_DSPR2; michael@0: } michael@0: #endif michael@0: michael@0: if (filtering == kFilterLinear) { michael@0: src_stride = 0; michael@0: } michael@0: int y; michael@0: for (y = 0; y < dst_height; ++y) { michael@0: ScaleRowDown4(src_ptr, src_stride, dst_ptr, dst_width); michael@0: src_ptr += row_stride; michael@0: dst_ptr += dst_stride; michael@0: } michael@0: } michael@0: michael@0: // Scale plane down, 3/4 michael@0: michael@0: static void ScalePlaneDown34(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_ptr, uint8* dst_ptr, michael@0: enum FilterMode filtering) { michael@0: assert(dst_width % 3 == 0); michael@0: void (*ScaleRowDown34_0)(const uint8* src_ptr, ptrdiff_t src_stride, michael@0: uint8* dst_ptr, int dst_width); michael@0: void (*ScaleRowDown34_1)(const uint8* src_ptr, ptrdiff_t src_stride, michael@0: uint8* dst_ptr, int dst_width); michael@0: if (!filtering) { michael@0: ScaleRowDown34_0 = ScaleRowDown34_C; michael@0: ScaleRowDown34_1 = ScaleRowDown34_C; michael@0: } else { michael@0: ScaleRowDown34_0 = ScaleRowDown34_0_Box_C; michael@0: ScaleRowDown34_1 = ScaleRowDown34_1_Box_C; michael@0: } michael@0: #if defined(HAS_SCALEROWDOWN34_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && (dst_width % 24 == 0)) { michael@0: if (!filtering) { michael@0: ScaleRowDown34_0 = ScaleRowDown34_NEON; michael@0: ScaleRowDown34_1 = ScaleRowDown34_NEON; michael@0: } else { michael@0: ScaleRowDown34_0 = ScaleRowDown34_0_Box_NEON; michael@0: ScaleRowDown34_1 = ScaleRowDown34_1_Box_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_SCALEROWDOWN34_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && (dst_width % 24 == 0) && michael@0: IS_ALIGNED(src_ptr, 16) && IS_ALIGNED(src_stride, 16)) { michael@0: if (!filtering) { michael@0: ScaleRowDown34_0 = ScaleRowDown34_SSSE3; michael@0: ScaleRowDown34_1 = ScaleRowDown34_SSSE3; michael@0: } else { michael@0: ScaleRowDown34_0 = ScaleRowDown34_0_Box_SSSE3; michael@0: ScaleRowDown34_1 = ScaleRowDown34_1_Box_SSSE3; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_SCALEROWDOWN34_MIPS_DSPR2) michael@0: if (TestCpuFlag(kCpuHasMIPS_DSPR2) && (dst_width % 24 == 0) && michael@0: IS_ALIGNED(src_ptr, 4) && IS_ALIGNED(src_stride, 4) && michael@0: IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { michael@0: if (!filtering) { michael@0: ScaleRowDown34_0 = ScaleRowDown34_MIPS_DSPR2; michael@0: ScaleRowDown34_1 = ScaleRowDown34_MIPS_DSPR2; michael@0: } else { michael@0: ScaleRowDown34_0 = ScaleRowDown34_0_Box_MIPS_DSPR2; michael@0: ScaleRowDown34_1 = ScaleRowDown34_1_Box_MIPS_DSPR2; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; michael@0: int y; michael@0: for (y = 0; y < dst_height - 2; y += 3) { michael@0: ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); michael@0: src_ptr += src_stride; michael@0: dst_ptr += dst_stride; michael@0: ScaleRowDown34_1(src_ptr, filter_stride, dst_ptr, dst_width); michael@0: src_ptr += src_stride; michael@0: dst_ptr += dst_stride; michael@0: ScaleRowDown34_0(src_ptr + src_stride, -filter_stride, michael@0: dst_ptr, dst_width); michael@0: src_ptr += src_stride * 2; michael@0: dst_ptr += dst_stride; michael@0: } michael@0: michael@0: // Remainder 1 or 2 rows with last row vertically unfiltered michael@0: if ((dst_height % 3) == 2) { michael@0: ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); michael@0: src_ptr += src_stride; michael@0: dst_ptr += dst_stride; michael@0: ScaleRowDown34_1(src_ptr, 0, dst_ptr, dst_width); michael@0: } else if ((dst_height % 3) == 1) { michael@0: ScaleRowDown34_0(src_ptr, 0, dst_ptr, dst_width); michael@0: } michael@0: } michael@0: michael@0: michael@0: // Scale plane, 3/8 michael@0: // This is an optimized version for scaling down a plane to 3/8 michael@0: // of its original size. michael@0: // michael@0: // Uses box filter arranges like this michael@0: // aaabbbcc -> abc michael@0: // aaabbbcc def michael@0: // aaabbbcc ghi michael@0: // dddeeeff michael@0: // dddeeeff michael@0: // dddeeeff michael@0: // ggghhhii michael@0: // ggghhhii michael@0: // Boxes are 3x3, 2x3, 3x2 and 2x2 michael@0: michael@0: static void ScalePlaneDown38(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_ptr, uint8* dst_ptr, michael@0: enum FilterMode filtering) { michael@0: assert(dst_width % 3 == 0); michael@0: void (*ScaleRowDown38_3)(const uint8* src_ptr, ptrdiff_t src_stride, michael@0: uint8* dst_ptr, int dst_width); michael@0: void (*ScaleRowDown38_2)(const uint8* src_ptr, ptrdiff_t src_stride, michael@0: uint8* dst_ptr, int dst_width); michael@0: if (!filtering) { michael@0: ScaleRowDown38_3 = ScaleRowDown38_C; michael@0: ScaleRowDown38_2 = ScaleRowDown38_C; michael@0: } else { michael@0: ScaleRowDown38_3 = ScaleRowDown38_3_Box_C; michael@0: ScaleRowDown38_2 = ScaleRowDown38_2_Box_C; michael@0: } michael@0: #if defined(HAS_SCALEROWDOWN38_NEON) michael@0: if (TestCpuFlag(kCpuHasNEON) && (dst_width % 12 == 0)) { michael@0: if (!filtering) { michael@0: ScaleRowDown38_3 = ScaleRowDown38_NEON; michael@0: ScaleRowDown38_2 = ScaleRowDown38_NEON; michael@0: } else { michael@0: ScaleRowDown38_3 = ScaleRowDown38_3_Box_NEON; michael@0: ScaleRowDown38_2 = ScaleRowDown38_2_Box_NEON; michael@0: } michael@0: } michael@0: #elif defined(HAS_SCALEROWDOWN38_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && (dst_width % 24 == 0) && michael@0: IS_ALIGNED(src_ptr, 16) && IS_ALIGNED(src_stride, 16)) { michael@0: if (!filtering) { michael@0: ScaleRowDown38_3 = ScaleRowDown38_SSSE3; michael@0: ScaleRowDown38_2 = ScaleRowDown38_SSSE3; michael@0: } else { michael@0: ScaleRowDown38_3 = ScaleRowDown38_3_Box_SSSE3; michael@0: ScaleRowDown38_2 = ScaleRowDown38_2_Box_SSSE3; michael@0: } michael@0: } michael@0: #elif defined(HAS_SCALEROWDOWN38_MIPS_DSPR2) michael@0: if (TestCpuFlag(kCpuHasMIPS_DSPR2) && (dst_width % 12 == 0) && michael@0: IS_ALIGNED(src_ptr, 4) && IS_ALIGNED(src_stride, 4) && michael@0: IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { michael@0: if (!filtering) { michael@0: ScaleRowDown38_3 = ScaleRowDown38_MIPS_DSPR2; michael@0: ScaleRowDown38_2 = ScaleRowDown38_MIPS_DSPR2; michael@0: } else { michael@0: ScaleRowDown38_3 = ScaleRowDown38_3_Box_MIPS_DSPR2; michael@0: ScaleRowDown38_2 = ScaleRowDown38_2_Box_MIPS_DSPR2; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; michael@0: int y; michael@0: for (y = 0; y < dst_height - 2; y += 3) { michael@0: ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); michael@0: src_ptr += src_stride * 3; michael@0: dst_ptr += dst_stride; michael@0: ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); michael@0: src_ptr += src_stride * 3; michael@0: dst_ptr += dst_stride; michael@0: ScaleRowDown38_2(src_ptr, filter_stride, dst_ptr, dst_width); michael@0: src_ptr += src_stride * 2; michael@0: dst_ptr += dst_stride; michael@0: } michael@0: michael@0: // Remainder 1 or 2 rows with last row vertically unfiltered michael@0: if ((dst_height % 3) == 2) { michael@0: ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); michael@0: src_ptr += src_stride * 3; michael@0: dst_ptr += dst_stride; michael@0: ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); michael@0: } else if ((dst_height % 3) == 1) { michael@0: ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); michael@0: } michael@0: } michael@0: michael@0: static __inline uint32 SumBox(int iboxwidth, int iboxheight, michael@0: ptrdiff_t src_stride, const uint8* src_ptr) { michael@0: assert(iboxwidth > 0); michael@0: assert(iboxheight > 0); michael@0: uint32 sum = 0u; michael@0: int y; michael@0: for (y = 0; y < iboxheight; ++y) { michael@0: int x; michael@0: for (x = 0; x < iboxwidth; ++x) { michael@0: sum += src_ptr[x]; michael@0: } michael@0: src_ptr += src_stride; michael@0: } michael@0: return sum; michael@0: } michael@0: michael@0: static void ScalePlaneBoxRow_C(int dst_width, int boxheight, michael@0: int x, int dx, ptrdiff_t src_stride, michael@0: const uint8* src_ptr, uint8* dst_ptr) { michael@0: int i; michael@0: for (i = 0; i < dst_width; ++i) { michael@0: int ix = x >> 16; michael@0: x += dx; michael@0: int boxwidth = (x >> 16) - ix; michael@0: *dst_ptr++ = SumBox(boxwidth, boxheight, src_stride, src_ptr + ix) / michael@0: (boxwidth * boxheight); michael@0: } michael@0: } michael@0: michael@0: static __inline uint32 SumPixels(int iboxwidth, const uint16* src_ptr) { michael@0: assert(iboxwidth > 0); michael@0: uint32 sum = 0u; michael@0: int x; michael@0: for (x = 0; x < iboxwidth; ++x) { michael@0: sum += src_ptr[x]; michael@0: } michael@0: return sum; michael@0: } michael@0: michael@0: static void ScaleAddCols2_C(int dst_width, int boxheight, int x, int dx, michael@0: const uint16* src_ptr, uint8* dst_ptr) { michael@0: int scaletbl[2]; michael@0: int minboxwidth = (dx >> 16); michael@0: scaletbl[0] = 65536 / (minboxwidth * boxheight); michael@0: scaletbl[1] = 65536 / ((minboxwidth + 1) * boxheight); michael@0: int* scaleptr = scaletbl - minboxwidth; michael@0: int i; michael@0: for (i = 0; i < dst_width; ++i) { michael@0: int ix = x >> 16; michael@0: x += dx; michael@0: int boxwidth = (x >> 16) - ix; michael@0: *dst_ptr++ = SumPixels(boxwidth, src_ptr + ix) * scaleptr[boxwidth] >> 16; michael@0: } michael@0: } michael@0: michael@0: static void ScaleAddCols1_C(int dst_width, int boxheight, int x, int dx, michael@0: const uint16* src_ptr, uint8* dst_ptr) { michael@0: int boxwidth = (dx >> 16); michael@0: int scaleval = 65536 / (boxwidth * boxheight); michael@0: int i; michael@0: for (i = 0; i < dst_width; ++i) { michael@0: *dst_ptr++ = SumPixels(boxwidth, src_ptr + x) * scaleval >> 16; michael@0: x += boxwidth; michael@0: } michael@0: } michael@0: michael@0: // Scale plane down to any dimensions, with interpolation. michael@0: // (boxfilter). michael@0: // michael@0: // Same method as SimpleScale, which is fixed point, outputting michael@0: // one pixel of destination using fixed point (16.16) to step michael@0: // through source, sampling a box of pixel with simple michael@0: // averaging. michael@0: static void ScalePlaneBox(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_ptr, uint8* dst_ptr) { 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: ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterBox, michael@0: &x, &y, &dx, &dy); michael@0: src_width = Abs(src_width); michael@0: const int max_y = (src_height << 16); michael@0: // TODO(fbarchard): Remove this and make AddRows handle boxheight 1. michael@0: if (!IS_ALIGNED(src_width, 16) || dst_height * 2 > src_height) { michael@0: uint8* dst = dst_ptr; michael@0: int j; michael@0: for (j = 0; j < dst_height; ++j) { michael@0: int iy = y >> 16; michael@0: const uint8* src = src_ptr + iy * src_stride; michael@0: y += dy; michael@0: if (y > max_y) { michael@0: y = max_y; michael@0: } michael@0: int boxheight = (y >> 16) - iy; michael@0: ScalePlaneBoxRow_C(dst_width, boxheight, michael@0: x, dx, src_stride, michael@0: src, dst); michael@0: dst += dst_stride; michael@0: } michael@0: return; michael@0: } michael@0: // Allocate a row buffer of uint16. michael@0: align_buffer_64(row16, src_width * 2); michael@0: michael@0: void (*ScaleAddCols)(int dst_width, int boxheight, int x, int dx, michael@0: const uint16* src_ptr, uint8* dst_ptr) = michael@0: (dx & 0xffff) ? ScaleAddCols2_C: ScaleAddCols1_C; michael@0: void (*ScaleAddRows)(const uint8* src_ptr, ptrdiff_t src_stride, michael@0: uint16* dst_ptr, int src_width, int src_height) = ScaleAddRows_C; michael@0: #if defined(HAS_SCALEADDROWS_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && michael@0: #ifdef AVOID_OVERREAD michael@0: IS_ALIGNED(src_width, 16) && michael@0: #endif michael@0: IS_ALIGNED(src_ptr, 16) && IS_ALIGNED(src_stride, 16)) { michael@0: ScaleAddRows = ScaleAddRows_SSE2; michael@0: } michael@0: #endif michael@0: michael@0: int j; michael@0: for (j = 0; j < dst_height; ++j) { michael@0: int iy = y >> 16; michael@0: const uint8* src = src_ptr + iy * src_stride; michael@0: y += dy; michael@0: if (y > (src_height << 16)) { michael@0: y = (src_height << 16); michael@0: } michael@0: int boxheight = (y >> 16) - iy; michael@0: ScaleAddRows(src, src_stride, (uint16*)(row16), michael@0: src_width, boxheight); michael@0: ScaleAddCols(dst_width, boxheight, x, dx, (uint16*)(row16), michael@0: dst_ptr); michael@0: dst_ptr += dst_stride; michael@0: } michael@0: free_aligned_buffer_64(row16); michael@0: } michael@0: michael@0: // Scale plane down with bilinear interpolation. michael@0: void ScalePlaneBilinearDown(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_ptr, uint8* dst_ptr, 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: 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: michael@0: void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr, 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) && src_width >= 16) { michael@0: InterpolateRow = InterpolateRow_Any_SSE2; michael@0: if (IS_ALIGNED(src_width, 16)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSE2; michael@0: if (IS_ALIGNED(src_ptr, 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) && src_width >= 16) { michael@0: InterpolateRow = InterpolateRow_Any_SSSE3; michael@0: if (IS_ALIGNED(src_width, 16)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(src_ptr, 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) && src_width >= 32) { michael@0: InterpolateRow = InterpolateRow_Any_AVX2; michael@0: if (IS_ALIGNED(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) && src_width >= 16) { michael@0: InterpolateRow = InterpolateRow_Any_NEON; michael@0: if (IS_ALIGNED(src_width, 16)) { michael@0: InterpolateRow = InterpolateRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_INTERPOLATEROW_MIPS_DSPR2) michael@0: if (TestCpuFlag(kCpuHasMIPS_DSPR2) && src_width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_MIPS_DSPR2; michael@0: if (IS_ALIGNED(src_width, 4)) { michael@0: InterpolateRow = InterpolateRow_MIPS_DSPR2; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: void (*ScaleFilterCols)(uint8* dst_ptr, const uint8* src_ptr, michael@0: int dst_width, int x, int dx) = michael@0: (src_width >= 32768) ? ScaleFilterCols64_C : ScaleFilterCols_C; michael@0: michael@0: #if defined(HAS_SCALEFILTERCOLS_SSSE3) michael@0: if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { michael@0: ScaleFilterCols = ScaleFilterCols_SSSE3; michael@0: } michael@0: #endif michael@0: michael@0: // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear. michael@0: // Allocate a row buffer. michael@0: align_buffer_64(row, src_width); michael@0: michael@0: const int max_y = (src_height - 1) << 16; michael@0: int j; 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_ptr + yi * src_stride; michael@0: if (filtering == kFilterLinear) { michael@0: ScaleFilterCols(dst_ptr, src, dst_width, x, dx); michael@0: } else { michael@0: int yf = (y >> 8) & 255; michael@0: InterpolateRow(row, src, src_stride, src_width, yf); michael@0: ScaleFilterCols(dst_ptr, row, dst_width, x, dx); michael@0: } michael@0: dst_ptr += dst_stride; michael@0: y += dy; michael@0: } michael@0: free_aligned_buffer_64(row); michael@0: } michael@0: michael@0: // Scale up down with bilinear interpolation. michael@0: void ScalePlaneBilinearUp(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_ptr, uint8* dst_ptr, 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: 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: michael@0: void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr, 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 >= 16) { michael@0: InterpolateRow = InterpolateRow_Any_SSE2; michael@0: if (IS_ALIGNED(dst_width, 16)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSE2; michael@0: if (IS_ALIGNED(dst_ptr, 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 >= 16) { michael@0: InterpolateRow = InterpolateRow_Any_SSSE3; michael@0: if (IS_ALIGNED(dst_width, 16)) { michael@0: InterpolateRow = InterpolateRow_Unaligned_SSSE3; michael@0: if (IS_ALIGNED(dst_ptr, 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 >= 32) { michael@0: InterpolateRow = InterpolateRow_Any_AVX2; michael@0: if (IS_ALIGNED(dst_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) && dst_width >= 16) { michael@0: InterpolateRow = InterpolateRow_Any_NEON; michael@0: if (IS_ALIGNED(dst_width, 16)) { michael@0: InterpolateRow = InterpolateRow_NEON; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(HAS_INTERPOLATEROW_MIPS_DSPR2) michael@0: if (TestCpuFlag(kCpuHasMIPS_DSPR2) && dst_width >= 4) { michael@0: InterpolateRow = InterpolateRow_Any_MIPS_DSPR2; michael@0: if (IS_ALIGNED(dst_width, 4)) { michael@0: InterpolateRow = InterpolateRow_MIPS_DSPR2; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: void (*ScaleFilterCols)(uint8* dst_ptr, const uint8* src_ptr, michael@0: int dst_width, int x, int dx) = michael@0: filtering ? ScaleFilterCols_C : ScaleCols_C; michael@0: if (filtering && src_width >= 32768) { michael@0: ScaleFilterCols = ScaleFilterCols64_C; michael@0: } michael@0: #if defined(HAS_SCALEFILTERCOLS_SSSE3) michael@0: if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { michael@0: ScaleFilterCols = ScaleFilterCols_SSSE3; michael@0: } michael@0: #endif michael@0: if (!filtering && src_width * 2 == dst_width && x < 0x8000) { michael@0: ScaleFilterCols = ScaleColsUp2_C; michael@0: #if defined(HAS_SCALECOLS_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8) && michael@0: IS_ALIGNED(src_ptr, 16) && IS_ALIGNED(src_stride, 16) && michael@0: IS_ALIGNED(dst_ptr, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleFilterCols = ScaleColsUp2_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_ptr + yi * src_stride; michael@0: michael@0: // Allocate 2 row buffers. michael@0: const int kRowSize = (dst_width + 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: ScaleFilterCols(rowptr, src, dst_width, x, dx); michael@0: if (src_height > 1) { michael@0: src += src_stride; michael@0: } michael@0: ScaleFilterCols(rowptr + rowstride, src, dst_width, x, dx); michael@0: src += src_stride; michael@0: michael@0: int j; 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_ptr + yi * src_stride; michael@0: } michael@0: if (yi != lasty) { michael@0: ScaleFilterCols(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_ptr, rowptr, 0, dst_width, 0); michael@0: } else { michael@0: int yf = (y >> 8) & 255; michael@0: InterpolateRow(dst_ptr, rowptr, rowstride, dst_width, yf); michael@0: } michael@0: dst_ptr += dst_stride; michael@0: y += dy; michael@0: } michael@0: free_aligned_buffer_64(row); michael@0: } michael@0: michael@0: // Scale Plane 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 ScalePlaneSimple(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_ptr, uint8* dst_ptr) { 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: ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterNone, michael@0: &x, &y, &dx, &dy); michael@0: src_width = Abs(src_width); michael@0: michael@0: void (*ScaleCols)(uint8* dst_ptr, const uint8* src_ptr, michael@0: int dst_width, int x, int dx) = ScaleCols_C; michael@0: if (src_width * 2 == dst_width && x < 0x8000) { michael@0: ScaleCols = ScaleColsUp2_C; michael@0: #if defined(HAS_SCALECOLS_SSE2) michael@0: if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8) && michael@0: IS_ALIGNED(src_ptr, 16) && IS_ALIGNED(src_stride, 16) && michael@0: IS_ALIGNED(dst_ptr, 16) && IS_ALIGNED(dst_stride, 16)) { michael@0: ScaleCols = ScaleColsUp2_SSE2; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: int i; michael@0: for (i = 0; i < dst_height; ++i) { michael@0: ScaleCols(dst_ptr, src_ptr + (y >> 16) * src_stride, michael@0: dst_width, x, dx); michael@0: dst_ptr += dst_stride; michael@0: y += dy; michael@0: } michael@0: } michael@0: michael@0: // Scale a plane. michael@0: // This function dispatches to a specialized scaler based on scale factor. michael@0: michael@0: LIBYUV_API michael@0: void ScalePlane(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: enum FilterMode filtering) { 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 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: michael@0: // Use specialized scales to improve performance for common resolutions. michael@0: // For example, all the 1/2 scalings will use ScalePlaneDown2() michael@0: if (dst_width == src_width && dst_height == src_height) { michael@0: // Straight copy. michael@0: CopyPlane(src, src_stride, dst, dst_stride, dst_width, dst_height); michael@0: return; michael@0: } michael@0: if (dst_width == src_width) { michael@0: int dy = FixedDiv(src_height, dst_height); michael@0: // Arbitrary scale vertically, but unscaled vertically. michael@0: ScalePlaneVertical(src_height, michael@0: dst_width, dst_height, michael@0: src_stride, dst_stride, src, dst, michael@0: 0, 0, dy, 1, filtering); michael@0: return; michael@0: } michael@0: if (dst_width <= Abs(src_width) && dst_height <= src_height) { michael@0: // Scale down. michael@0: if (4 * dst_width == 3 * src_width && michael@0: 4 * dst_height == 3 * src_height) { michael@0: // optimized, 3/4 michael@0: ScalePlaneDown34(src_width, src_height, dst_width, dst_height, michael@0: src_stride, dst_stride, src, dst, filtering); michael@0: return; michael@0: } michael@0: if (2 * dst_width == src_width && 2 * dst_height == src_height) { michael@0: // optimized, 1/2 michael@0: ScalePlaneDown2(src_width, src_height, dst_width, dst_height, michael@0: src_stride, dst_stride, src, dst, filtering); michael@0: return; michael@0: } michael@0: // 3/8 rounded up for odd sized chroma height. michael@0: if (8 * dst_width == 3 * src_width && michael@0: dst_height == ((src_height * 3 + 7) / 8)) { michael@0: // optimized, 3/8 michael@0: ScalePlaneDown38(src_width, src_height, dst_width, dst_height, michael@0: src_stride, dst_stride, src, dst, filtering); michael@0: return; michael@0: } michael@0: if (4 * dst_width == src_width && 4 * dst_height == src_height && michael@0: filtering != kFilterBilinear) { michael@0: // optimized, 1/4 michael@0: ScalePlaneDown4(src_width, src_height, dst_width, dst_height, michael@0: src_stride, dst_stride, src, dst, filtering); michael@0: return; michael@0: } michael@0: } michael@0: if (filtering == kFilterBox && dst_height * 2 < src_height) { michael@0: ScalePlaneBox(src_width, src_height, dst_width, dst_height, michael@0: src_stride, dst_stride, src, dst); michael@0: return; michael@0: } michael@0: if (filtering && dst_height > src_height) { michael@0: ScalePlaneBilinearUp(src_width, src_height, dst_width, dst_height, michael@0: src_stride, dst_stride, src, dst, filtering); michael@0: return; michael@0: } michael@0: if (filtering) { michael@0: ScalePlaneBilinearDown(src_width, src_height, dst_width, dst_height, michael@0: src_stride, dst_stride, src, dst, filtering); michael@0: return; michael@0: } michael@0: ScalePlaneSimple(src_width, src_height, dst_width, dst_height, michael@0: src_stride, dst_stride, src, dst); michael@0: } michael@0: michael@0: // Scale an I420 image. michael@0: // This function in turn calls a scaling function for each plane. michael@0: michael@0: LIBYUV_API michael@0: int I420Scale(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: int src_width, int src_height, 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 dst_width, int dst_height, michael@0: enum FilterMode filtering) { michael@0: if (!src_y || !src_u || !src_v || src_width == 0 || src_height == 0 || michael@0: !dst_y || !dst_u || !dst_v || dst_width <= 0 || dst_height <= 0) { michael@0: return -1; michael@0: } michael@0: int src_halfwidth = SUBSAMPLE(src_width, 1, 1); michael@0: int src_halfheight = SUBSAMPLE(src_height, 1, 1); michael@0: int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); michael@0: int dst_halfheight = SUBSAMPLE(dst_height, 1, 1); michael@0: michael@0: ScalePlane(src_y, src_stride_y, src_width, src_height, michael@0: dst_y, dst_stride_y, dst_width, dst_height, michael@0: filtering); michael@0: ScalePlane(src_u, src_stride_u, src_halfwidth, src_halfheight, michael@0: dst_u, dst_stride_u, dst_halfwidth, dst_halfheight, michael@0: filtering); michael@0: ScalePlane(src_v, src_stride_v, src_halfwidth, src_halfheight, michael@0: dst_v, dst_stride_v, dst_halfwidth, dst_halfheight, michael@0: filtering); michael@0: return 0; michael@0: } michael@0: michael@0: // Deprecated api michael@0: LIBYUV_API michael@0: int Scale(const uint8* src_y, const uint8* src_u, const uint8* src_v, michael@0: int src_stride_y, int src_stride_u, int src_stride_v, michael@0: int src_width, int src_height, michael@0: uint8* dst_y, uint8* dst_u, uint8* dst_v, michael@0: int dst_stride_y, int dst_stride_u, int dst_stride_v, michael@0: int dst_width, int dst_height, michael@0: LIBYUV_BOOL interpolate) { michael@0: return I420Scale(src_y, src_stride_y, michael@0: src_u, src_stride_u, michael@0: src_v, src_stride_v, michael@0: src_width, src_height, michael@0: dst_y, dst_stride_y, michael@0: dst_u, dst_stride_u, michael@0: dst_v, dst_stride_v, michael@0: dst_width, dst_height, michael@0: interpolate ? kFilterBox : kFilterNone); michael@0: } michael@0: michael@0: // Deprecated api michael@0: LIBYUV_API michael@0: int ScaleOffset(const uint8* src, int src_width, int src_height, michael@0: uint8* dst, int dst_width, int dst_height, int dst_yoffset, michael@0: LIBYUV_BOOL interpolate) { michael@0: if (!src || src_width <= 0 || src_height <= 0 || michael@0: !dst || dst_width <= 0 || dst_height <= 0 || dst_yoffset < 0 || michael@0: dst_yoffset >= dst_height) { michael@0: return -1; michael@0: } michael@0: dst_yoffset = dst_yoffset & ~1; // chroma requires offset to multiple of 2. michael@0: int src_halfwidth = SUBSAMPLE(src_width, 1, 1); michael@0: int src_halfheight = SUBSAMPLE(src_height, 1, 1); michael@0: int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); michael@0: int dst_halfheight = SUBSAMPLE(dst_height, 1, 1); michael@0: int aheight = dst_height - dst_yoffset * 2; // actual output height michael@0: const uint8* src_y = src; michael@0: const uint8* src_u = src + src_width * src_height; michael@0: const uint8* src_v = src + src_width * src_height + michael@0: src_halfwidth * src_halfheight; michael@0: uint8* dst_y = dst + dst_yoffset * dst_width; michael@0: uint8* dst_u = dst + dst_width * dst_height + michael@0: (dst_yoffset >> 1) * dst_halfwidth; michael@0: uint8* dst_v = dst + dst_width * dst_height + dst_halfwidth * dst_halfheight + michael@0: (dst_yoffset >> 1) * dst_halfwidth; michael@0: return I420Scale(src_y, src_width, michael@0: src_u, src_halfwidth, michael@0: src_v, src_halfwidth, michael@0: src_width, src_height, michael@0: dst_y, dst_width, michael@0: dst_u, dst_halfwidth, michael@0: dst_v, dst_halfwidth, michael@0: dst_width, aheight, michael@0: interpolate ? kFilterBox : kFilterNone); michael@0: } michael@0: michael@0: #ifdef __cplusplus michael@0: } // extern "C" michael@0: } // namespace libyuv michael@0: #endif