rotate_argb.cc 7.5 KB

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