| 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 |
| 11 | #include "libyuv/rotate.h" |
| 12 |
| 13 | #include "libyuv/convert.h" |
| 14 | #include "libyuv/cpu_id.h" |
| 15 | #include "libyuv/planar_functions.h" |
| 16 | #include "libyuv/rotate_row.h" |
| 17 | #include "libyuv/row.h" |
| 18 |
| 19 | #ifdef __cplusplus |
| 20 | namespace libyuv { |
| 21 | extern "C" { |
| 22 | #endif |
| 23 |
| 24 | LIBYUV_API |
| 25 | void TransposePlane(const uint8_t* src, |
| 26 | int src_stride, |
| 27 | uint8_t* dst, |
| 28 | int dst_stride, |
| 29 | int width, |
| 30 | int height) { |
| 31 | int i = height; |
| 32 | #if defined(HAS_TRANSPOSEWX16_MSA) || defined(HAS_TRANSPOSEWX16_LSX) |
| 33 | void (*TransposeWx16)(const uint8_t* src, int src_stride, uint8_t* dst, |
| 34 | int dst_stride, int width) = TransposeWx16_C; |
| 35 | #else |
| 36 | void (*TransposeWx8)(const uint8_t* src, int src_stride, uint8_t* dst, |
| 37 | int dst_stride, int width) = TransposeWx8_C; |
| 38 | #endif |
| 39 |
| 40 | #if defined(HAS_TRANSPOSEWX8_NEON) |
| 41 | if (TestCpuFlag(kCpuHasNEON)) { |
| 42 | TransposeWx8 = TransposeWx8_Any_NEON; |
| 43 | if (IS_ALIGNED(width, 8)) { |
| 44 | TransposeWx8 = TransposeWx8_NEON; |
| 45 | } |
| 46 | } |
| 47 | #endif |
| 48 | #if defined(HAS_TRANSPOSEWX8_SSSE3) |
| 49 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 50 | TransposeWx8 = TransposeWx8_Any_SSSE3; |
| 51 | if (IS_ALIGNED(width, 8)) { |
| 52 | TransposeWx8 = TransposeWx8_SSSE3; |
| 53 | } |
| 54 | } |
| 55 | #endif |
| 56 | #if defined(HAS_TRANSPOSEWX8_FAST_SSSE3) |
| 57 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 58 | TransposeWx8 = TransposeWx8_Fast_Any_SSSE3; |
| 59 | if (IS_ALIGNED(width, 16)) { |
| 60 | TransposeWx8 = TransposeWx8_Fast_SSSE3; |
| 61 | } |
| 62 | } |
| 63 | #endif |
| 64 | #if defined(HAS_TRANSPOSEWX16_MSA) |
| 65 | if (TestCpuFlag(kCpuHasMSA)) { |
| 66 | TransposeWx16 = TransposeWx16_Any_MSA; |
| 67 | if (IS_ALIGNED(width, 16)) { |
| 68 | TransposeWx16 = TransposeWx16_MSA; |
| 69 | } |
| 70 | } |
| 71 | #endif |
| 72 | #if defined(HAS_TRANSPOSEWX16_LSX) |
| 73 | if (TestCpuFlag(kCpuHasLSX)) { |
| 74 | TransposeWx16 = TransposeWx16_Any_LSX; |
| 75 | if (IS_ALIGNED(width, 16)) { |
| 76 | TransposeWx16 = TransposeWx16_LSX; |
| 77 | } |
| 78 | } |
| 79 | #endif |
| 80 |
| 81 | #if defined(HAS_TRANSPOSEWX16_MSA) || defined(HAS_TRANSPOSEWX16_LSX) |
| 82 | // Work across the source in 16x16 tiles |
| 83 | while (i >= 16) { |
| 84 | TransposeWx16(src, src_stride, dst, dst_stride, width); |
| 85 | src += 16 * src_stride; // Go down 16 rows. |
| 86 | dst += 16; // Move over 16 columns. |
| 87 | i -= 16; |
| 88 | } |
| 89 | #else |
| 90 | // Work across the source in 8x8 tiles |
| 91 | while (i >= 8) { |
| 92 | TransposeWx8(src, src_stride, dst, dst_stride, width); |
| 93 | src += 8 * src_stride; // Go down 8 rows. |
| 94 | dst += 8; // Move over 8 columns. |
| 95 | i -= 8; |
| 96 | } |
| 97 | #endif |
| 98 |
| 99 | if (i > 0) { |
| 100 | TransposeWxH_C(src, src_stride, dst, dst_stride, width, i); |
| 101 | } |
| 102 | } |
| 103 |
| 104 | LIBYUV_API |
| 105 | void RotatePlane90(const uint8_t* src, |
| 106 | int src_stride, |
| 107 | uint8_t* dst, |
| 108 | int dst_stride, |
| 109 | int width, |
| 110 | int height) { |
| 111 | // Rotate by 90 is a transpose with the source read |
| 112 | // from bottom to top. So set the source pointer to the end |
| 113 | // of the buffer and flip the sign of the source stride. |
| 114 | src += src_stride * (height - 1); |
| 115 | src_stride = -src_stride; |
| 116 | TransposePlane(src, src_stride, dst, dst_stride, width, height); |
| 117 | } |
| 118 |
| 119 | LIBYUV_API |
| 120 | void RotatePlane270(const uint8_t* src, |
| 121 | int src_stride, |
| 122 | uint8_t* dst, |
| 123 | int dst_stride, |
| 124 | int width, |
| 125 | int height) { |
| 126 | // Rotate by 270 is a transpose with the destination written |
| 127 | // from bottom to top. So set the destination pointer to the end |
| 128 | // of the buffer and flip the sign of the destination stride. |
| 129 | dst += dst_stride * (width - 1); |
| 130 | dst_stride = -dst_stride; |
| 131 | TransposePlane(src, src_stride, dst, dst_stride, width, height); |
| 132 | } |
| 133 |
| 134 | LIBYUV_API |
| 135 | void RotatePlane180(const uint8_t* src, |
| 136 | int src_stride, |
| 137 | uint8_t* dst, |
| 138 | int dst_stride, |
| 139 | int width, |
| 140 | int height) { |
| 141 | // Swap top and bottom row and mirror the content. Uses a temporary row. |
| 142 | align_buffer_64(row, width); |
| 143 | const uint8_t* src_bot = src + src_stride * (height - 1); |
| 144 | uint8_t* dst_bot = dst + dst_stride * (height - 1); |
| 145 | int half_height = (height + 1) >> 1; |
| 146 | int y; |
| 147 | void (*MirrorRow)(const uint8_t* src, uint8_t* dst, int width) = MirrorRow_C; |
| 148 | void (*CopyRow)(const uint8_t* src, uint8_t* dst, int width) = CopyRow_C; |
| 149 | #if defined(HAS_MIRRORROW_NEON) |
| 150 | if (TestCpuFlag(kCpuHasNEON)) { |
| 151 | MirrorRow = MirrorRow_Any_NEON; |
| 152 | if (IS_ALIGNED(width, 32)) { |
| 153 | MirrorRow = MirrorRow_NEON; |
| 154 | } |
| 155 | } |
| 156 | #endif |
| 157 | #if defined(HAS_MIRRORROW_SSSE3) |
| 158 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 159 | MirrorRow = MirrorRow_Any_SSSE3; |
| 160 | if (IS_ALIGNED(width, 16)) { |
| 161 | MirrorRow = MirrorRow_SSSE3; |
| 162 | } |
| 163 | } |
| 164 | #endif |
| 165 | #if defined(HAS_MIRRORROW_AVX2) |
| 166 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 167 | MirrorRow = MirrorRow_Any_AVX2; |
| 168 | if (IS_ALIGNED(width, 32)) { |
| 169 | MirrorRow = MirrorRow_AVX2; |
| 170 | } |
| 171 | } |
| 172 | #endif |
| 173 | #if defined(HAS_MIRRORROW_MSA) |
| 174 | if (TestCpuFlag(kCpuHasMSA)) { |
| 175 | MirrorRow = MirrorRow_Any_MSA; |
| 176 | if (IS_ALIGNED(width, 64)) { |
| 177 | MirrorRow = MirrorRow_MSA; |
| 178 | } |
| 179 | } |
| 180 | #endif |
| 181 | #if defined(HAS_MIRRORROW_LASX) |
| 182 | if (TestCpuFlag(kCpuHasLASX)) { |
| 183 | MirrorRow = MirrorRow_Any_LASX; |
| 184 | if (IS_ALIGNED(width, 64)) { |
| 185 | MirrorRow = MirrorRow_LASX; |
| 186 | } |
| 187 | } |
| 188 | #endif |
| 189 | #if defined(HAS_COPYROW_SSE2) |
| 190 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 191 | CopyRow = IS_ALIGNED(width, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2; |
| 192 | } |
| 193 | #endif |
| 194 | #if defined(HAS_COPYROW_AVX) |
| 195 | if (TestCpuFlag(kCpuHasAVX)) { |
| 196 | CopyRow = IS_ALIGNED(width, 64) ? CopyRow_AVX : CopyRow_Any_AVX; |
| 197 | } |
| 198 | #endif |
| 199 | #if defined(HAS_COPYROW_ERMS) |
| 200 | if (TestCpuFlag(kCpuHasERMS)) { |
| 201 | CopyRow = CopyRow_ERMS; |
| 202 | } |
| 203 | #endif |
| 204 | #if defined(HAS_COPYROW_NEON) |
| 205 | if (TestCpuFlag(kCpuHasNEON)) { |
| 206 | CopyRow = IS_ALIGNED(width, 32) ? CopyRow_NEON : CopyRow_Any_NEON; |
| 207 | } |
| 208 | #endif |
| 209 |
| 210 | // Odd height will harmlessly mirror the middle row twice. |
| 211 | for (y = 0; y < half_height; ++y) { |
| 212 | CopyRow(src, row, width); // Copy top row into buffer |
| 213 | MirrorRow(src_bot, dst, width); // Mirror bottom row into top row |
| 214 | MirrorRow(row, dst_bot, width); // Mirror buffer into bottom row |
| 215 | src += src_stride; |
| 216 | dst += dst_stride; |
| 217 | src_bot -= src_stride; |
| 218 | dst_bot -= dst_stride; |
| 219 | } |
| 220 | free_aligned_buffer_64(row); |
| 221 | } |
| 222 |
| 223 | LIBYUV_API |
| 224 | void SplitTransposeUV(const uint8_t* src, |
| 225 | int src_stride, |
| 226 | uint8_t* dst_a, |
| 227 | int dst_stride_a, |
| 228 | uint8_t* dst_b, |
| 229 | int dst_stride_b, |
| 230 | int width, |
| 231 | int height) { |
| 232 | int i = height; |
| 233 | #if defined(HAS_TRANSPOSEUVWX16_MSA) |
| 234 | void (*TransposeUVWx16)(const uint8_t* src, int src_stride, uint8_t* dst_a, |
| 235 | int dst_stride_a, uint8_t* dst_b, int dst_stride_b, |
| 236 | int width) = TransposeUVWx16_C; |
| 237 | #elif defined(HAS_TRANSPOSEUVWX16_LSX) |
| 238 | void (*TransposeUVWx16)(const uint8_t* src, int src_stride, uint8_t* dst_a, |
| 239 | int dst_stride_a, uint8_t* dst_b, int dst_stride_b, |
| 240 | int width) = TransposeUVWx16_C; |
| 241 | #else |
| 242 | void (*TransposeUVWx8)(const uint8_t* src, int src_stride, uint8_t* dst_a, |
| 243 | int dst_stride_a, uint8_t* dst_b, int dst_stride_b, |
| 244 | int width) = TransposeUVWx8_C; |
| 245 | #endif |
| 246 |
| 247 | #if defined(HAS_TRANSPOSEUVWX16_MSA) |
| 248 | if (TestCpuFlag(kCpuHasMSA)) { |
| 249 | TransposeUVWx16 = TransposeUVWx16_Any_MSA; |
| 250 | if (IS_ALIGNED(width, 8)) { |
| 251 | TransposeUVWx16 = TransposeUVWx16_MSA; |
| 252 | } |
| 253 | } |
| 254 | #elif defined(HAS_TRANSPOSEUVWX16_LSX) |
| 255 | if (TestCpuFlag(kCpuHasLSX)) { |
| 256 | TransposeUVWx16 = TransposeUVWx16_Any_LSX; |
| 257 | if (IS_ALIGNED(width, 8)) { |
| 258 | TransposeUVWx16 = TransposeUVWx16_LSX; |
| 259 | } |
| 260 | } |
| 261 | #else |
| 262 | #if defined(HAS_TRANSPOSEUVWX8_NEON) |
| 263 | if (TestCpuFlag(kCpuHasNEON)) { |
| 264 | TransposeUVWx8 = TransposeUVWx8_NEON; |
| 265 | } |
| 266 | #endif |
| 267 | #if defined(HAS_TRANSPOSEUVWX8_SSE2) |
| 268 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 269 | TransposeUVWx8 = TransposeUVWx8_Any_SSE2; |
| 270 | if (IS_ALIGNED(width, 8)) { |
| 271 | TransposeUVWx8 = TransposeUVWx8_SSE2; |
| 272 | } |
| 273 | } |
| 274 | #endif |
| 275 | #endif /* defined(HAS_TRANSPOSEUVWX16_MSA) */ |
| 276 |
| 277 | #if defined(HAS_TRANSPOSEUVWX16_MSA) |
| 278 | // Work through the source in 8x8 tiles. |
| 279 | while (i >= 16) { |
| 280 | TransposeUVWx16(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b, |
| 281 | width); |
| 282 | src += 16 * src_stride; // Go down 16 rows. |
| 283 | dst_a += 16; // Move over 8 columns. |
| 284 | dst_b += 16; // Move over 8 columns. |
| 285 | i -= 16; |
| 286 | } |
| 287 | #elif defined(HAS_TRANSPOSEUVWX16_LSX) |
| 288 | // Work through the source in 8x8 tiles. |
| 289 | while (i >= 16) { |
| 290 | TransposeUVWx16(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b, |
| 291 | width); |
| 292 | src += 16 * src_stride; // Go down 16 rows. |
| 293 | dst_a += 16; // Move over 8 columns. |
| 294 | dst_b += 16; // Move over 8 columns. |
| 295 | i -= 16; |
| 296 | } |
| 297 | #else |
| 298 | // Work through the source in 8x8 tiles. |
| 299 | while (i >= 8) { |
| 300 | TransposeUVWx8(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b, |
| 301 | width); |
| 302 | src += 8 * src_stride; // Go down 8 rows. |
| 303 | dst_a += 8; // Move over 8 columns. |
| 304 | dst_b += 8; // Move over 8 columns. |
| 305 | i -= 8; |
| 306 | } |
| 307 | #endif |
| 308 |
| 309 | if (i > 0) { |
| 310 | TransposeUVWxH_C(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b, |
| 311 | width, i); |
| 312 | } |
| 313 | } |
| 314 |
| 315 | LIBYUV_API |
| 316 | void SplitRotateUV90(const uint8_t* src, |
| 317 | int src_stride, |
| 318 | uint8_t* dst_a, |
| 319 | int dst_stride_a, |
| 320 | uint8_t* dst_b, |
| 321 | int dst_stride_b, |
| 322 | int width, |
| 323 | int height) { |
| 324 | src += src_stride * (height - 1); |
| 325 | src_stride = -src_stride; |
| 326 |
| 327 | SplitTransposeUV(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b, |
| 328 | width, height); |
| 329 | } |
| 330 |
| 331 | LIBYUV_API |
| 332 | void SplitRotateUV270(const uint8_t* src, |
| 333 | int src_stride, |
| 334 | uint8_t* dst_a, |
| 335 | int dst_stride_a, |
| 336 | uint8_t* dst_b, |
| 337 | int dst_stride_b, |
| 338 | int width, |
| 339 | int height) { |
| 340 | dst_a += dst_stride_a * (width - 1); |
| 341 | dst_b += dst_stride_b * (width - 1); |
| 342 | dst_stride_a = -dst_stride_a; |
| 343 | dst_stride_b = -dst_stride_b; |
| 344 |
| 345 | SplitTransposeUV(src, src_stride, dst_a, dst_stride_a, dst_b, dst_stride_b, |
| 346 | width, height); |
| 347 | } |
| 348 |
| 349 | // Rotate 180 is a horizontal and vertical flip. |
| 350 | LIBYUV_API |
| 351 | void SplitRotateUV180(const uint8_t* src, |
| 352 | int src_stride, |
| 353 | uint8_t* dst_a, |
| 354 | int dst_stride_a, |
| 355 | uint8_t* dst_b, |
| 356 | int dst_stride_b, |
| 357 | int width, |
| 358 | int height) { |
| 359 | int i; |
| 360 | void (*MirrorSplitUVRow)(const uint8_t* src, uint8_t* dst_u, uint8_t* dst_v, |
| 361 | int width) = MirrorSplitUVRow_C; |
| 362 | #if defined(HAS_MIRRORSPLITUVROW_NEON) |
| 363 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16)) { |
| 364 | MirrorSplitUVRow = MirrorSplitUVRow_NEON; |
| 365 | } |
| 366 | #endif |
| 367 | #if defined(HAS_MIRRORSPLITUVROW_SSSE3) |
| 368 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 16)) { |
| 369 | MirrorSplitUVRow = MirrorSplitUVRow_SSSE3; |
| 370 | } |
| 371 | #endif |
| 372 | #if defined(HAS_MIRRORSPLITUVROW_MSA) |
| 373 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 32)) { |
| 374 | MirrorSplitUVRow = MirrorSplitUVRow_MSA; |
| 375 | } |
| 376 | #endif |
| 377 | #if defined(HAS_MIRRORSPLITUVROW_LSX) |
| 378 | if (TestCpuFlag(kCpuHasLSX) && IS_ALIGNED(width, 32)) { |
| 379 | MirrorSplitUVRow = MirrorSplitUVRow_LSX; |
| 380 | } |
| 381 | #endif |
| 382 |
| 383 | dst_a += dst_stride_a * (height - 1); |
| 384 | dst_b += dst_stride_b * (height - 1); |
| 385 |
| 386 | for (i = 0; i < height; ++i) { |
| 387 | MirrorSplitUVRow(src, dst_a, dst_b, width); |
| 388 | src += src_stride; |
| 389 | dst_a -= dst_stride_a; |
| 390 | dst_b -= dst_stride_b; |
| 391 | } |
| 392 | } |
| 393 |
| 394 | // Rotate UV and split into planar. |
| 395 | // width and height expected to be half size for NV12 |
| 396 | LIBYUV_API |
| 397 | int SplitRotateUV(const uint8_t* src_uv, |
| 398 | int src_stride_uv, |
| 399 | uint8_t* dst_u, |
| 400 | int dst_stride_u, |
| 401 | uint8_t* dst_v, |
| 402 | int dst_stride_v, |
| 403 | int width, |
| 404 | int height, |
| 405 | enum RotationMode mode) { |
| 406 | if (!src_uv || width <= 0 || height == 0 || !dst_u || !dst_v) { |
| 407 | return -1; |
| 408 | } |
| 409 |
| 410 | // Negative height means invert the image. |
| 411 | if (height < 0) { |
| 412 | height = -height; |
| 413 | src_uv = src_uv + (height - 1) * src_stride_uv; |
| 414 | src_stride_uv = -src_stride_uv; |
| 415 | } |
| 416 |
| 417 | switch (mode) { |
| 418 | case kRotate0: |
| 419 | SplitUVPlane(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v, |
| 420 | dst_stride_v, width, height); |
| 421 | return 0; |
| 422 | case kRotate90: |
| 423 | SplitRotateUV90(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v, |
| 424 | dst_stride_v, width, height); |
| 425 | return 0; |
| 426 | case kRotate270: |
| 427 | SplitRotateUV270(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v, |
| 428 | dst_stride_v, width, height); |
| 429 | return 0; |
| 430 | case kRotate180: |
| 431 | SplitRotateUV180(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v, |
| 432 | dst_stride_v, width, height); |
| 433 | return 0; |
| 434 | default: |
| 435 | break; |
| 436 | } |
| 437 | return -1; |
| 438 | } |
| 439 |
| 440 | LIBYUV_API |
| 441 | int RotatePlane(const uint8_t* src, |
| 442 | int src_stride, |
| 443 | uint8_t* dst, |
| 444 | int dst_stride, |
| 445 | int width, |
| 446 | int height, |
| 447 | enum RotationMode mode) { |
| 448 | if (!src || width <= 0 || height == 0 || !dst) { |
| 449 | return -1; |
| 450 | } |
| 451 |
| 452 | // Negative height means invert the image. |
| 453 | if (height < 0) { |
| 454 | height = -height; |
| 455 | src = src + (height - 1) * src_stride; |
| 456 | src_stride = -src_stride; |
| 457 | } |
| 458 |
| 459 | switch (mode) { |
| 460 | case kRotate0: |
| 461 | // copy frame |
| 462 | CopyPlane(src, src_stride, dst, dst_stride, width, height); |
| 463 | return 0; |
| 464 | case kRotate90: |
| 465 | RotatePlane90(src, src_stride, dst, dst_stride, width, height); |
| 466 | return 0; |
| 467 | case kRotate270: |
| 468 | RotatePlane270(src, src_stride, dst, dst_stride, width, height); |
| 469 | return 0; |
| 470 | case kRotate180: |
| 471 | RotatePlane180(src, src_stride, dst, dst_stride, width, height); |
| 472 | return 0; |
| 473 | default: |
| 474 | break; |
| 475 | } |
| 476 | return -1; |
| 477 | } |
| 478 |
| 479 | LIBYUV_API |
| 480 | void TransposePlane_16(const uint16_t* src, |
| 481 | int src_stride, |
| 482 | uint16_t* dst, |
| 483 | int dst_stride, |
| 484 | int width, |
| 485 | int height) { |
| 486 | int i = height; |
| 487 | // Work across the source in 8x8 tiles |
| 488 | while (i >= 8) { |
| 489 | TransposeWx8_16_C(src, src_stride, dst, dst_stride, width); |
| 490 | src += 8 * src_stride; // Go down 8 rows. |
| 491 | dst += 8; // Move over 8 columns. |
| 492 | i -= 8; |
| 493 | } |
| 494 |
| 495 | if (i > 0) { |
| 496 | TransposeWxH_16_C(src, src_stride, dst, dst_stride, width, i); |
| 497 | } |
| 498 | } |
| 499 |
| 500 | static void RotatePlane90_16(const uint16_t* src, |
| 501 | int src_stride, |
| 502 | uint16_t* dst, |
| 503 | int dst_stride, |
| 504 | int width, |
| 505 | int height) { |
| 506 | // Rotate by 90 is a transpose with the source read |
| 507 | // from bottom to top. So set the source pointer to the end |
| 508 | // of the buffer and flip the sign of the source stride. |
| 509 | src += src_stride * (height - 1); |
| 510 | src_stride = -src_stride; |
| 511 | TransposePlane_16(src, src_stride, dst, dst_stride, width, height); |
| 512 | } |
| 513 |
| 514 | static void RotatePlane270_16(const uint16_t* src, |
| 515 | int src_stride, |
| 516 | uint16_t* dst, |
| 517 | int dst_stride, |
| 518 | int width, |
| 519 | int height) { |
| 520 | // Rotate by 270 is a transpose with the destination written |
| 521 | // from bottom to top. So set the destination pointer to the end |
| 522 | // of the buffer and flip the sign of the destination stride. |
| 523 | dst += dst_stride * (width - 1); |
| 524 | dst_stride = -dst_stride; |
| 525 | TransposePlane_16(src, src_stride, dst, dst_stride, width, height); |
| 526 | } |
| 527 |
| 528 | static void RotatePlane180_16(const uint16_t* src, |
| 529 | int src_stride, |
| 530 | uint16_t* dst, |
| 531 | int dst_stride, |
| 532 | int width, |
| 533 | int height) { |
| 534 | // Swap top and bottom row and mirror the content. Uses a temporary row. |
| 535 | align_buffer_64_16(row, width); |
| 536 | const uint16_t* src_bot = src + src_stride * (height - 1); |
| 537 | uint16_t* dst_bot = dst + dst_stride * (height - 1); |
| 538 | int half_height = (height + 1) >> 1; |
| 539 | int y; |
| 540 |
| 541 | // Odd height will harmlessly mirror the middle row twice. |
| 542 | for (y = 0; y < half_height; ++y) { |
| 543 | CopyRow_16_C(src, row, width); // Copy top row into buffer |
| 544 | MirrorRow_16_C(src_bot, dst, width); // Mirror bottom row into top row |
| 545 | MirrorRow_16_C(row, dst_bot, width); // Mirror buffer into bottom row |
| 546 | src += src_stride; |
| 547 | dst += dst_stride; |
| 548 | src_bot -= src_stride; |
| 549 | dst_bot -= dst_stride; |
| 550 | } |
| 551 | free_aligned_buffer_64_16(row); |
| 552 | } |
| 553 |
| 554 | LIBYUV_API |
| 555 | int RotatePlane_16(const uint16_t* src, |
| 556 | int src_stride, |
| 557 | uint16_t* dst, |
| 558 | int dst_stride, |
| 559 | int width, |
| 560 | int height, |
| 561 | enum RotationMode mode) { |
| 562 | if (!src || width <= 0 || height == 0 || !dst) { |
| 563 | return -1; |
| 564 | } |
| 565 |
| 566 | // Negative height means invert the image. |
| 567 | if (height < 0) { |
| 568 | height = -height; |
| 569 | src = src + (height - 1) * src_stride; |
| 570 | src_stride = -src_stride; |
| 571 | } |
| 572 |
| 573 | switch (mode) { |
| 574 | case kRotate0: |
| 575 | // copy frame |
| 576 | CopyPlane_16(src, src_stride, dst, dst_stride, width, height); |
| 577 | return 0; |
| 578 | case kRotate90: |
| 579 | RotatePlane90_16(src, src_stride, dst, dst_stride, width, height); |
| 580 | return 0; |
| 581 | case kRotate270: |
| 582 | RotatePlane270_16(src, src_stride, dst, dst_stride, width, height); |
| 583 | return 0; |
| 584 | case kRotate180: |
| 585 | RotatePlane180_16(src, src_stride, dst, dst_stride, width, height); |
| 586 | return 0; |
| 587 | default: |
| 588 | break; |
| 589 | } |
| 590 | return -1; |
| 591 | } |
| 592 |
| 593 | LIBYUV_API |
| 594 | int I420Rotate(const uint8_t* src_y, |
| 595 | int src_stride_y, |
| 596 | const uint8_t* src_u, |
| 597 | int src_stride_u, |
| 598 | const uint8_t* src_v, |
| 599 | int src_stride_v, |
| 600 | uint8_t* dst_y, |
| 601 | int dst_stride_y, |
| 602 | uint8_t* dst_u, |
| 603 | int dst_stride_u, |
| 604 | uint8_t* dst_v, |
| 605 | int dst_stride_v, |
| 606 | int width, |
| 607 | int height, |
| 608 | enum RotationMode mode) { |
| 609 | int halfwidth = (width + 1) >> 1; |
| 610 | int halfheight = (height + 1) >> 1; |
| 611 | if ((!src_y && dst_y) || !src_u || !src_v || width <= 0 || height == 0 || |
| 612 | !dst_y || !dst_u || !dst_v) { |
| 613 | return -1; |
| 614 | } |
| 615 |
| 616 | // Negative height means invert the image. |
| 617 | if (height < 0) { |
| 618 | height = -height; |
| 619 | halfheight = (height + 1) >> 1; |
| 620 | src_y = src_y + (height - 1) * src_stride_y; |
| 621 | src_u = src_u + (halfheight - 1) * src_stride_u; |
| 622 | src_v = src_v + (halfheight - 1) * src_stride_v; |
| 623 | src_stride_y = -src_stride_y; |
| 624 | src_stride_u = -src_stride_u; |
| 625 | src_stride_v = -src_stride_v; |
| 626 | } |
| 627 |
| 628 | switch (mode) { |
| 629 | case kRotate0: |
| 630 | // copy frame |
| 631 | return I420Copy(src_y, src_stride_y, src_u, src_stride_u, src_v, |
| 632 | src_stride_v, dst_y, dst_stride_y, dst_u, dst_stride_u, |
| 633 | dst_v, dst_stride_v, width, height); |
| 634 | case kRotate90: |
| 635 | RotatePlane90(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 636 | RotatePlane90(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, |
| 637 | halfheight); |
| 638 | RotatePlane90(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, |
| 639 | halfheight); |
| 640 | return 0; |
| 641 | case kRotate270: |
| 642 | RotatePlane270(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 643 | RotatePlane270(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, |
| 644 | halfheight); |
| 645 | RotatePlane270(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, |
| 646 | halfheight); |
| 647 | return 0; |
| 648 | case kRotate180: |
| 649 | RotatePlane180(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 650 | RotatePlane180(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, |
| 651 | halfheight); |
| 652 | RotatePlane180(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, |
| 653 | halfheight); |
| 654 | return 0; |
| 655 | default: |
| 656 | break; |
| 657 | } |
| 658 | return -1; |
| 659 | } |
| 660 |
| 661 | // I422 has half width x full height UV planes, so rotate by 90 and 270 |
| 662 | // require scaling to maintain 422 subsampling. |
| 663 | LIBYUV_API |
| 664 | int I422Rotate(const uint8_t* src_y, |
| 665 | int src_stride_y, |
| 666 | const uint8_t* src_u, |
| 667 | int src_stride_u, |
| 668 | const uint8_t* src_v, |
| 669 | int src_stride_v, |
| 670 | uint8_t* dst_y, |
| 671 | int dst_stride_y, |
| 672 | uint8_t* dst_u, |
| 673 | int dst_stride_u, |
| 674 | uint8_t* dst_v, |
| 675 | int dst_stride_v, |
| 676 | int width, |
| 677 | int height, |
| 678 | enum RotationMode mode) { |
| 679 | int halfwidth = (width + 1) >> 1; |
| 680 | int halfheight = (height + 1) >> 1; |
| 681 | if (!src_y || !src_u || !src_v || width <= 0 || height == 0 || !dst_y || |
| 682 | !dst_u || !dst_v) { |
| 683 | return -1; |
| 684 | } |
| 685 | // Negative height means invert the image. |
| 686 | if (height < 0) { |
| 687 | height = -height; |
| 688 | src_y = src_y + (height - 1) * src_stride_y; |
| 689 | src_u = src_u + (height - 1) * src_stride_u; |
| 690 | src_v = src_v + (height - 1) * src_stride_v; |
| 691 | src_stride_y = -src_stride_y; |
| 692 | src_stride_u = -src_stride_u; |
| 693 | src_stride_v = -src_stride_v; |
| 694 | } |
| 695 |
| 696 | switch (mode) { |
| 697 | case kRotate0: |
| 698 | // Copy frame |
| 699 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 700 | CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height); |
| 701 | CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height); |
| 702 | return 0; |
| 703 |
| 704 | // Note on temporary Y plane for UV. |
| 705 | // Rotation of UV first fits within the Y destination plane rows. |
| 706 | // Y plane is width x height |
| 707 | // Y plane rotated is height x width |
| 708 | // UV plane is (width / 2) x height |
| 709 | // UV plane rotated is height x (width / 2) |
| 710 | // UV plane rotated+scaled is (height / 2) x width. |
| 711 | // UV plane rotated is a temporary that fits within the Y plane rotated. |
| 712 |
| 713 | case kRotate90: |
| 714 | RotatePlane90(src_u, src_stride_u, dst_y, dst_stride_y, halfwidth, |
| 715 | height); |
| 716 | ScalePlane(dst_y, dst_stride_y, height, halfwidth, dst_u, dst_stride_u, |
| 717 | halfheight, width, kFilterBilinear); |
| 718 | RotatePlane90(src_v, src_stride_v, dst_y, dst_stride_y, halfwidth, |
| 719 | height); |
| 720 | ScalePlane(dst_y, dst_stride_y, height, halfwidth, dst_v, dst_stride_v, |
| 721 | halfheight, width, kFilterLinear); |
| 722 | RotatePlane90(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 723 | return 0; |
| 724 | case kRotate270: |
| 725 | RotatePlane270(src_u, src_stride_u, dst_y, dst_stride_y, halfwidth, |
| 726 | height); |
| 727 | ScalePlane(dst_y, dst_stride_y, height, halfwidth, dst_u, dst_stride_u, |
| 728 | halfheight, width, kFilterBilinear); |
| 729 | RotatePlane270(src_v, src_stride_v, dst_y, dst_stride_y, halfwidth, |
| 730 | height); |
| 731 | ScalePlane(dst_y, dst_stride_y, height, halfwidth, dst_v, dst_stride_v, |
| 732 | halfheight, width, kFilterLinear); |
| 733 | RotatePlane270(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 734 | return 0; |
| 735 | case kRotate180: |
| 736 | RotatePlane180(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 737 | RotatePlane180(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, |
| 738 | height); |
| 739 | RotatePlane180(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, |
| 740 | height); |
| 741 | return 0; |
| 742 | default: |
| 743 | break; |
| 744 | } |
| 745 | return -1; |
| 746 | } |
| 747 |
| 748 | LIBYUV_API |
| 749 | int I444Rotate(const uint8_t* src_y, |
| 750 | int src_stride_y, |
| 751 | const uint8_t* src_u, |
| 752 | int src_stride_u, |
| 753 | const uint8_t* src_v, |
| 754 | int src_stride_v, |
| 755 | uint8_t* dst_y, |
| 756 | int dst_stride_y, |
| 757 | uint8_t* dst_u, |
| 758 | int dst_stride_u, |
| 759 | uint8_t* dst_v, |
| 760 | int dst_stride_v, |
| 761 | int width, |
| 762 | int height, |
| 763 | enum RotationMode mode) { |
| 764 | if (!src_y || !src_u || !src_v || width <= 0 || height == 0 || !dst_y || |
| 765 | !dst_u || !dst_v) { |
| 766 | return -1; |
| 767 | } |
| 768 |
| 769 | // Negative height means invert the image. |
| 770 | if (height < 0) { |
| 771 | height = -height; |
| 772 | src_y = src_y + (height - 1) * src_stride_y; |
| 773 | src_u = src_u + (height - 1) * src_stride_u; |
| 774 | src_v = src_v + (height - 1) * src_stride_v; |
| 775 | src_stride_y = -src_stride_y; |
| 776 | src_stride_u = -src_stride_u; |
| 777 | src_stride_v = -src_stride_v; |
| 778 | } |
| 779 |
| 780 | switch (mode) { |
| 781 | case kRotate0: |
| 782 | // copy frame |
| 783 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 784 | CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 785 | CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 786 | return 0; |
| 787 | case kRotate90: |
| 788 | RotatePlane90(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 789 | RotatePlane90(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 790 | RotatePlane90(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 791 | return 0; |
| 792 | case kRotate270: |
| 793 | RotatePlane270(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 794 | RotatePlane270(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 795 | RotatePlane270(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 796 | return 0; |
| 797 | case kRotate180: |
| 798 | RotatePlane180(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 799 | RotatePlane180(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 800 | RotatePlane180(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 801 | return 0; |
| 802 | default: |
| 803 | break; |
| 804 | } |
| 805 | return -1; |
| 806 | } |
| 807 |
| 808 | LIBYUV_API |
| 809 | int NV12ToI420Rotate(const uint8_t* src_y, |
| 810 | int src_stride_y, |
| 811 | const uint8_t* src_uv, |
| 812 | int src_stride_uv, |
| 813 | uint8_t* dst_y, |
| 814 | int dst_stride_y, |
| 815 | uint8_t* dst_u, |
| 816 | int dst_stride_u, |
| 817 | uint8_t* dst_v, |
| 818 | int dst_stride_v, |
| 819 | int width, |
| 820 | int height, |
| 821 | enum RotationMode mode) { |
| 822 | int halfwidth = (width + 1) >> 1; |
| 823 | int halfheight = (height + 1) >> 1; |
| 824 | if (!src_y || !src_uv || width <= 0 || height == 0 || !dst_y || !dst_u || |
| 825 | !dst_v) { |
| 826 | return -1; |
| 827 | } |
| 828 |
| 829 | // Negative height means invert the image. |
| 830 | if (height < 0) { |
| 831 | height = -height; |
| 832 | halfheight = (height + 1) >> 1; |
| 833 | src_y = src_y + (height - 1) * src_stride_y; |
| 834 | src_uv = src_uv + (halfheight - 1) * src_stride_uv; |
| 835 | src_stride_y = -src_stride_y; |
| 836 | src_stride_uv = -src_stride_uv; |
| 837 | } |
| 838 |
| 839 | switch (mode) { |
| 840 | case kRotate0: |
| 841 | // copy frame |
| 842 | return NV12ToI420(src_y, src_stride_y, src_uv, src_stride_uv, dst_y, |
| 843 | dst_stride_y, dst_u, dst_stride_u, dst_v, dst_stride_v, |
| 844 | width, height); |
| 845 | case kRotate90: |
| 846 | RotatePlane90(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 847 | SplitRotateUV90(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v, |
| 848 | dst_stride_v, halfwidth, halfheight); |
| 849 | return 0; |
| 850 | case kRotate270: |
| 851 | RotatePlane270(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 852 | SplitRotateUV270(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v, |
| 853 | dst_stride_v, halfwidth, halfheight); |
| 854 | return 0; |
| 855 | case kRotate180: |
| 856 | RotatePlane180(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 857 | SplitRotateUV180(src_uv, src_stride_uv, dst_u, dst_stride_u, dst_v, |
| 858 | dst_stride_v, halfwidth, halfheight); |
| 859 | return 0; |
| 860 | default: |
| 861 | break; |
| 862 | } |
| 863 | return -1; |
| 864 | } |
| 865 |
| 866 | static void SplitPixels(const uint8_t* src_u, |
| 867 | int src_pixel_stride_uv, |
| 868 | uint8_t* dst_u, |
| 869 | int width) { |
| 870 | int i; |
| 871 | for (i = 0; i < width; ++i) { |
| 872 | *dst_u = *src_u; |
| 873 | ++dst_u; |
| 874 | src_u += src_pixel_stride_uv; |
| 875 | } |
| 876 | } |
| 877 |
| 878 | // Convert Android420 to I420 with Rotate |
| 879 | LIBYUV_API |
| 880 | int Android420ToI420Rotate(const uint8_t* src_y, |
| 881 | int src_stride_y, |
| 882 | const uint8_t* src_u, |
| 883 | int src_stride_u, |
| 884 | const uint8_t* src_v, |
| 885 | int src_stride_v, |
| 886 | int src_pixel_stride_uv, |
| 887 | uint8_t* dst_y, |
| 888 | int dst_stride_y, |
| 889 | uint8_t* dst_u, |
| 890 | int dst_stride_u, |
| 891 | uint8_t* dst_v, |
| 892 | int dst_stride_v, |
| 893 | int width, |
| 894 | int height, |
| 895 | enum RotationMode rotation) { |
| 896 | int y; |
| 897 | const ptrdiff_t vu_off = src_v - src_u; |
| 898 | int halfwidth = (width + 1) >> 1; |
| 899 | int halfheight = (height + 1) >> 1; |
| 900 | if ((!src_y && dst_y) || !src_u || !src_v || !dst_u || !dst_v || width <= 0 || |
| 901 | height == 0) { |
| 902 | return -1; |
| 903 | } |
| 904 | // Negative height means invert the image. |
| 905 | if (height < 0) { |
| 906 | height = -height; |
| 907 | halfheight = (height + 1) >> 1; |
| 908 | src_y = src_y + (height - 1) * src_stride_y; |
| 909 | src_u = src_u + (halfheight - 1) * src_stride_u; |
| 910 | src_v = src_v + (halfheight - 1) * src_stride_v; |
| 911 | src_stride_y = -src_stride_y; |
| 912 | src_stride_u = -src_stride_u; |
| 913 | src_stride_v = -src_stride_v; |
| 914 | } |
| 915 |
| 916 | if (dst_y) { |
| 917 | RotatePlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height, |
| 918 | rotation); |
| 919 | } |
| 920 |
| 921 | // Copy UV planes - I420 |
| 922 | if (src_pixel_stride_uv == 1) { |
| 923 | RotatePlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, halfheight, |
| 924 | rotation); |
| 925 | RotatePlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, halfheight, |
| 926 | rotation); |
| 927 | return 0; |
| 928 | } |
| 929 | // Split UV planes - NV21 |
| 930 | if (src_pixel_stride_uv == 2 && vu_off == -1 && |
| 931 | src_stride_u == src_stride_v) { |
| 932 | SplitRotateUV(src_v, src_stride_v, dst_v, dst_stride_v, dst_u, dst_stride_u, |
| 933 | halfwidth, halfheight, rotation); |
| 934 | return 0; |
| 935 | } |
| 936 | // Split UV planes - NV12 |
| 937 | if (src_pixel_stride_uv == 2 && vu_off == 1 && src_stride_u == src_stride_v) { |
| 938 | SplitRotateUV(src_u, src_stride_u, dst_u, dst_stride_u, dst_v, dst_stride_v, |
| 939 | halfwidth, halfheight, rotation); |
| 940 | return 0; |
| 941 | } |
| 942 |
| 943 | if (rotation == 0) { |
| 944 | for (y = 0; y < halfheight; ++y) { |
| 945 | SplitPixels(src_u, src_pixel_stride_uv, dst_u, halfwidth); |
| 946 | SplitPixels(src_v, src_pixel_stride_uv, dst_v, halfwidth); |
| 947 | src_u += src_stride_u; |
| 948 | src_v += src_stride_v; |
| 949 | dst_u += dst_stride_u; |
| 950 | dst_v += dst_stride_v; |
| 951 | } |
| 952 | return 0; |
| 953 | } |
| 954 | // unsupported type and/or rotation. |
| 955 | return -1; |
| 956 | } |
| 957 |
| 958 | LIBYUV_API |
| 959 | int I010Rotate(const uint16_t* src_y, |
| 960 | int src_stride_y, |
| 961 | const uint16_t* src_u, |
| 962 | int src_stride_u, |
| 963 | const uint16_t* src_v, |
| 964 | int src_stride_v, |
| 965 | uint16_t* dst_y, |
| 966 | int dst_stride_y, |
| 967 | uint16_t* dst_u, |
| 968 | int dst_stride_u, |
| 969 | uint16_t* dst_v, |
| 970 | int dst_stride_v, |
| 971 | int width, |
| 972 | int height, |
| 973 | enum RotationMode mode) { |
| 974 | int halfwidth = (width + 1) >> 1; |
| 975 | int halfheight = (height + 1) >> 1; |
| 976 | if (!src_y || !src_u || !src_v || width <= 0 || height == 0 || !dst_y || |
| 977 | !dst_u || !dst_v || dst_stride_y < 0) { |
| 978 | return -1; |
| 979 | } |
| 980 | // Negative height means invert the image. |
| 981 | if (height < 0) { |
| 982 | height = -height; |
| 983 | src_y = src_y + (height - 1) * src_stride_y; |
| 984 | src_u = src_u + (height - 1) * src_stride_u; |
| 985 | src_v = src_v + (height - 1) * src_stride_v; |
| 986 | src_stride_y = -src_stride_y; |
| 987 | src_stride_u = -src_stride_u; |
| 988 | src_stride_v = -src_stride_v; |
| 989 | } |
| 990 |
| 991 | switch (mode) { |
| 992 | case kRotate0: |
| 993 | // copy frame |
| 994 | return I010Copy(src_y, src_stride_y, src_u, src_stride_u, src_v, |
| 995 | src_stride_v, dst_y, dst_stride_y, dst_u, dst_stride_u, |
| 996 | dst_v, dst_stride_v, width, height); |
| 997 | case kRotate90: |
| 998 | RotatePlane90_16(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 999 | RotatePlane90_16(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, |
| 1000 | halfheight); |
| 1001 | RotatePlane90_16(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, |
| 1002 | halfheight); |
| 1003 | return 0; |
| 1004 | case kRotate270: |
| 1005 | RotatePlane270_16(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 1006 | height); |
| 1007 | RotatePlane270_16(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, |
| 1008 | halfheight); |
| 1009 | RotatePlane270_16(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, |
| 1010 | halfheight); |
| 1011 | return 0; |
| 1012 | case kRotate180: |
| 1013 | RotatePlane180_16(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 1014 | height); |
| 1015 | RotatePlane180_16(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, |
| 1016 | halfheight); |
| 1017 | RotatePlane180_16(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, |
| 1018 | halfheight); |
| 1019 | return 0; |
| 1020 | default: |
| 1021 | break; |
| 1022 | } |
| 1023 | return -1; |
| 1024 | } |
| 1025 |
| 1026 | // I210 has half width x full height UV planes, so rotate by 90 and 270 |
| 1027 | // require scaling to maintain 422 subsampling. |
| 1028 | LIBYUV_API |
| 1029 | int I210Rotate(const uint16_t* src_y, |
| 1030 | int src_stride_y, |
| 1031 | const uint16_t* src_u, |
| 1032 | int src_stride_u, |
| 1033 | const uint16_t* src_v, |
| 1034 | int src_stride_v, |
| 1035 | uint16_t* dst_y, |
| 1036 | int dst_stride_y, |
| 1037 | uint16_t* dst_u, |
| 1038 | int dst_stride_u, |
| 1039 | uint16_t* dst_v, |
| 1040 | int dst_stride_v, |
| 1041 | int width, |
| 1042 | int height, |
| 1043 | enum RotationMode mode) { |
| 1044 | int halfwidth = (width + 1) >> 1; |
| 1045 | int halfheight = (height + 1) >> 1; |
| 1046 | if (!src_y || !src_u || !src_v || width <= 0 || height == 0 || !dst_y || |
| 1047 | !dst_u || !dst_v) { |
| 1048 | return -1; |
| 1049 | } |
| 1050 | // Negative height means invert the image. |
| 1051 | if (height < 0) { |
| 1052 | height = -height; |
| 1053 | src_y = src_y + (height - 1) * src_stride_y; |
| 1054 | src_u = src_u + (height - 1) * src_stride_u; |
| 1055 | src_v = src_v + (height - 1) * src_stride_v; |
| 1056 | src_stride_y = -src_stride_y; |
| 1057 | src_stride_u = -src_stride_u; |
| 1058 | src_stride_v = -src_stride_v; |
| 1059 | } |
| 1060 |
| 1061 | switch (mode) { |
| 1062 | case kRotate0: |
| 1063 | // Copy frame |
| 1064 | CopyPlane_16(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 1065 | CopyPlane_16(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height); |
| 1066 | CopyPlane_16(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height); |
| 1067 | return 0; |
| 1068 |
| 1069 | // Note on temporary Y plane for UV. |
| 1070 | // Rotation of UV first fits within the Y destination plane rows. |
| 1071 | // Y plane is width x height |
| 1072 | // Y plane rotated is height x width |
| 1073 | // UV plane is (width / 2) x height |
| 1074 | // UV plane rotated is height x (width / 2) |
| 1075 | // UV plane rotated+scaled is (height / 2) x width. |
| 1076 | // UV plane rotated is a temporary that fits within the Y plane rotated. |
| 1077 |
| 1078 | case kRotate90: |
| 1079 | RotatePlane90_16(src_u, src_stride_u, dst_y, dst_stride_y, halfwidth, |
| 1080 | height); |
| 1081 | ScalePlane_16(dst_y, dst_stride_y, height, halfwidth, dst_u, dst_stride_u, |
| 1082 | halfheight, width, kFilterBilinear); |
| 1083 | RotatePlane90_16(src_v, src_stride_v, dst_y, dst_stride_y, halfwidth, |
| 1084 | height); |
| 1085 | ScalePlane_16(dst_y, dst_stride_y, height, halfwidth, dst_v, dst_stride_v, |
| 1086 | halfheight, width, kFilterLinear); |
| 1087 | RotatePlane90_16(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 1088 | return 0; |
| 1089 | case kRotate270: |
| 1090 | RotatePlane270_16(src_u, src_stride_u, dst_y, dst_stride_y, halfwidth, |
| 1091 | height); |
| 1092 | ScalePlane_16(dst_y, dst_stride_y, height, halfwidth, dst_u, dst_stride_u, |
| 1093 | halfheight, width, kFilterBilinear); |
| 1094 | RotatePlane270_16(src_v, src_stride_v, dst_y, dst_stride_y, halfwidth, |
| 1095 | height); |
| 1096 | ScalePlane_16(dst_y, dst_stride_y, height, halfwidth, dst_v, dst_stride_v, |
| 1097 | halfheight, width, kFilterLinear); |
| 1098 | RotatePlane270_16(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 1099 | height); |
| 1100 | return 0; |
| 1101 | case kRotate180: |
| 1102 | RotatePlane180_16(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 1103 | height); |
| 1104 | RotatePlane180_16(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, |
| 1105 | height); |
| 1106 | RotatePlane180_16(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, |
| 1107 | height); |
| 1108 | return 0; |
| 1109 | default: |
| 1110 | break; |
| 1111 | } |
| 1112 | return -1; |
| 1113 | } |
| 1114 |
| 1115 | LIBYUV_API |
| 1116 | int I410Rotate(const uint16_t* src_y, |
| 1117 | int src_stride_y, |
| 1118 | const uint16_t* src_u, |
| 1119 | int src_stride_u, |
| 1120 | const uint16_t* src_v, |
| 1121 | int src_stride_v, |
| 1122 | uint16_t* dst_y, |
| 1123 | int dst_stride_y, |
| 1124 | uint16_t* dst_u, |
| 1125 | int dst_stride_u, |
| 1126 | uint16_t* dst_v, |
| 1127 | int dst_stride_v, |
| 1128 | int width, |
| 1129 | int height, |
| 1130 | enum RotationMode mode) { |
| 1131 | if (!src_y || !src_u || !src_v || width <= 0 || height == 0 || !dst_y || |
| 1132 | !dst_u || !dst_v || dst_stride_y < 0) { |
| 1133 | return -1; |
| 1134 | } |
| 1135 | // Negative height means invert the image. |
| 1136 | if (height < 0) { |
| 1137 | height = -height; |
| 1138 | src_y = src_y + (height - 1) * src_stride_y; |
| 1139 | src_u = src_u + (height - 1) * src_stride_u; |
| 1140 | src_v = src_v + (height - 1) * src_stride_v; |
| 1141 | src_stride_y = -src_stride_y; |
| 1142 | src_stride_u = -src_stride_u; |
| 1143 | src_stride_v = -src_stride_v; |
| 1144 | } |
| 1145 |
| 1146 | switch (mode) { |
| 1147 | case kRotate0: |
| 1148 | // copy frame |
| 1149 | CopyPlane_16(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 1150 | CopyPlane_16(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 1151 | CopyPlane_16(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 1152 | return 0; |
| 1153 | case kRotate90: |
| 1154 | RotatePlane90_16(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 1155 | RotatePlane90_16(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 1156 | RotatePlane90_16(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 1157 | return 0; |
| 1158 | case kRotate270: |
| 1159 | RotatePlane270_16(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 1160 | height); |
| 1161 | RotatePlane270_16(src_u, src_stride_u, dst_u, dst_stride_u, width, |
| 1162 | height); |
| 1163 | RotatePlane270_16(src_v, src_stride_v, dst_v, dst_stride_v, width, |
| 1164 | height); |
| 1165 | return 0; |
| 1166 | case kRotate180: |
| 1167 | RotatePlane180_16(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 1168 | height); |
| 1169 | RotatePlane180_16(src_u, src_stride_u, dst_u, dst_stride_u, width, |
| 1170 | height); |
| 1171 | RotatePlane180_16(src_v, src_stride_v, dst_v, dst_stride_v, width, |
| 1172 | height); |
| 1173 | return 0; |
| 1174 | default: |
| 1175 | break; |
| 1176 | } |
| 1177 | return -1; |
| 1178 | } |
| 1179 |
| 1180 | #ifdef __cplusplus |
| 1181 | } // extern "C" |
| 1182 | } // namespace libyuv |
| 1183 | #endif |
| 1184 |