blob: cccfb9b481ee1bf3745f3d435efbdbe7fe199e79 [file] [log] [blame]
fbarchard@google.comd51c3422012-06-26 23:46:25 +00001/*
fbarchard@google.comb0c97972012-08-08 19:04:24 +00002 * Copyright 2012 The LibYuv Project Authors. All rights reserved.
fbarchard@google.comd51c3422012-06-26 23:46:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
fbarchard@google.comcde58702013-01-28 00:02:35 +00007 * in the file PATENTS. All contributing project authors may
fbarchard@google.comd51c3422012-06-26 23:46:25 +00008 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "libyuv/rotate.h"
12
13#include "libyuv/cpu_id.h"
14#include "libyuv/convert.h"
15#include "libyuv/planar_functions.h"
fbarchard@google.com142f6c42012-09-18 20:56:51 +000016#include "libyuv/row.h"
fbarchard@google.comd51c3422012-06-26 23:46:25 +000017
18#ifdef __cplusplus
19namespace libyuv {
20extern "C" {
21#endif
22
23// ARGBScale has a function to copy pixels to a row, striding each source
24// pixel by a constant.
fbarchard@google.com83a63e62013-02-27 00:20:29 +000025#if !defined(LIBYUV_DISABLE_X86) && (defined(_M_IX86) || \
fbarchard@google.comd51c3422012-06-26 23:46:25 +000026 defined(__x86_64__) || defined(__i386__))
27#define HAS_SCALEARGBROWDOWNEVEN_SSE2
28void ScaleARGBRowDownEven_SSE2(const uint8* src_ptr, int src_stride,
29 int src_stepx,
30 uint8* dst_ptr, int dst_width);
31#endif
fbarchard@google.com83a63e62013-02-27 00:20:29 +000032#if !defined(LIBYUV_DISABLE_NEON) && \
33 (defined(__ARM_NEON__) || defined(LIBYUV_NEON))
fbarchard@google.comcb5262d2012-11-16 01:41:35 +000034#define HAS_SCALEARGBROWDOWNEVEN_NEON
35void ScaleARGBRowDownEven_NEON(const uint8* src_ptr, int src_stride,
36 int src_stepx,
37 uint8* dst_ptr, int dst_width);
38#endif
39
fbarchard@google.comd51c3422012-06-26 23:46:25 +000040void ScaleARGBRowDownEven_C(const uint8* src_ptr, int,
41 int src_stepx,
42 uint8* dst_ptr, int dst_width);
43
44static void ARGBTranspose(const uint8* src, int src_stride,
45 uint8* dst, int dst_stride,
46 int width, int height) {
fbarchard@google.comcb5262d2012-11-16 01:41:35 +000047 int src_pixel_step = src_stride >> 2;
fbarchard@google.comd51c3422012-06-26 23:46:25 +000048 void (*ScaleARGBRowDownEven)(const uint8* src_ptr, int src_stride,
49 int src_step, uint8* dst_ptr, int dst_width) = ScaleARGBRowDownEven_C;
50#if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2)
fbarchard@google.com3e464442012-11-14 02:03:49 +000051 if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(height, 4) && // Width of dest.
fbarchard@google.comd51c3422012-06-26 23:46:25 +000052 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
53 ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2;
54 }
fbarchard@google.comcb5262d2012-11-16 01:41:35 +000055#elif defined(HAS_SCALEARGBROWDOWNEVEN_NEON)
56 if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(height, 4) && // Width of dest.
57 IS_ALIGNED(src, 4)) {
58 ScaleARGBRowDownEven = ScaleARGBRowDownEven_NEON;
59 }
fbarchard@google.comd51c3422012-06-26 23:46:25 +000060#endif
61
fbarchard@google.comd51c3422012-06-26 23:46:25 +000062 for (int i = 0; i < width; ++i) { // column of source to row of dest.
63 ScaleARGBRowDownEven(src, 0, src_pixel_step, dst, height);
64 dst += dst_stride;
65 src += 4;
66 }
67}
68
69void ARGBRotate90(const uint8* src, int src_stride,
70 uint8* dst, int dst_stride,
71 int width, int height) {
72 // Rotate by 90 is a ARGBTranspose with the source read
fbarchard@google.com64ce0ab2012-10-09 00:05:29 +000073 // from bottom to top. So set the source pointer to the end
fbarchard@google.comd51c3422012-06-26 23:46:25 +000074 // of the buffer and flip the sign of the source stride.
75 src += src_stride * (height - 1);
76 src_stride = -src_stride;
77 ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
78}
79
80void ARGBRotate270(const uint8* src, int src_stride,
81 uint8* dst, int dst_stride,
82 int width, int height) {
83 // Rotate by 270 is a ARGBTranspose with the destination written
fbarchard@google.com64ce0ab2012-10-09 00:05:29 +000084 // from bottom to top. So set the destination pointer to the end
fbarchard@google.comd51c3422012-06-26 23:46:25 +000085 // of the buffer and flip the sign of the destination stride.
86 dst += dst_stride * (width - 1);
87 dst_stride = -dst_stride;
88 ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
89}
90
91void ARGBRotate180(const uint8* src, int src_stride,
92 uint8* dst, int dst_stride,
93 int width, int height) {
94 void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) =
95 ARGBMirrorRow_C;
96#if defined(HAS_ARGBMIRRORROW_SSSE3)
97 if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4) &&
98 IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) &&
99 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
100 ARGBMirrorRow = ARGBMirrorRow_SSSE3;
101 }
fbarchard@google.com51398e02013-03-06 00:57:48 +0000102#endif
103#if defined(HAS_ARGBMIRRORROW_AVX2)
104 bool clear = false;
105 if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 8)) {
106 clear = true;
107 ARGBMirrorRow = ARGBMirrorRow_AVX2;
108 }
109#endif
110#if defined(HAS_ARGBMIRRORROW_NEON)
fbarchard@google.com3e464442012-11-14 02:03:49 +0000111 if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 4)) {
112 ARGBMirrorRow = ARGBMirrorRow_NEON;
113 }
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000114#endif
115 void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C;
116#if defined(HAS_COPYROW_NEON)
fbarchard@google.com62a961b2012-10-22 17:24:50 +0000117 if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width * 4, 32)) {
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000118 CopyRow = CopyRow_NEON;
119 }
120#endif
121#if defined(HAS_COPYROW_X86)
122 if (TestCpuFlag(kCpuHasX86)) {
123 CopyRow = CopyRow_X86;
124 }
125#endif
126#if defined(HAS_COPYROW_SSE2)
127 if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width * 4, 32) &&
128 IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) &&
129 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
130 CopyRow = CopyRow_SSE2;
131 }
132#endif
fbarchard@google.com2007dca2013-03-05 22:40:55 +0000133#if defined(HAS_COPYROW_AVX2)
134 // TODO(fbarchard): Detect Fast String support.
135 if (TestCpuFlag(kCpuHasAVX2)) {
136 CopyRow = CopyRow_AVX2;
137 }
138#endif
fbarchard@google.com92352b72013-01-28 19:43:29 +0000139#if defined(HAS_COPYROW_MIPS)
140 if (TestCpuFlag(kCpuHasMIPS)) {
141 CopyRow = CopyRow_MIPS;
142 }
143#endif
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000144 if (width * 4 > kMaxStride) {
145 return;
146 }
fbarchard@google.com64ce0ab2012-10-09 00:05:29 +0000147 // Swap first and last row and mirror the content. Uses a temporary row.
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000148 SIMD_ALIGNED(uint8 row[kMaxStride]);
149 const uint8* src_bot = src + src_stride * (height - 1);
150 uint8* dst_bot = dst + dst_stride * (height - 1);
151 int half_height = (height + 1) >> 1;
152 // Odd height will harmlessly mirror the middle row twice.
153 for (int y = 0; y < half_height; ++y) {
154 ARGBMirrorRow(src, row, width); // Mirror first row into a buffer
155 src += src_stride;
156 ARGBMirrorRow(src_bot, dst, width); // Mirror last row into first row
157 dst += dst_stride;
158 CopyRow(row, dst_bot, width * 4); // Copy first mirrored row into last
159 src_bot -= src_stride;
160 dst_bot -= dst_stride;
161 }
fbarchard@google.com51398e02013-03-06 00:57:48 +0000162#if defined(HAS_ARGBMIRRORROW_AVX2)
163 if (clear) {
164 __asm vzeroupper;
165 }
166#endif
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000167}
168
fbarchard@google.comfc7314e2012-09-27 02:17:51 +0000169LIBYUV_API
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000170int ARGBRotate(const uint8* src_argb, int src_stride_argb,
171 uint8* dst_argb, int dst_stride_argb,
172 int width, int height,
173 RotationMode mode) {
174 if (!src_argb || width <= 0 || height == 0 || !dst_argb) {
175 return -1;
176 }
177
178 // Negative height means invert the image.
179 if (height < 0) {
180 height = -height;
181 src_argb = src_argb + (height - 1) * src_stride_argb;
182 src_stride_argb = -src_stride_argb;
183 }
184
185 switch (mode) {
186 case kRotate0:
187 // copy frame
188 return ARGBCopy(src_argb, src_stride_argb,
189 dst_argb, dst_stride_argb,
190 width, height);
191 case kRotate90:
192 ARGBRotate90(src_argb, src_stride_argb,
193 dst_argb, dst_stride_argb,
194 width, height);
195 return 0;
196 case kRotate270:
197 ARGBRotate270(src_argb, src_stride_argb,
198 dst_argb, dst_stride_argb,
199 width, height);
200 return 0;
201 case kRotate180:
202 ARGBRotate180(src_argb, src_stride_argb,
203 dst_argb, dst_stride_argb,
204 width, height);
205 return 0;
206 default:
207 break;
208 }
209 return -1;
210}
211
212#ifdef __cplusplus
213} // extern "C"
214} // namespace libyuv
215#endif