rotate_argb.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include "libyuv/rotate.h"
  11. #include "libyuv/cpu_id.h"
  12. #include "libyuv/convert.h"
  13. #include "libyuv/planar_functions.h"
  14. #include "libyuv/row.h"
  15. #ifdef __cplusplus
  16. namespace libyuv {
  17. extern "C" {
  18. #endif
  19. // ARGBScale has a function to copy pixels to a row, striding each source
  20. // pixel by a constant.
  21. #if !defined(LIBYUV_DISABLE_X86) && \
  22. (defined(_M_IX86) || \
  23. (defined(__x86_64__) && !defined(__native_client__)) || defined(__i386__))
  24. #define HAS_SCALEARGBROWDOWNEVEN_SSE2
  25. void ScaleARGBRowDownEven_SSE2(const uint8* src_ptr, int src_stride,
  26. int src_stepx, uint8* dst_ptr, int dst_width);
  27. #endif
  28. #if !defined(LIBYUV_DISABLE_NEON) && !defined(__native_client__) && \
  29. (defined(__ARM_NEON__) || defined(LIBYUV_NEON) || defined(__aarch64__))
  30. #define HAS_SCALEARGBROWDOWNEVEN_NEON
  31. void ScaleARGBRowDownEven_NEON(const uint8* src_ptr, int src_stride,
  32. int src_stepx, uint8* dst_ptr, int dst_width);
  33. #endif
  34. void ScaleARGBRowDownEven_C(const uint8* src_ptr, int,
  35. int src_stepx, uint8* dst_ptr, int dst_width);
  36. static void ARGBTranspose(const uint8* src, int src_stride,
  37. uint8* dst, int dst_stride, int width, int height) {
  38. int i;
  39. int src_pixel_step = src_stride >> 2;
  40. void (*ScaleARGBRowDownEven)(const uint8* src_ptr, int src_stride,
  41. int src_step, uint8* dst_ptr, int dst_width) = ScaleARGBRowDownEven_C;
  42. #if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2)
  43. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(height, 4)) { // Width of dest.
  44. ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2;
  45. }
  46. #endif
  47. #if defined(HAS_SCALEARGBROWDOWNEVEN_NEON)
  48. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(height, 4)) { // Width of dest.
  49. ScaleARGBRowDownEven = ScaleARGBRowDownEven_NEON;
  50. }
  51. #endif
  52. for (i = 0; i < width; ++i) { // column of source to row of dest.
  53. ScaleARGBRowDownEven(src, 0, src_pixel_step, dst, height);
  54. dst += dst_stride;
  55. src += 4;
  56. }
  57. }
  58. void ARGBRotate90(const uint8* src, int src_stride,
  59. uint8* dst, int dst_stride, 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. void ARGBRotate270(const uint8* src, int src_stride,
  68. uint8* dst, int dst_stride, int width, int height) {
  69. // Rotate by 270 is a ARGBTranspose with the destination written
  70. // from bottom to top. So set the destination pointer to the end
  71. // of the buffer and flip the sign of the destination stride.
  72. dst += dst_stride * (width - 1);
  73. dst_stride = -dst_stride;
  74. ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
  75. }
  76. void ARGBRotate180(const uint8* src, int src_stride,
  77. uint8* dst, int dst_stride, int width, int height) {
  78. // Swap first and last row and mirror the content. Uses a temporary row.
  79. align_buffer_64(row, width * 4);
  80. const uint8* src_bot = src + src_stride * (height - 1);
  81. uint8* dst_bot = dst + dst_stride * (height - 1);
  82. int half_height = (height + 1) >> 1;
  83. int y;
  84. void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) =
  85. ARGBMirrorRow_C;
  86. void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C;
  87. #if defined(HAS_ARGBMIRRORROW_NEON)
  88. if (TestCpuFlag(kCpuHasNEON)) {
  89. ARGBMirrorRow = ARGBMirrorRow_Any_NEON;
  90. if (IS_ALIGNED(width, 4)) {
  91. ARGBMirrorRow = ARGBMirrorRow_NEON;
  92. }
  93. }
  94. #endif
  95. #if defined(HAS_ARGBMIRRORROW_SSE2)
  96. if (TestCpuFlag(kCpuHasSSE2)) {
  97. ARGBMirrorRow = ARGBMirrorRow_Any_SSE2;
  98. if (IS_ALIGNED(width, 4)) {
  99. ARGBMirrorRow = ARGBMirrorRow_SSE2;
  100. }
  101. }
  102. #endif
  103. #if defined(HAS_ARGBMIRRORROW_AVX2)
  104. if (TestCpuFlag(kCpuHasAVX2)) {
  105. ARGBMirrorRow = ARGBMirrorRow_Any_AVX2;
  106. if (IS_ALIGNED(width, 8)) {
  107. ARGBMirrorRow = ARGBMirrorRow_AVX2;
  108. }
  109. }
  110. #endif
  111. #if defined(HAS_ARGBMIRRORROW_MSA)
  112. if (TestCpuFlag(kCpuHasMSA)) {
  113. ARGBMirrorRow = ARGBMirrorRow_Any_MSA;
  114. if (IS_ALIGNED(width, 16)) {
  115. ARGBMirrorRow = ARGBMirrorRow_MSA;
  116. }
  117. }
  118. #endif
  119. #if defined(HAS_COPYROW_SSE2)
  120. if (TestCpuFlag(kCpuHasSSE2)) {
  121. CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2;
  122. }
  123. #endif
  124. #if defined(HAS_COPYROW_AVX)
  125. if (TestCpuFlag(kCpuHasAVX)) {
  126. CopyRow = IS_ALIGNED(width * 4, 64) ? CopyRow_AVX : CopyRow_Any_AVX;
  127. }
  128. #endif
  129. #if defined(HAS_COPYROW_ERMS)
  130. if (TestCpuFlag(kCpuHasERMS)) {
  131. CopyRow = CopyRow_ERMS;
  132. }
  133. #endif
  134. #if defined(HAS_COPYROW_NEON)
  135. if (TestCpuFlag(kCpuHasNEON)) {
  136. CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_NEON : CopyRow_Any_NEON;
  137. }
  138. #endif
  139. #if defined(HAS_COPYROW_MIPS)
  140. if (TestCpuFlag(kCpuHasMIPS)) {
  141. CopyRow = CopyRow_MIPS;
  142. }
  143. #endif
  144. // Odd height will harmlessly mirror the middle row twice.
  145. for (y = 0; y < half_height; ++y) {
  146. ARGBMirrorRow(src, row, width); // Mirror first row into a buffer
  147. ARGBMirrorRow(src_bot, dst, width); // Mirror last row into first row
  148. CopyRow(row, dst_bot, width * 4); // Copy first mirrored row into last
  149. src += src_stride;
  150. dst += dst_stride;
  151. src_bot -= src_stride;
  152. dst_bot -= dst_stride;
  153. }
  154. free_aligned_buffer_64(row);
  155. }
  156. LIBYUV_API
  157. int ARGBRotate(const uint8* src_argb, int src_stride_argb,
  158. uint8* dst_argb, int dst_stride_argb, int width, int height,
  159. enum RotationMode mode) {
  160. if (!src_argb || width <= 0 || height == 0 || !dst_argb) {
  161. return -1;
  162. }
  163. // Negative height means invert the image.
  164. if (height < 0) {
  165. height = -height;
  166. src_argb = src_argb + (height - 1) * src_stride_argb;
  167. src_stride_argb = -src_stride_argb;
  168. }
  169. switch (mode) {
  170. case kRotate0:
  171. // copy frame
  172. return ARGBCopy(src_argb, src_stride_argb,
  173. dst_argb, dst_stride_argb,
  174. width, height);
  175. case kRotate90:
  176. ARGBRotate90(src_argb, src_stride_argb,
  177. dst_argb, dst_stride_argb,
  178. width, height);
  179. return 0;
  180. case kRotate270:
  181. ARGBRotate270(src_argb, src_stride_argb,
  182. dst_argb, dst_stride_argb,
  183. width, height);
  184. return 0;
  185. case kRotate180:
  186. ARGBRotate180(src_argb, src_stride_argb,
  187. dst_argb, dst_stride_argb,
  188. width, height);
  189. return 0;
  190. default:
  191. break;
  192. }
  193. return -1;
  194. }
  195. #ifdef __cplusplus
  196. } // extern "C"
  197. } // namespace libyuv
  198. #endif