1 /*
2 * Copyright 2012 The LibYuv Project Authors. All rights reserved.
3 *
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
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "libyuv/rotate.h"
12
13 #include "libyuv/convert.h"
14 #include "libyuv/cpu_id.h"
15 #include "libyuv/planar_functions.h"
16 #include "libyuv/row.h"
17 #include "libyuv/scale_row.h" /* for ScaleARGBRowDownEven_ */
18
19 #ifdef __cplusplus
20 namespace libyuv {
21 extern "C" {
22 #endif
23
ARGBTranspose(const uint8_t * src_argb,int src_stride_argb,uint8_t * dst_argb,int dst_stride_argb,int width,int height)24 static void ARGBTranspose(const uint8_t* src_argb,
25 int src_stride_argb,
26 uint8_t* dst_argb,
27 int dst_stride_argb,
28 int width,
29 int height) {
30 int i;
31 int src_pixel_step = src_stride_argb >> 2;
32 void (*ScaleARGBRowDownEven)(
33 const uint8_t* src_argb, ptrdiff_t src_stride_argb, int src_step,
34 uint8_t* dst_argb, int dst_width) = ScaleARGBRowDownEven_C;
35 #if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2)
36 if (TestCpuFlag(kCpuHasSSE2)) {
37 ScaleARGBRowDownEven = ScaleARGBRowDownEven_Any_SSE2;
38 if (IS_ALIGNED(height, 4)) { // Width of dest.
39 ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2;
40 }
41 }
42 #endif
43 #if defined(HAS_SCALEARGBROWDOWNEVEN_NEON)
44 if (TestCpuFlag(kCpuHasNEON)) {
45 ScaleARGBRowDownEven = ScaleARGBRowDownEven_Any_NEON;
46 if (IS_ALIGNED(height, 4)) { // Width of dest.
47 ScaleARGBRowDownEven = ScaleARGBRowDownEven_NEON;
48 }
49 }
50 #endif
51 #if defined(HAS_SCALEARGBROWDOWNEVEN_MSA)
52 if (TestCpuFlag(kCpuHasMSA)) {
53 ScaleARGBRowDownEven = ScaleARGBRowDownEven_Any_MSA;
54 if (IS_ALIGNED(height, 4)) { // Width of dest.
55 ScaleARGBRowDownEven = ScaleARGBRowDownEven_MSA;
56 }
57 }
58 #endif
59 #if defined(HAS_SCALEARGBROWDOWNEVEN_MMI)
60 if (TestCpuFlag(kCpuHasMMI)) {
61 ScaleARGBRowDownEven = ScaleARGBRowDownEven_Any_MMI;
62 if (IS_ALIGNED(height, 4)) { // Width of dest.
63 ScaleARGBRowDownEven = ScaleARGBRowDownEven_MMI;
64 }
65 }
66 #endif
67
68 for (i = 0; i < width; ++i) { // column of source to row of dest.
69 ScaleARGBRowDownEven(src_argb, 0, src_pixel_step, dst_argb, height);
70 dst_argb += dst_stride_argb;
71 src_argb += 4;
72 }
73 }
74
ARGBRotate90(const uint8_t * src_argb,int src_stride_argb,uint8_t * dst_argb,int dst_stride_argb,int width,int height)75 void ARGBRotate90(const uint8_t* src_argb,
76 int src_stride_argb,
77 uint8_t* dst_argb,
78 int dst_stride_argb,
79 int width,
80 int height) {
81 // Rotate by 90 is a ARGBTranspose with the source read
82 // from bottom to top. So set the source pointer to the end
83 // of the buffer and flip the sign of the source stride.
84 src_argb += src_stride_argb * (height - 1);
85 src_stride_argb = -src_stride_argb;
86 ARGBTranspose(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
87 height);
88 }
89
ARGBRotate270(const uint8_t * src_argb,int src_stride_argb,uint8_t * dst_argb,int dst_stride_argb,int width,int height)90 void ARGBRotate270(const uint8_t* src_argb,
91 int src_stride_argb,
92 uint8_t* dst_argb,
93 int dst_stride_argb,
94 int width,
95 int height) {
96 // Rotate by 270 is a ARGBTranspose with the destination written
97 // from bottom to top. So set the destination pointer to the end
98 // of the buffer and flip the sign of the destination stride.
99 dst_argb += dst_stride_argb * (width - 1);
100 dst_stride_argb = -dst_stride_argb;
101 ARGBTranspose(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
102 height);
103 }
104
ARGBRotate180(const uint8_t * src_argb,int src_stride_argb,uint8_t * dst_argb,int dst_stride_argb,int width,int height)105 void ARGBRotate180(const uint8_t* src_argb,
106 int src_stride_argb,
107 uint8_t* dst_argb,
108 int dst_stride_argb,
109 int width,
110 int height) {
111 // Swap first and last row and mirror the content. Uses a temporary row.
112 align_buffer_64(row, width * 4);
113 const uint8_t* src_bot = src_argb + src_stride_argb * (height - 1);
114 uint8_t* dst_bot = dst_argb + dst_stride_argb * (height - 1);
115 int half_height = (height + 1) >> 1;
116 int y;
117 void (*ARGBMirrorRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) =
118 ARGBMirrorRow_C;
119 void (*CopyRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) =
120 CopyRow_C;
121 #if defined(HAS_ARGBMIRRORROW_NEON)
122 if (TestCpuFlag(kCpuHasNEON)) {
123 ARGBMirrorRow = ARGBMirrorRow_Any_NEON;
124 if (IS_ALIGNED(width, 4)) {
125 ARGBMirrorRow = ARGBMirrorRow_NEON;
126 }
127 }
128 #endif
129 #if defined(HAS_ARGBMIRRORROW_SSE2)
130 if (TestCpuFlag(kCpuHasSSE2)) {
131 ARGBMirrorRow = ARGBMirrorRow_Any_SSE2;
132 if (IS_ALIGNED(width, 4)) {
133 ARGBMirrorRow = ARGBMirrorRow_SSE2;
134 }
135 }
136 #endif
137 #if defined(HAS_ARGBMIRRORROW_AVX2)
138 if (TestCpuFlag(kCpuHasAVX2)) {
139 ARGBMirrorRow = ARGBMirrorRow_Any_AVX2;
140 if (IS_ALIGNED(width, 8)) {
141 ARGBMirrorRow = ARGBMirrorRow_AVX2;
142 }
143 }
144 #endif
145 #if defined(HAS_ARGBMIRRORROW_MSA)
146 if (TestCpuFlag(kCpuHasMSA)) {
147 ARGBMirrorRow = ARGBMirrorRow_Any_MSA;
148 if (IS_ALIGNED(width, 16)) {
149 ARGBMirrorRow = ARGBMirrorRow_MSA;
150 }
151 }
152 #endif
153 #if defined(HAS_ARGBMIRRORROW_MMI)
154 if (TestCpuFlag(kCpuHasMMI)) {
155 ARGBMirrorRow = ARGBMirrorRow_Any_MMI;
156 if (IS_ALIGNED(width, 2)) {
157 ARGBMirrorRow = ARGBMirrorRow_MMI;
158 }
159 }
160 #endif
161 #if defined(HAS_COPYROW_SSE2)
162 if (TestCpuFlag(kCpuHasSSE2)) {
163 CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2;
164 }
165 #endif
166 #if defined(HAS_COPYROW_AVX)
167 if (TestCpuFlag(kCpuHasAVX)) {
168 CopyRow = IS_ALIGNED(width * 4, 64) ? CopyRow_AVX : CopyRow_Any_AVX;
169 }
170 #endif
171 #if defined(HAS_COPYROW_ERMS)
172 if (TestCpuFlag(kCpuHasERMS)) {
173 CopyRow = CopyRow_ERMS;
174 }
175 #endif
176 #if defined(HAS_COPYROW_NEON)
177 if (TestCpuFlag(kCpuHasNEON)) {
178 CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_NEON : CopyRow_Any_NEON;
179 }
180 #endif
181
182 // Odd height will harmlessly mirror the middle row twice.
183 for (y = 0; y < half_height; ++y) {
184 ARGBMirrorRow(src_argb, row, width); // Mirror first row into a buffer
185 ARGBMirrorRow(src_bot, dst_argb, width); // Mirror last row into first row
186 CopyRow(row, dst_bot, width * 4); // Copy first mirrored row into last
187 src_argb += src_stride_argb;
188 dst_argb += dst_stride_argb;
189 src_bot -= src_stride_argb;
190 dst_bot -= dst_stride_argb;
191 }
192 free_aligned_buffer_64(row);
193 }
194
195 LIBYUV_API
ARGBRotate(const uint8_t * src_argb,int src_stride_argb,uint8_t * dst_argb,int dst_stride_argb,int width,int height,enum RotationMode mode)196 int ARGBRotate(const uint8_t* src_argb,
197 int src_stride_argb,
198 uint8_t* dst_argb,
199 int dst_stride_argb,
200 int width,
201 int height,
202 enum RotationMode mode) {
203 if (!src_argb || width <= 0 || height == 0 || !dst_argb) {
204 return -1;
205 }
206
207 // Negative height means invert the image.
208 if (height < 0) {
209 height = -height;
210 src_argb = src_argb + (height - 1) * src_stride_argb;
211 src_stride_argb = -src_stride_argb;
212 }
213
214 switch (mode) {
215 case kRotate0:
216 // copy frame
217 return ARGBCopy(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
218 width, height);
219 case kRotate90:
220 ARGBRotate90(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
221 height);
222 return 0;
223 case kRotate270:
224 ARGBRotate270(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
225 height);
226 return 0;
227 case kRotate180:
228 ARGBRotate180(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
229 height);
230 return 0;
231 default:
232 break;
233 }
234 return -1;
235 }
236
237 #ifdef __cplusplus
238 } // extern "C"
239 } // namespace libyuv
240 #endif
241