blob: 02f928ecdb1ea34c7f32ef690cae00b753fd5874 [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.
25#if !defined(YUV_DISABLE_ASM) && (defined(_M_IX86) || \
26 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.comcb5262d2012-11-16 01:41:35 +000032#if !defined(YUV_DISABLE_ASM) && (defined(__ARM_NEON__) || defined(LIBYUV_NEON))
33#define HAS_SCALEARGBROWDOWNEVEN_NEON
34void ScaleARGBRowDownEven_NEON(const uint8* src_ptr, int src_stride,
35 int src_stepx,
36 uint8* dst_ptr, int dst_width);
37#endif
38
fbarchard@google.comd51c3422012-06-26 23:46:25 +000039void ScaleARGBRowDownEven_C(const uint8* src_ptr, int,
40 int src_stepx,
41 uint8* dst_ptr, int dst_width);
42
43static void ARGBTranspose(const uint8* src, int src_stride,
44 uint8* dst, int dst_stride,
45 int width, int height) {
fbarchard@google.comcb5262d2012-11-16 01:41:35 +000046 int src_pixel_step = src_stride >> 2;
fbarchard@google.comd51c3422012-06-26 23:46:25 +000047 void (*ScaleARGBRowDownEven)(const uint8* src_ptr, int src_stride,
48 int src_step, uint8* dst_ptr, int dst_width) = ScaleARGBRowDownEven_C;
49#if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2)
fbarchard@google.com3e464442012-11-14 02:03:49 +000050 if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(height, 4) && // Width of dest.
fbarchard@google.comd51c3422012-06-26 23:46:25 +000051 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
52 ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2;
53 }
fbarchard@google.comcb5262d2012-11-16 01:41:35 +000054#elif defined(HAS_SCALEARGBROWDOWNEVEN_NEON)
55 if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(height, 4) && // Width of dest.
56 IS_ALIGNED(src, 4)) {
57 ScaleARGBRowDownEven = ScaleARGBRowDownEven_NEON;
58 }
fbarchard@google.comd51c3422012-06-26 23:46:25 +000059#endif
60
fbarchard@google.comd51c3422012-06-26 23:46:25 +000061 for (int i = 0; i < width; ++i) { // column of source to row of dest.
62 ScaleARGBRowDownEven(src, 0, src_pixel_step, dst, height);
63 dst += dst_stride;
64 src += 4;
65 }
66}
67
68void ARGBRotate90(const uint8* src, int src_stride,
69 uint8* dst, int dst_stride,
70 int width, int height) {
71 // Rotate by 90 is a ARGBTranspose with the source read
fbarchard@google.com64ce0ab2012-10-09 00:05:29 +000072 // from bottom to top. So set the source pointer to the end
fbarchard@google.comd51c3422012-06-26 23:46:25 +000073 // of the buffer and flip the sign of the source stride.
74 src += src_stride * (height - 1);
75 src_stride = -src_stride;
76 ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
77}
78
79void ARGBRotate270(const uint8* src, int src_stride,
80 uint8* dst, int dst_stride,
81 int width, int height) {
82 // Rotate by 270 is a ARGBTranspose with the destination written
fbarchard@google.com64ce0ab2012-10-09 00:05:29 +000083 // from bottom to top. So set the destination pointer to the end
fbarchard@google.comd51c3422012-06-26 23:46:25 +000084 // of the buffer and flip the sign of the destination stride.
85 dst += dst_stride * (width - 1);
86 dst_stride = -dst_stride;
87 ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
88}
89
90void ARGBRotate180(const uint8* src, int src_stride,
91 uint8* dst, int dst_stride,
92 int width, int height) {
93 void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) =
94 ARGBMirrorRow_C;
95#if defined(HAS_ARGBMIRRORROW_SSSE3)
96 if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4) &&
97 IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) &&
98 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
99 ARGBMirrorRow = ARGBMirrorRow_SSSE3;
100 }
fbarchard@google.com3e464442012-11-14 02:03:49 +0000101#elif defined(HAS_ARGBMIRRORROW_NEON)
102 if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 4)) {
103 ARGBMirrorRow = ARGBMirrorRow_NEON;
104 }
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000105#endif
106 void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C;
107#if defined(HAS_COPYROW_NEON)
fbarchard@google.com62a961b2012-10-22 17:24:50 +0000108 if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width * 4, 32)) {
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000109 CopyRow = CopyRow_NEON;
110 }
111#endif
112#if defined(HAS_COPYROW_X86)
113 if (TestCpuFlag(kCpuHasX86)) {
114 CopyRow = CopyRow_X86;
115 }
116#endif
117#if defined(HAS_COPYROW_SSE2)
118 if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width * 4, 32) &&
119 IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) &&
120 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
121 CopyRow = CopyRow_SSE2;
122 }
123#endif
fbarchard@google.com92352b72013-01-28 19:43:29 +0000124#if defined(HAS_COPYROW_MIPS)
125 if (TestCpuFlag(kCpuHasMIPS)) {
126 CopyRow = CopyRow_MIPS;
127 }
128#endif
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000129 if (width * 4 > kMaxStride) {
130 return;
131 }
fbarchard@google.com64ce0ab2012-10-09 00:05:29 +0000132 // Swap first and last row and mirror the content. Uses a temporary row.
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000133 SIMD_ALIGNED(uint8 row[kMaxStride]);
134 const uint8* src_bot = src + src_stride * (height - 1);
135 uint8* dst_bot = dst + dst_stride * (height - 1);
136 int half_height = (height + 1) >> 1;
137 // Odd height will harmlessly mirror the middle row twice.
138 for (int y = 0; y < half_height; ++y) {
139 ARGBMirrorRow(src, row, width); // Mirror first row into a buffer
140 src += src_stride;
141 ARGBMirrorRow(src_bot, dst, width); // Mirror last row into first row
142 dst += dst_stride;
143 CopyRow(row, dst_bot, width * 4); // Copy first mirrored row into last
144 src_bot -= src_stride;
145 dst_bot -= dst_stride;
146 }
147}
148
fbarchard@google.comfc7314e2012-09-27 02:17:51 +0000149LIBYUV_API
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000150int ARGBRotate(const uint8* src_argb, int src_stride_argb,
151 uint8* dst_argb, int dst_stride_argb,
152 int width, int height,
153 RotationMode mode) {
154 if (!src_argb || width <= 0 || height == 0 || !dst_argb) {
155 return -1;
156 }
157
158 // Negative height means invert the image.
159 if (height < 0) {
160 height = -height;
161 src_argb = src_argb + (height - 1) * src_stride_argb;
162 src_stride_argb = -src_stride_argb;
163 }
164
165 switch (mode) {
166 case kRotate0:
167 // copy frame
168 return ARGBCopy(src_argb, src_stride_argb,
169 dst_argb, dst_stride_argb,
170 width, height);
171 case kRotate90:
172 ARGBRotate90(src_argb, src_stride_argb,
173 dst_argb, dst_stride_argb,
174 width, height);
175 return 0;
176 case kRotate270:
177 ARGBRotate270(src_argb, src_stride_argb,
178 dst_argb, dst_stride_argb,
179 width, height);
180 return 0;
181 case kRotate180:
182 ARGBRotate180(src_argb, src_stride_argb,
183 dst_argb, dst_stride_argb,
184 width, height);
185 return 0;
186 default:
187 break;
188 }
189 return -1;
190}
191
192#ifdef __cplusplus
193} // extern "C"
194} // namespace libyuv
195#endif