blob: 2202e3b7d068a7d82b5248d0d1be503c25dcf2d4 [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
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/cpu_id.h"
14#include "libyuv/convert.h"
15#include "libyuv/planar_functions.h"
16#include "source/row.h"
17
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
32void ScaleARGBRowDownEven_C(const uint8* src_ptr, int,
33 int src_stepx,
34 uint8* dst_ptr, int dst_width);
35
36static void ARGBTranspose(const uint8* src, int src_stride,
37 uint8* dst, int dst_stride,
38 int width, int height) {
39 void (*ScaleARGBRowDownEven)(const uint8* src_ptr, int src_stride,
40 int src_step, uint8* dst_ptr, int dst_width) = ScaleARGBRowDownEven_C;
41#if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2)
42 if (TestCpuFlag(kCpuHasSSE2) &&
43 IS_ALIGNED(height, 4) && // width of dest.
44 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
45 ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2;
46 }
47#endif
48
49 int src_pixel_step = src_stride / 4;
50 for (int i = 0; i < width; ++i) { // column of source to row of dest.
51 ScaleARGBRowDownEven(src, 0, src_pixel_step, dst, height);
52 dst += dst_stride;
53 src += 4;
54 }
55}
56
57void ARGBRotate90(const uint8* src, int src_stride,
58 uint8* dst, int dst_stride,
59 int width, int height) {
60 // Rotate by 90 is a ARGBTranspose with the source read
61 // from bottom to top. So set the source pointer to the end
62 // of the buffer and flip the sign of the source stride.
63 src += src_stride * (height - 1);
64 src_stride = -src_stride;
65 ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
66}
67
68void ARGBRotate270(const uint8* src, int src_stride,
69 uint8* dst, int dst_stride,
70 int width, int height) {
71 // Rotate by 270 is a ARGBTranspose with the destination written
72 // from bottom to top. So set the destination pointer to the end
73 // of the buffer and flip the sign of the destination stride.
74 dst += dst_stride * (width - 1);
75 dst_stride = -dst_stride;
76 ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
77}
78
79void ARGBRotate180(const uint8* src, int src_stride,
80 uint8* dst, int dst_stride,
81 int width, int height) {
82 void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) =
83 ARGBMirrorRow_C;
84#if defined(HAS_ARGBMIRRORROW_SSSE3)
85 if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4) &&
86 IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) &&
87 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
88 ARGBMirrorRow = ARGBMirrorRow_SSSE3;
89 }
90#endif
91 void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C;
92#if defined(HAS_COPYROW_NEON)
93 if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width * 4, 64)) {
94 CopyRow = CopyRow_NEON;
95 }
96#endif
97#if defined(HAS_COPYROW_X86)
98 if (TestCpuFlag(kCpuHasX86)) {
99 CopyRow = CopyRow_X86;
100 }
101#endif
102#if defined(HAS_COPYROW_SSE2)
103 if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width * 4, 32) &&
104 IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) &&
105 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
106 CopyRow = CopyRow_SSE2;
107 }
108#endif
109 if (width * 4 > kMaxStride) {
110 return;
111 }
112 // Swap first and last row and mirror the content. Uses a temporary row.
113 SIMD_ALIGNED(uint8 row[kMaxStride]);
114 const uint8* src_bot = src + src_stride * (height - 1);
115 uint8* dst_bot = dst + dst_stride * (height - 1);
116 int half_height = (height + 1) >> 1;
117 // Odd height will harmlessly mirror the middle row twice.
118 for (int y = 0; y < half_height; ++y) {
119 ARGBMirrorRow(src, row, width); // Mirror first row into a buffer
120 src += src_stride;
121 ARGBMirrorRow(src_bot, dst, width); // Mirror last row into first row
122 dst += dst_stride;
123 CopyRow(row, dst_bot, width * 4); // Copy first mirrored row into last
124 src_bot -= src_stride;
125 dst_bot -= dst_stride;
126 }
127}
128
129int ARGBRotate(const uint8* src_argb, int src_stride_argb,
130 uint8* dst_argb, int dst_stride_argb,
131 int width, int height,
132 RotationMode mode) {
133 if (!src_argb || width <= 0 || height == 0 || !dst_argb) {
134 return -1;
135 }
136
137 // Negative height means invert the image.
138 if (height < 0) {
139 height = -height;
140 src_argb = src_argb + (height - 1) * src_stride_argb;
141 src_stride_argb = -src_stride_argb;
142 }
143
144 switch (mode) {
145 case kRotate0:
146 // copy frame
147 return ARGBCopy(src_argb, src_stride_argb,
148 dst_argb, dst_stride_argb,
149 width, height);
150 case kRotate90:
151 ARGBRotate90(src_argb, src_stride_argb,
152 dst_argb, dst_stride_argb,
153 width, height);
154 return 0;
155 case kRotate270:
156 ARGBRotate270(src_argb, src_stride_argb,
157 dst_argb, dst_stride_argb,
158 width, height);
159 return 0;
160 case kRotate180:
161 ARGBRotate180(src_argb, src_stride_argb,
162 dst_argb, dst_stride_argb,
163 width, height);
164 return 0;
165 default:
166 break;
167 }
168 return -1;
169}
170
171#ifdef __cplusplus
172} // extern "C"
173} // namespace libyuv
174#endif