rotate_argb.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_COPYROW_SSE2)
  112. if (TestCpuFlag(kCpuHasSSE2)) {
  113. CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2;
  114. }
  115. #endif
  116. #if defined(HAS_COPYROW_AVX)
  117. if (TestCpuFlag(kCpuHasAVX)) {
  118. CopyRow = IS_ALIGNED(width * 4, 64) ? CopyRow_AVX : CopyRow_Any_AVX;
  119. }
  120. #endif
  121. #if defined(HAS_COPYROW_ERMS)
  122. if (TestCpuFlag(kCpuHasERMS)) {
  123. CopyRow = CopyRow_ERMS;
  124. }
  125. #endif
  126. #if defined(HAS_COPYROW_NEON)
  127. if (TestCpuFlag(kCpuHasNEON)) {
  128. CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_NEON : CopyRow_Any_NEON;
  129. }
  130. #endif
  131. #if defined(HAS_COPYROW_MIPS)
  132. if (TestCpuFlag(kCpuHasMIPS)) {
  133. CopyRow = CopyRow_MIPS;
  134. }
  135. #endif
  136. // Odd height will harmlessly mirror the middle row twice.
  137. for (y = 0; y < half_height; ++y) {
  138. ARGBMirrorRow(src, row, width); // Mirror first row into a buffer
  139. ARGBMirrorRow(src_bot, dst, width); // Mirror last row into first row
  140. CopyRow(row, dst_bot, width * 4); // Copy first mirrored row into last
  141. src += src_stride;
  142. dst += dst_stride;
  143. src_bot -= src_stride;
  144. dst_bot -= dst_stride;
  145. }
  146. free_aligned_buffer_64(row);
  147. }
  148. LIBYUV_API
  149. int ARGBRotate(const uint8* src_argb, int src_stride_argb,
  150. uint8* dst_argb, int dst_stride_argb, int width, int height,
  151. enum RotationMode mode) {
  152. if (!src_argb || width <= 0 || height == 0 || !dst_argb) {
  153. return -1;
  154. }
  155. // Negative height means invert the image.
  156. if (height < 0) {
  157. height = -height;
  158. src_argb = src_argb + (height - 1) * src_stride_argb;
  159. src_stride_argb = -src_stride_argb;
  160. }
  161. switch (mode) {
  162. case kRotate0:
  163. // copy frame
  164. return ARGBCopy(src_argb, src_stride_argb,
  165. dst_argb, dst_stride_argb,
  166. width, height);
  167. case kRotate90:
  168. ARGBRotate90(src_argb, src_stride_argb,
  169. dst_argb, dst_stride_argb,
  170. width, height);
  171. return 0;
  172. case kRotate270:
  173. ARGBRotate270(src_argb, src_stride_argb,
  174. dst_argb, dst_stride_argb,
  175. width, height);
  176. return 0;
  177. case kRotate180:
  178. ARGBRotate180(src_argb, src_stride_argb,
  179. dst_argb, dst_stride_argb,
  180. width, height);
  181. return 0;
  182. default:
  183. break;
  184. }
  185. return -1;
  186. }
  187. #ifdef __cplusplus
  188. } // extern "C"
  189. } // namespace libyuv
  190. #endif