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