rotate_argb.cc 7.7 KB

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