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