rotate_argb.cc 8.2 KB

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