rotate.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright 2011 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/rotate_row.h"
  15. #include "libyuv/row.h"
  16. #ifdef __cplusplus
  17. namespace libyuv {
  18. extern "C" {
  19. #endif
  20. LIBYUV_API
  21. void TransposePlane(const uint8_t* src,
  22. int src_stride,
  23. uint8_t* dst,
  24. int dst_stride,
  25. int width,
  26. int height) {
  27. int i = height;
  28. #if defined(HAS_TRANSPOSEWX16_MSA)
  29. void (*TransposeWx16)(const uint8_t* src, int src_stride, uint8_t* dst,
  30. int dst_stride, int width) = TransposeWx16_C;
  31. #else
  32. void (*TransposeWx8)(const uint8_t* src, int src_stride, uint8_t* dst,
  33. int dst_stride, int width) = TransposeWx8_C;
  34. #endif
  35. #if defined(HAS_TRANSPOSEWX8_NEON)
  36. if (TestCpuFlag(kCpuHasNEON)) {
  37. TransposeWx8 = TransposeWx8_NEON;
  38. }
  39. #endif
  40. #if defined(HAS_TRANSPOSEWX8_SSSE3)
  41. if (TestCpuFlag(kCpuHasSSSE3)) {
  42. TransposeWx8 = TransposeWx8_Any_SSSE3;
  43. if (IS_ALIGNED(width, 8)) {
  44. TransposeWx8 = TransposeWx8_SSSE3;
  45. }
  46. }
  47. #endif
  48. #if defined(HAS_TRANSPOSEWX8_FAST_SSSE3)
  49. if (TestCpuFlag(kCpuHasSSSE3)) {
  50. TransposeWx8 = TransposeWx8_Fast_Any_SSSE3;
  51. if (IS_ALIGNED(width, 16)) {
  52. TransposeWx8 = TransposeWx8_Fast_SSSE3;
  53. }
  54. }
  55. #endif
  56. #if defined(HAS_TRANSPOSEWX16_MSA)
  57. if (TestCpuFlag(kCpuHasMSA)) {
  58. TransposeWx16 = TransposeWx16_Any_MSA;
  59. if (IS_ALIGNED(width, 16)) {
  60. TransposeWx16 = TransposeWx16_MSA;
  61. }
  62. }
  63. #endif
  64. #if defined(HAS_TRANSPOSEWX16_MSA)
  65. // Work across the source in 16x16 tiles
  66. while (i >= 16) {
  67. TransposeWx16(src, src_stride, dst, dst_stride, width);
  68. src += 16 * src_stride; // Go down 16 rows.
  69. dst += 16; // Move over 16 columns.
  70. i -= 16;
  71. }
  72. #else
  73. // Work across the source in 8x8 tiles
  74. while (i >= 8) {
  75. TransposeWx8(src, src_stride, dst, dst_stride, width);
  76. src += 8 * src_stride; // Go down 8 rows.
  77. dst += 8; // Move over 8 columns.
  78. i -= 8;
  79. }
  80. #endif
  81. if (i > 0) {
  82. TransposeWxH_C(src, src_stride, dst, dst_stride, width, i);
  83. }
  84. }
  85. LIBYUV_API
  86. void RotatePlane90(const uint8_t* src,
  87. int src_stride,
  88. uint8_t* dst,
  89. int dst_stride,
  90. int width,
  91. int height) {
  92. // Rotate by 90 is a transpose with the source read
  93. // from bottom to top. So set the source pointer to the end
  94. // of the buffer and flip the sign of the source stride.
  95. src += src_stride * (height - 1);
  96. src_stride = -src_stride;
  97. TransposePlane(src, src_stride, dst, dst_stride, width, height);
  98. }
  99. LIBYUV_API
  100. void RotatePlane270(const uint8_t* src,
  101. int src_stride,
  102. uint8_t* dst,
  103. int dst_stride,
  104. int width,
  105. int height) {
  106. // Rotate by 270 is a transpose with the destination written
  107. // from bottom to top. So set the destination pointer to the end
  108. // of the buffer and flip the sign of the destination stride.
  109. dst += dst_stride * (width - 1);
  110. dst_stride = -dst_stride;
  111. TransposePlane(src, src_stride, dst, dst_stride, width, height);
  112. }
  113. LIBYUV_API
  114. void RotatePlane180(const uint8_t* src,
  115. int src_stride,
  116. uint8_t* dst,
  117. int dst_stride,
  118. int width,
  119. int height) {
  120. // Swap first and last row and mirror the content. Uses a temporary row.
  121. align_buffer_64(row, width);
  122. const uint8_t* src_bot = src + src_stride * (height - 1);
  123. uint8_t* dst_bot = dst + dst_stride * (height - 1);
  124. int half_height = (height + 1) >> 1;
  125. int y;
  126. void (*MirrorRow)(const uint8_t* src, uint8_t* dst, int width) = MirrorRow_C;
  127. void (*CopyRow)(const uint8_t* src, uint8_t* dst, int width) = CopyRow_C;
  128. #if defined(HAS_MIRRORROW_NEON)
  129. if (TestCpuFlag(kCpuHasNEON)) {
  130. MirrorRow = MirrorRow_Any_NEON;
  131. if (IS_ALIGNED(width, 16)) {
  132. MirrorRow = MirrorRow_NEON;
  133. }
  134. }
  135. #endif
  136. #if defined(HAS_MIRRORROW_SSSE3)
  137. if (TestCpuFlag(kCpuHasSSSE3)) {
  138. MirrorRow = MirrorRow_Any_SSSE3;
  139. if (IS_ALIGNED(width, 16)) {
  140. MirrorRow = MirrorRow_SSSE3;
  141. }
  142. }
  143. #endif
  144. #if defined(HAS_MIRRORROW_AVX2)
  145. if (TestCpuFlag(kCpuHasAVX2)) {
  146. MirrorRow = MirrorRow_Any_AVX2;
  147. if (IS_ALIGNED(width, 32)) {
  148. MirrorRow = MirrorRow_AVX2;
  149. }
  150. }
  151. #endif
  152. #if defined(HAS_MIRRORROW_MSA)
  153. if (TestCpuFlag(kCpuHasMSA)) {
  154. MirrorRow = MirrorRow_Any_MSA;
  155. if (IS_ALIGNED(width, 64)) {
  156. MirrorRow = MirrorRow_MSA;
  157. }
  158. }
  159. #endif
  160. #if defined(HAS_COPYROW_SSE2)
  161. if (TestCpuFlag(kCpuHasSSE2)) {
  162. CopyRow = IS_ALIGNED(width, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2;
  163. }
  164. #endif
  165. #if defined(HAS_COPYROW_AVX)
  166. if (TestCpuFlag(kCpuHasAVX)) {
  167. CopyRow = IS_ALIGNED(width, 64) ? CopyRow_AVX : CopyRow_Any_AVX;
  168. }
  169. #endif
  170. #if defined(HAS_COPYROW_ERMS)
  171. if (TestCpuFlag(kCpuHasERMS)) {
  172. CopyRow = CopyRow_ERMS;
  173. }
  174. #endif
  175. #if defined(HAS_COPYROW_NEON)
  176. if (TestCpuFlag(kCpuHasNEON)) {
  177. CopyRow = IS_ALIGNED(width, 32) ? CopyRow_NEON : CopyRow_Any_NEON;
  178. }
  179. #endif
  180. // Odd height will harmlessly mirror the middle row twice.
  181. for (y = 0; y < half_height; ++y) {
  182. MirrorRow(src, row, width); // Mirror first row into a buffer
  183. src += src_stride;
  184. MirrorRow(src_bot, dst, width); // Mirror last row into first row
  185. dst += dst_stride;
  186. CopyRow(row, dst_bot, width); // Copy first mirrored row into last
  187. src_bot -= src_stride;
  188. dst_bot -= dst_stride;
  189. }
  190. free_aligned_buffer_64(row);
  191. }
  192. LIBYUV_API
  193. void TransposeUV(const uint8_t* src,
  194. int src_stride,
  195. uint8_t* dst_a,
  196. int dst_stride_a,
  197. uint8_t* dst_b,
  198. int dst_stride_b,
  199. int width,
  200. int height) {
  201. int i = height;
  202. #if defined(HAS_TRANSPOSEUVWX16_MSA)
  203. void (*TransposeUVWx16)(const uint8_t* src, int src_stride, uint8_t* dst_a,
  204. int dst_stride_a, uint8_t* dst_b, int dst_stride_b,
  205. int width) = TransposeUVWx16_C;
  206. #else
  207. void (*TransposeUVWx8)(const uint8_t* src, int src_stride, uint8_t* dst_a,
  208. int dst_stride_a, uint8_t* dst_b, int dst_stride_b,
  209. int width) = TransposeUVWx8_C;
  210. #endif
  211. #if defined(HAS_TRANSPOSEUVWX8_NEON)
  212. if (TestCpuFlag(kCpuHasNEON)) {
  213. TransposeUVWx8 = TransposeUVWx8_NEON;
  214. }
  215. #endif
  216. #if defined(HAS_TRANSPOSEUVWX8_SSE2)
  217. if (TestCpuFlag(kCpuHasSSE2)) {
  218. TransposeUVWx8 = TransposeUVWx8_Any_SSE2;
  219. if (IS_ALIGNED(width, 8)) {
  220. TransposeUVWx8 = TransposeUVWx8_SSE2;
  221. }
  222. }
  223. #endif
  224. #if defined(HAS_TRANSPOSEUVWX16_MSA)
  225. if (TestCpuFlag(kCpuHasMSA)) {
  226. TransposeUVWx16 = TransposeUVWx16_Any_MSA;
  227. if (IS_ALIGNED(width, 8)) {
  228. TransposeUVWx16 = TransposeUVWx16_MSA;
  229. }
  230. }
  231. #endif
  232. #if defined(HAS_TRANSPOSEUVWX16_MSA)
  233. // Work through the source in 8x8 tiles.
  234. while (i >= 16) {
  235. TransposeUVWx16(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b,
  236. width);
  237. src += 16 * src_stride; // Go down 16 rows.
  238. dst_a += 16; // Move over 8 columns.
  239. dst_b += 16; // Move over 8 columns.
  240. i -= 16;
  241. }
  242. #else
  243. // Work through the source in 8x8 tiles.
  244. while (i >= 8) {
  245. TransposeUVWx8(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b,
  246. width);
  247. src += 8 * src_stride; // Go down 8 rows.
  248. dst_a += 8; // Move over 8 columns.
  249. dst_b += 8; // Move over 8 columns.
  250. i -= 8;
  251. }
  252. #endif
  253. if (i > 0) {
  254. TransposeUVWxH_C(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b,
  255. width, i);
  256. }
  257. }
  258. LIBYUV_API
  259. void RotateUV90(const uint8_t* src,
  260. int src_stride,
  261. uint8_t* dst_a,
  262. int dst_stride_a,
  263. uint8_t* dst_b,
  264. int dst_stride_b,
  265. int width,
  266. int height) {
  267. src += src_stride * (height - 1);
  268. src_stride = -src_stride;
  269. TransposeUV(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b, width,
  270. height);
  271. }
  272. LIBYUV_API
  273. void RotateUV270(const uint8_t* src,
  274. int src_stride,
  275. uint8_t* dst_a,
  276. int dst_stride_a,
  277. uint8_t* dst_b,
  278. int dst_stride_b,
  279. int width,
  280. int height) {
  281. dst_a += dst_stride_a * (width - 1);
  282. dst_b += dst_stride_b * (width - 1);
  283. dst_stride_a = -dst_stride_a;
  284. dst_stride_b = -dst_stride_b;
  285. TransposeUV(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b, width,
  286. height);
  287. }
  288. // Rotate 180 is a horizontal and vertical flip.
  289. LIBYUV_API
  290. void RotateUV180(const uint8_t* src,
  291. int src_stride,
  292. uint8_t* dst_a,
  293. int dst_stride_a,
  294. uint8_t* dst_b,
  295. int dst_stride_b,
  296. int width,
  297. int height) {
  298. int i;
  299. void (*MirrorUVRow)(const uint8_t* src, uint8_t* dst_u, uint8_t* dst_v,
  300. int width) = MirrorUVRow_C;
  301. #if defined(HAS_MIRRORUVROW_NEON)
  302. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
  303. MirrorUVRow = MirrorUVRow_NEON;
  304. }
  305. #endif
  306. #if defined(HAS_MIRRORUVROW_SSSE3)
  307. if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 16)) {
  308. MirrorUVRow = MirrorUVRow_SSSE3;
  309. }
  310. #endif
  311. #if defined(HAS_MIRRORUVROW_MSA)
  312. if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 32)) {
  313. MirrorUVRow = MirrorUVRow_MSA;
  314. }
  315. #endif
  316. dst_a += dst_stride_a * (height - 1);
  317. dst_b += dst_stride_b * (height - 1);
  318. for (i = 0; i < height; ++i) {
  319. MirrorUVRow(src, dst_a, dst_b, width);
  320. src += src_stride;
  321. dst_a -= dst_stride_a;
  322. dst_b -= dst_stride_b;
  323. }
  324. }
  325. LIBYUV_API
  326. int RotatePlane(const uint8_t* src,
  327. int src_stride,
  328. uint8_t* dst,
  329. int dst_stride,
  330. int width,
  331. int height,
  332. enum RotationMode mode) {
  333. if (!src || width <= 0 || height == 0 || !dst) {
  334. return -1;
  335. }
  336. // Negative height means invert the image.
  337. if (height < 0) {
  338. height = -height;
  339. src = src + (height - 1) * src_stride;
  340. src_stride = -src_stride;
  341. }
  342. switch (mode) {
  343. case kRotate0:
  344. // copy frame
  345. CopyPlane(src, src_stride, dst, dst_stride, width, height);
  346. return 0;
  347. case kRotate90:
  348. RotatePlane90(src, src_stride, dst, dst_stride, width, height);
  349. return 0;
  350. case kRotate270:
  351. RotatePlane270(src, src_stride, dst, dst_stride, width, height);
  352. return 0;
  353. case kRotate180:
  354. RotatePlane180(src, src_stride, dst, dst_stride, width, height);
  355. return 0;
  356. default:
  357. break;
  358. }
  359. return -1;
  360. }
  361. LIBYUV_API
  362. int I420Rotate(const uint8_t* src_y,
  363. int src_stride_y,
  364. const uint8_t* src_u,
  365. int src_stride_u,
  366. const uint8_t* src_v,
  367. int src_stride_v,
  368. uint8_t* dst_y,
  369. int dst_stride_y,
  370. uint8_t* dst_u,
  371. int dst_stride_u,
  372. uint8_t* dst_v,
  373. int dst_stride_v,
  374. int width,
  375. int height,
  376. enum RotationMode mode) {
  377. int halfwidth = (width + 1) >> 1;
  378. int halfheight = (height + 1) >> 1;
  379. if (!src_y || !src_u || !src_v || width <= 0 || height == 0 || !dst_y ||
  380. !dst_u || !dst_v) {
  381. return -1;
  382. }
  383. // Negative height means invert the image.
  384. if (height < 0) {
  385. height = -height;
  386. halfheight = (height + 1) >> 1;
  387. src_y = src_y + (height - 1) * src_stride_y;
  388. src_u = src_u + (halfheight - 1) * src_stride_u;
  389. src_v = src_v + (halfheight - 1) * src_stride_v;
  390. src_stride_y = -src_stride_y;
  391. src_stride_u = -src_stride_u;
  392. src_stride_v = -src_stride_v;
  393. }
  394. switch (mode) {
  395. case kRotate0:
  396. // copy frame
  397. return I420Copy(src_y, src_stride_y, src_u, src_stride_u, src_v,
  398. src_stride_v, dst_y, dst_stride_y, dst_u, dst_stride_u,
  399. dst_v, dst_stride_v, width, height);
  400. case kRotate90:
  401. RotatePlane90(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  402. RotatePlane90(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth,
  403. halfheight);
  404. RotatePlane90(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth,
  405. halfheight);
  406. return 0;
  407. case kRotate270:
  408. RotatePlane270(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  409. RotatePlane270(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth,
  410. halfheight);
  411. RotatePlane270(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth,
  412. halfheight);
  413. return 0;
  414. case kRotate180:
  415. RotatePlane180(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  416. RotatePlane180(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth,
  417. halfheight);
  418. RotatePlane180(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth,
  419. halfheight);
  420. return 0;
  421. default:
  422. break;
  423. }
  424. return -1;
  425. }
  426. LIBYUV_API
  427. int NV12ToI420Rotate(const uint8_t* src_y,
  428. int src_stride_y,
  429. const uint8_t* src_uv,
  430. int src_stride_uv,
  431. uint8_t* dst_y,
  432. int dst_stride_y,
  433. uint8_t* dst_u,
  434. int dst_stride_u,
  435. uint8_t* dst_v,
  436. int dst_stride_v,
  437. int width,
  438. int height,
  439. enum RotationMode mode) {
  440. int halfwidth = (width + 1) >> 1;
  441. int halfheight = (height + 1) >> 1;
  442. if (!src_y || !src_uv || width <= 0 || height == 0 || !dst_y || !dst_u ||
  443. !dst_v) {
  444. return -1;
  445. }
  446. // Negative height means invert the image.
  447. if (height < 0) {
  448. height = -height;
  449. halfheight = (height + 1) >> 1;
  450. src_y = src_y + (height - 1) * src_stride_y;
  451. src_uv = src_uv + (halfheight - 1) * src_stride_uv;
  452. src_stride_y = -src_stride_y;
  453. src_stride_uv = -src_stride_uv;
  454. }
  455. switch (mode) {
  456. case kRotate0:
  457. // copy frame
  458. return NV12ToI420(src_y, src_stride_y, src_uv, src_stride_uv, dst_y,
  459. dst_stride_y, dst_u, dst_stride_u, dst_v, dst_stride_v,
  460. width, height);
  461. case kRotate90:
  462. RotatePlane90(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  463. RotateUV90(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v,
  464. dst_stride_v, halfwidth, halfheight);
  465. return 0;
  466. case kRotate270:
  467. RotatePlane270(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  468. RotateUV270(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v,
  469. dst_stride_v, halfwidth, halfheight);
  470. return 0;
  471. case kRotate180:
  472. RotatePlane180(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  473. RotateUV180(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v,
  474. dst_stride_v, halfwidth, halfheight);
  475. return 0;
  476. default:
  477. break;
  478. }
  479. return -1;
  480. }
  481. #ifdef __cplusplus
  482. } // extern "C"
  483. } // namespace libyuv
  484. #endif