Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2012 The LibYuv Project Authors. All rights reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "libyuv/rotate.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "libyuv/cpu_id.h" |
michael@0 | 14 | #include "libyuv/convert.h" |
michael@0 | 15 | #include "libyuv/planar_functions.h" |
michael@0 | 16 | #include "libyuv/row.h" |
michael@0 | 17 | |
michael@0 | 18 | #ifdef __cplusplus |
michael@0 | 19 | namespace libyuv { |
michael@0 | 20 | extern "C" { |
michael@0 | 21 | #endif |
michael@0 | 22 | |
michael@0 | 23 | // ARGBScale has a function to copy pixels to a row, striding each source |
michael@0 | 24 | // pixel by a constant. |
michael@0 | 25 | #if !defined(LIBYUV_DISABLE_X86) && \ |
michael@0 | 26 | (defined(_M_IX86) || \ |
michael@0 | 27 | (defined(__x86_64__) && !defined(__native_client__)) || defined(__i386__)) |
michael@0 | 28 | #define HAS_SCALEARGBROWDOWNEVEN_SSE2 |
michael@0 | 29 | void ScaleARGBRowDownEven_SSE2(const uint8* src_ptr, int src_stride, |
michael@0 | 30 | int src_stepx, |
michael@0 | 31 | uint8* dst_ptr, int dst_width); |
michael@0 | 32 | #endif |
michael@0 | 33 | #if !defined(LIBYUV_DISABLE_NEON) && !defined(__native_client__) && \ |
michael@0 | 34 | (defined(__ARM_NEON__) || defined(LIBYUV_NEON)) |
michael@0 | 35 | #define HAS_SCALEARGBROWDOWNEVEN_NEON |
michael@0 | 36 | void ScaleARGBRowDownEven_NEON(const uint8* src_ptr, int src_stride, |
michael@0 | 37 | int src_stepx, |
michael@0 | 38 | uint8* dst_ptr, int dst_width); |
michael@0 | 39 | #endif |
michael@0 | 40 | |
michael@0 | 41 | void ScaleARGBRowDownEven_C(const uint8* src_ptr, int, |
michael@0 | 42 | int src_stepx, |
michael@0 | 43 | uint8* dst_ptr, int dst_width); |
michael@0 | 44 | |
michael@0 | 45 | static void ARGBTranspose(const uint8* src, int src_stride, |
michael@0 | 46 | uint8* dst, int dst_stride, |
michael@0 | 47 | int width, int height) { |
michael@0 | 48 | int i; |
michael@0 | 49 | int src_pixel_step = src_stride >> 2; |
michael@0 | 50 | void (*ScaleARGBRowDownEven)(const uint8* src_ptr, int src_stride, |
michael@0 | 51 | int src_step, uint8* dst_ptr, int dst_width) = ScaleARGBRowDownEven_C; |
michael@0 | 52 | #if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2) |
michael@0 | 53 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(height, 4) && // Width of dest. |
michael@0 | 54 | IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) { |
michael@0 | 55 | ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2; |
michael@0 | 56 | } |
michael@0 | 57 | #elif defined(HAS_SCALEARGBROWDOWNEVEN_NEON) |
michael@0 | 58 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(height, 4) && // Width of dest. |
michael@0 | 59 | IS_ALIGNED(src, 4)) { |
michael@0 | 60 | ScaleARGBRowDownEven = ScaleARGBRowDownEven_NEON; |
michael@0 | 61 | } |
michael@0 | 62 | #endif |
michael@0 | 63 | |
michael@0 | 64 | for (i = 0; i < width; ++i) { // column of source to row of dest. |
michael@0 | 65 | ScaleARGBRowDownEven(src, 0, src_pixel_step, dst, height); |
michael@0 | 66 | dst += dst_stride; |
michael@0 | 67 | src += 4; |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | void ARGBRotate90(const uint8* src, int src_stride, |
michael@0 | 72 | uint8* dst, int dst_stride, |
michael@0 | 73 | int width, int height) { |
michael@0 | 74 | // Rotate by 90 is a ARGBTranspose with the source read |
michael@0 | 75 | // from bottom to top. So set the source pointer to the end |
michael@0 | 76 | // of the buffer and flip the sign of the source stride. |
michael@0 | 77 | src += src_stride * (height - 1); |
michael@0 | 78 | src_stride = -src_stride; |
michael@0 | 79 | ARGBTranspose(src, src_stride, dst, dst_stride, width, height); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | void ARGBRotate270(const uint8* src, int src_stride, |
michael@0 | 83 | uint8* dst, int dst_stride, |
michael@0 | 84 | int width, int height) { |
michael@0 | 85 | // Rotate by 270 is a ARGBTranspose with the destination written |
michael@0 | 86 | // from bottom to top. So set the destination pointer to the end |
michael@0 | 87 | // of the buffer and flip the sign of the destination stride. |
michael@0 | 88 | dst += dst_stride * (width - 1); |
michael@0 | 89 | dst_stride = -dst_stride; |
michael@0 | 90 | ARGBTranspose(src, src_stride, dst, dst_stride, width, height); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | void ARGBRotate180(const uint8* src, int src_stride, |
michael@0 | 94 | uint8* dst, int dst_stride, |
michael@0 | 95 | int width, int height) { |
michael@0 | 96 | // Swap first and last row and mirror the content. Uses a temporary row. |
michael@0 | 97 | align_buffer_64(row, width * 4); |
michael@0 | 98 | const uint8* src_bot = src + src_stride * (height - 1); |
michael@0 | 99 | uint8* dst_bot = dst + dst_stride * (height - 1); |
michael@0 | 100 | int half_height = (height + 1) >> 1; |
michael@0 | 101 | int y; |
michael@0 | 102 | void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) = |
michael@0 | 103 | ARGBMirrorRow_C; |
michael@0 | 104 | void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C; |
michael@0 | 105 | #if defined(HAS_ARGBMIRRORROW_SSSE3) |
michael@0 | 106 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4) && |
michael@0 | 107 | IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) && |
michael@0 | 108 | IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) { |
michael@0 | 109 | ARGBMirrorRow = ARGBMirrorRow_SSSE3; |
michael@0 | 110 | } |
michael@0 | 111 | #endif |
michael@0 | 112 | #if defined(HAS_ARGBMIRRORROW_AVX2) |
michael@0 | 113 | if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 8)) { |
michael@0 | 114 | ARGBMirrorRow = ARGBMirrorRow_AVX2; |
michael@0 | 115 | } |
michael@0 | 116 | #endif |
michael@0 | 117 | #if defined(HAS_ARGBMIRRORROW_NEON) |
michael@0 | 118 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 4)) { |
michael@0 | 119 | ARGBMirrorRow = ARGBMirrorRow_NEON; |
michael@0 | 120 | } |
michael@0 | 121 | #endif |
michael@0 | 122 | #if defined(HAS_COPYROW_NEON) |
michael@0 | 123 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width * 4, 32)) { |
michael@0 | 124 | CopyRow = CopyRow_NEON; |
michael@0 | 125 | } |
michael@0 | 126 | #endif |
michael@0 | 127 | #if defined(HAS_COPYROW_X86) |
michael@0 | 128 | if (TestCpuFlag(kCpuHasX86)) { |
michael@0 | 129 | CopyRow = CopyRow_X86; |
michael@0 | 130 | } |
michael@0 | 131 | #endif |
michael@0 | 132 | #if defined(HAS_COPYROW_SSE2) |
michael@0 | 133 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width * 4, 32) && |
michael@0 | 134 | IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) && |
michael@0 | 135 | IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) { |
michael@0 | 136 | CopyRow = CopyRow_SSE2; |
michael@0 | 137 | } |
michael@0 | 138 | #endif |
michael@0 | 139 | #if defined(HAS_COPYROW_ERMS) |
michael@0 | 140 | if (TestCpuFlag(kCpuHasERMS)) { |
michael@0 | 141 | CopyRow = CopyRow_ERMS; |
michael@0 | 142 | } |
michael@0 | 143 | #endif |
michael@0 | 144 | #if defined(HAS_COPYROW_MIPS) |
michael@0 | 145 | if (TestCpuFlag(kCpuHasMIPS)) { |
michael@0 | 146 | CopyRow = CopyRow_MIPS; |
michael@0 | 147 | } |
michael@0 | 148 | #endif |
michael@0 | 149 | |
michael@0 | 150 | // Odd height will harmlessly mirror the middle row twice. |
michael@0 | 151 | for (y = 0; y < half_height; ++y) { |
michael@0 | 152 | ARGBMirrorRow(src, row, width); // Mirror first row into a buffer |
michael@0 | 153 | ARGBMirrorRow(src_bot, dst, width); // Mirror last row into first row |
michael@0 | 154 | CopyRow(row, dst_bot, width * 4); // Copy first mirrored row into last |
michael@0 | 155 | src += src_stride; |
michael@0 | 156 | dst += dst_stride; |
michael@0 | 157 | src_bot -= src_stride; |
michael@0 | 158 | dst_bot -= dst_stride; |
michael@0 | 159 | } |
michael@0 | 160 | free_aligned_buffer_64(row); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | LIBYUV_API |
michael@0 | 164 | int ARGBRotate(const uint8* src_argb, int src_stride_argb, |
michael@0 | 165 | uint8* dst_argb, int dst_stride_argb, |
michael@0 | 166 | int width, int height, |
michael@0 | 167 | enum RotationMode mode) { |
michael@0 | 168 | if (!src_argb || width <= 0 || height == 0 || !dst_argb) { |
michael@0 | 169 | return -1; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | // Negative height means invert the image. |
michael@0 | 173 | if (height < 0) { |
michael@0 | 174 | height = -height; |
michael@0 | 175 | src_argb = src_argb + (height - 1) * src_stride_argb; |
michael@0 | 176 | src_stride_argb = -src_stride_argb; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | switch (mode) { |
michael@0 | 180 | case kRotate0: |
michael@0 | 181 | // copy frame |
michael@0 | 182 | return ARGBCopy(src_argb, src_stride_argb, |
michael@0 | 183 | dst_argb, dst_stride_argb, |
michael@0 | 184 | width, height); |
michael@0 | 185 | case kRotate90: |
michael@0 | 186 | ARGBRotate90(src_argb, src_stride_argb, |
michael@0 | 187 | dst_argb, dst_stride_argb, |
michael@0 | 188 | width, height); |
michael@0 | 189 | return 0; |
michael@0 | 190 | case kRotate270: |
michael@0 | 191 | ARGBRotate270(src_argb, src_stride_argb, |
michael@0 | 192 | dst_argb, dst_stride_argb, |
michael@0 | 193 | width, height); |
michael@0 | 194 | return 0; |
michael@0 | 195 | case kRotate180: |
michael@0 | 196 | ARGBRotate180(src_argb, src_stride_argb, |
michael@0 | 197 | dst_argb, dst_stride_argb, |
michael@0 | 198 | width, height); |
michael@0 | 199 | return 0; |
michael@0 | 200 | default: |
michael@0 | 201 | break; |
michael@0 | 202 | } |
michael@0 | 203 | return -1; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | #ifdef __cplusplus |
michael@0 | 207 | } // extern "C" |
michael@0 | 208 | } // namespace libyuv |
michael@0 | 209 | #endif |