blob: 6c6fe4dc36d879574968624b4f0cb2c812733519 [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"
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
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)
fbarchard@google.com3e464442012-11-14 02:03:49 +000042 if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(height, 4) && // Width of dest.
fbarchard@google.comd51c3422012-06-26 23:46:25 +000043 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
44 ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2;
45 }
46#endif
47
fbarchard@google.com3e464442012-11-14 02:03:49 +000048 int src_pixel_step = src_stride >> 2;
fbarchard@google.comd51c3422012-06-26 23:46:25 +000049 for (int i = 0; i < width; ++i) { // column of source to row of dest.
50 ScaleARGBRowDownEven(src, 0, src_pixel_step, dst, height);
51 dst += dst_stride;
52 src += 4;
53 }
54}
55
56void ARGBRotate90(const uint8* src, int src_stride,
57 uint8* dst, int dst_stride,
58 int width, int height) {
59 // Rotate by 90 is a ARGBTranspose with the source read
fbarchard@google.com64ce0ab2012-10-09 00:05:29 +000060 // from bottom to top. So set the source pointer to the end
fbarchard@google.comd51c3422012-06-26 23:46:25 +000061 // of the buffer and flip the sign of the source stride.
62 src += src_stride * (height - 1);
63 src_stride = -src_stride;
64 ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
65}
66
67void ARGBRotate270(const uint8* src, int src_stride,
68 uint8* dst, int dst_stride,
69 int width, int height) {
70 // Rotate by 270 is a ARGBTranspose with the destination written
fbarchard@google.com64ce0ab2012-10-09 00:05:29 +000071 // from bottom to top. So set the destination pointer to the end
fbarchard@google.comd51c3422012-06-26 23:46:25 +000072 // of the buffer and flip the sign of the destination stride.
73 dst += dst_stride * (width - 1);
74 dst_stride = -dst_stride;
75 ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
76}
77
78void ARGBRotate180(const uint8* src, int src_stride,
79 uint8* dst, int dst_stride,
80 int width, int height) {
81 void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) =
82 ARGBMirrorRow_C;
83#if defined(HAS_ARGBMIRRORROW_SSSE3)
84 if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4) &&
85 IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) &&
86 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
87 ARGBMirrorRow = ARGBMirrorRow_SSSE3;
88 }
fbarchard@google.com3e464442012-11-14 02:03:49 +000089#elif defined(HAS_ARGBMIRRORROW_NEON)
90 if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 4)) {
91 ARGBMirrorRow = ARGBMirrorRow_NEON;
92 }
fbarchard@google.comd51c3422012-06-26 23:46:25 +000093#endif
94 void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C;
95#if defined(HAS_COPYROW_NEON)
fbarchard@google.com62a961b2012-10-22 17:24:50 +000096 if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width * 4, 32)) {
fbarchard@google.comd51c3422012-06-26 23:46:25 +000097 CopyRow = CopyRow_NEON;
98 }
99#endif
100#if defined(HAS_COPYROW_X86)
101 if (TestCpuFlag(kCpuHasX86)) {
102 CopyRow = CopyRow_X86;
103 }
104#endif
105#if defined(HAS_COPYROW_SSE2)
106 if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width * 4, 32) &&
107 IS_ALIGNED(src, 16) && IS_ALIGNED(src_stride, 16) &&
108 IS_ALIGNED(dst, 16) && IS_ALIGNED(dst_stride, 16)) {
109 CopyRow = CopyRow_SSE2;
110 }
111#endif
112 if (width * 4 > kMaxStride) {
113 return;
114 }
fbarchard@google.com64ce0ab2012-10-09 00:05:29 +0000115 // Swap first and last row and mirror the content. Uses a temporary row.
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000116 SIMD_ALIGNED(uint8 row[kMaxStride]);
117 const uint8* src_bot = src + src_stride * (height - 1);
118 uint8* dst_bot = dst + dst_stride * (height - 1);
119 int half_height = (height + 1) >> 1;
120 // Odd height will harmlessly mirror the middle row twice.
121 for (int y = 0; y < half_height; ++y) {
122 ARGBMirrorRow(src, row, width); // Mirror first row into a buffer
123 src += src_stride;
124 ARGBMirrorRow(src_bot, dst, width); // Mirror last row into first row
125 dst += dst_stride;
126 CopyRow(row, dst_bot, width * 4); // Copy first mirrored row into last
127 src_bot -= src_stride;
128 dst_bot -= dst_stride;
129 }
130}
131
fbarchard@google.comfc7314e2012-09-27 02:17:51 +0000132LIBYUV_API
fbarchard@google.comd51c3422012-06-26 23:46:25 +0000133int ARGBRotate(const uint8* src_argb, int src_stride_argb,
134 uint8* dst_argb, int dst_stride_argb,
135 int width, int height,
136 RotationMode mode) {
137 if (!src_argb || width <= 0 || height == 0 || !dst_argb) {
138 return -1;
139 }
140
141 // Negative height means invert the image.
142 if (height < 0) {
143 height = -height;
144 src_argb = src_argb + (height - 1) * src_stride_argb;
145 src_stride_argb = -src_stride_argb;
146 }
147
148 switch (mode) {
149 case kRotate0:
150 // copy frame
151 return ARGBCopy(src_argb, src_stride_argb,
152 dst_argb, dst_stride_argb,
153 width, height);
154 case kRotate90:
155 ARGBRotate90(src_argb, src_stride_argb,
156 dst_argb, dst_stride_argb,
157 width, height);
158 return 0;
159 case kRotate270:
160 ARGBRotate270(src_argb, src_stride_argb,
161 dst_argb, dst_stride_argb,
162 width, height);
163 return 0;
164 case kRotate180:
165 ARGBRotate180(src_argb, src_stride_argb,
166 dst_argb, dst_stride_argb,
167 width, height);
168 return 0;
169 default:
170 break;
171 }
172 return -1;
173}
174
175#ifdef __cplusplus
176} // extern "C"
177} // namespace libyuv
178#endif