| 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/planar_functions.h" |
| 12 |
| 13 | #include <string.h> // for memset() |
| 14 |
| 15 | #include "libyuv/cpu_id.h" |
| 16 | #ifdef HAVE_JPEG |
| 17 | #include "libyuv/mjpeg_decoder.h" |
| 18 | #endif |
| 19 | #include "libyuv/row.h" |
| 20 | #include "libyuv/scale_row.h" // for ScaleRowDown2 |
| 21 |
| 22 | #ifdef __cplusplus |
| 23 | namespace libyuv { |
| 24 | extern "C" { |
| 25 | #endif |
| 26 |
| 27 | // Copy a plane of data |
| 28 | LIBYUV_API |
| 29 | void CopyPlane(const uint8_t* src_y, |
| 30 | int src_stride_y, |
| 31 | uint8_t* dst_y, |
| 32 | int dst_stride_y, |
| 33 | int width, |
| 34 | int height) { |
| 35 | int y; |
| 36 | void (*CopyRow)(const uint8_t* src, uint8_t* dst, int width) = CopyRow_C; |
| 37 | // Negative height means invert the image. |
| 38 | if (height < 0) { |
| 39 | height = -height; |
| 40 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 41 | dst_stride_y = -dst_stride_y; |
| 42 | } |
| 43 | // Coalesce rows. |
| 44 | if (src_stride_y == width && dst_stride_y == width) { |
| 45 | width *= height; |
| 46 | height = 1; |
| 47 | src_stride_y = dst_stride_y = 0; |
| 48 | } |
| 49 | // Nothing to do. |
| 50 | if (src_y == dst_y && src_stride_y == dst_stride_y) { |
| 51 | return; |
| 52 | } |
| 53 |
| 54 | #if defined(HAS_COPYROW_SSE2) |
| 55 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 56 | CopyRow = IS_ALIGNED(width, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2; |
| 57 | } |
| 58 | #endif |
| 59 | #if defined(HAS_COPYROW_AVX) |
| 60 | if (TestCpuFlag(kCpuHasAVX)) { |
| 61 | CopyRow = IS_ALIGNED(width, 64) ? CopyRow_AVX : CopyRow_Any_AVX; |
| 62 | } |
| 63 | #endif |
| 64 | #if defined(HAS_COPYROW_ERMS) |
| 65 | if (TestCpuFlag(kCpuHasERMS)) { |
| 66 | CopyRow = CopyRow_ERMS; |
| 67 | } |
| 68 | #endif |
| 69 | #if defined(HAS_COPYROW_NEON) |
| 70 | if (TestCpuFlag(kCpuHasNEON)) { |
| 71 | CopyRow = IS_ALIGNED(width, 32) ? CopyRow_NEON : CopyRow_Any_NEON; |
| 72 | } |
| 73 | #endif |
| 74 |
| 75 | // Copy plane |
| 76 | for (y = 0; y < height; ++y) { |
| 77 | CopyRow(src_y, dst_y, width); |
| 78 | src_y += src_stride_y; |
| 79 | dst_y += dst_stride_y; |
| 80 | } |
| 81 | } |
| 82 |
| 83 | // TODO(fbarchard): Consider support for negative height. |
| 84 | // TODO(fbarchard): Consider stride measured in bytes. |
| 85 | LIBYUV_API |
| 86 | void CopyPlane_16(const uint16_t* src_y, |
| 87 | int src_stride_y, |
| 88 | uint16_t* dst_y, |
| 89 | int dst_stride_y, |
| 90 | int width, |
| 91 | int height) { |
| 92 | int y; |
| 93 | void (*CopyRow)(const uint16_t* src, uint16_t* dst, int width) = CopyRow_16_C; |
| 94 | // Coalesce rows. |
| 95 | if (src_stride_y == width && dst_stride_y == width) { |
| 96 | width *= height; |
| 97 | height = 1; |
| 98 | src_stride_y = dst_stride_y = 0; |
| 99 | } |
| 100 | #if defined(HAS_COPYROW_16_SSE2) |
| 101 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 32)) { |
| 102 | CopyRow = CopyRow_16_SSE2; |
| 103 | } |
| 104 | #endif |
| 105 | #if defined(HAS_COPYROW_16_ERMS) |
| 106 | if (TestCpuFlag(kCpuHasERMS)) { |
| 107 | CopyRow = CopyRow_16_ERMS; |
| 108 | } |
| 109 | #endif |
| 110 | #if defined(HAS_COPYROW_16_NEON) |
| 111 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 32)) { |
| 112 | CopyRow = CopyRow_16_NEON; |
| 113 | } |
| 114 | #endif |
| 115 |
| 116 | // Copy plane |
| 117 | for (y = 0; y < height; ++y) { |
| 118 | CopyRow(src_y, dst_y, width); |
| 119 | src_y += src_stride_y; |
| 120 | dst_y += dst_stride_y; |
| 121 | } |
| 122 | } |
| 123 |
| 124 | // Convert a plane of 16 bit data to 8 bit |
| 125 | LIBYUV_API |
| 126 | void Convert16To8Plane(const uint16_t* src_y, |
| 127 | int src_stride_y, |
| 128 | uint8_t* dst_y, |
| 129 | int dst_stride_y, |
| 130 | int scale, // 16384 for 10 bits |
| 131 | int width, |
| 132 | int height) { |
| 133 | int y; |
| 134 | void (*Convert16To8Row)(const uint16_t* src_y, uint8_t* dst_y, int scale, |
| 135 | int width) = Convert16To8Row_C; |
| 136 |
| 137 | // Negative height means invert the image. |
| 138 | if (height < 0) { |
| 139 | height = -height; |
| 140 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 141 | dst_stride_y = -dst_stride_y; |
| 142 | } |
| 143 | // Coalesce rows. |
| 144 | if (src_stride_y == width && dst_stride_y == width) { |
| 145 | width *= height; |
| 146 | height = 1; |
| 147 | src_stride_y = dst_stride_y = 0; |
| 148 | } |
| 149 | #if defined(HAS_CONVERT16TO8ROW_SSSE3) |
| 150 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 151 | Convert16To8Row = Convert16To8Row_Any_SSSE3; |
| 152 | if (IS_ALIGNED(width, 16)) { |
| 153 | Convert16To8Row = Convert16To8Row_SSSE3; |
| 154 | } |
| 155 | } |
| 156 | #endif |
| 157 | #if defined(HAS_CONVERT16TO8ROW_AVX2) |
| 158 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 159 | Convert16To8Row = Convert16To8Row_Any_AVX2; |
| 160 | if (IS_ALIGNED(width, 32)) { |
| 161 | Convert16To8Row = Convert16To8Row_AVX2; |
| 162 | } |
| 163 | } |
| 164 | #endif |
| 165 |
| 166 | // Convert plane |
| 167 | for (y = 0; y < height; ++y) { |
| 168 | Convert16To8Row(src_y, dst_y, scale, width); |
| 169 | src_y += src_stride_y; |
| 170 | dst_y += dst_stride_y; |
| 171 | } |
| 172 | } |
| 173 |
| 174 | // Convert a plane of 8 bit data to 16 bit |
| 175 | LIBYUV_API |
| 176 | void Convert8To16Plane(const uint8_t* src_y, |
| 177 | int src_stride_y, |
| 178 | uint16_t* dst_y, |
| 179 | int dst_stride_y, |
| 180 | int scale, // 16384 for 10 bits |
| 181 | int width, |
| 182 | int height) { |
| 183 | int y; |
| 184 | void (*Convert8To16Row)(const uint8_t* src_y, uint16_t* dst_y, int scale, |
| 185 | int width) = Convert8To16Row_C; |
| 186 |
| 187 | // Negative height means invert the image. |
| 188 | if (height < 0) { |
| 189 | height = -height; |
| 190 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 191 | dst_stride_y = -dst_stride_y; |
| 192 | } |
| 193 | // Coalesce rows. |
| 194 | if (src_stride_y == width && dst_stride_y == width) { |
| 195 | width *= height; |
| 196 | height = 1; |
| 197 | src_stride_y = dst_stride_y = 0; |
| 198 | } |
| 199 | #if defined(HAS_CONVERT8TO16ROW_SSE2) |
| 200 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 201 | Convert8To16Row = Convert8To16Row_Any_SSE2; |
| 202 | if (IS_ALIGNED(width, 16)) { |
| 203 | Convert8To16Row = Convert8To16Row_SSE2; |
| 204 | } |
| 205 | } |
| 206 | #endif |
| 207 | #if defined(HAS_CONVERT8TO16ROW_AVX2) |
| 208 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 209 | Convert8To16Row = Convert8To16Row_Any_AVX2; |
| 210 | if (IS_ALIGNED(width, 32)) { |
| 211 | Convert8To16Row = Convert8To16Row_AVX2; |
| 212 | } |
| 213 | } |
| 214 | #endif |
| 215 |
| 216 | // Convert plane |
| 217 | for (y = 0; y < height; ++y) { |
| 218 | Convert8To16Row(src_y, dst_y, scale, width); |
| 219 | src_y += src_stride_y; |
| 220 | dst_y += dst_stride_y; |
| 221 | } |
| 222 | } |
| 223 |
| 224 | // Copy I422. |
| 225 | LIBYUV_API |
| 226 | int I422Copy(const uint8_t* src_y, |
| 227 | int src_stride_y, |
| 228 | const uint8_t* src_u, |
| 229 | int src_stride_u, |
| 230 | const uint8_t* src_v, |
| 231 | int src_stride_v, |
| 232 | uint8_t* dst_y, |
| 233 | int dst_stride_y, |
| 234 | uint8_t* dst_u, |
| 235 | int dst_stride_u, |
| 236 | uint8_t* dst_v, |
| 237 | int dst_stride_v, |
| 238 | int width, |
| 239 | int height) { |
| 240 | int halfwidth = (width + 1) >> 1; |
| 241 | if (!src_u || !src_v || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 242 | return -1; |
| 243 | } |
| 244 | // Negative height means invert the image. |
| 245 | if (height < 0) { |
| 246 | height = -height; |
| 247 | src_y = src_y + (height - 1) * src_stride_y; |
| 248 | src_u = src_u + (height - 1) * src_stride_u; |
| 249 | src_v = src_v + (height - 1) * src_stride_v; |
| 250 | src_stride_y = -src_stride_y; |
| 251 | src_stride_u = -src_stride_u; |
| 252 | src_stride_v = -src_stride_v; |
| 253 | } |
| 254 |
| 255 | if (dst_y) { |
| 256 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 257 | } |
| 258 | CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height); |
| 259 | CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height); |
| 260 | return 0; |
| 261 | } |
| 262 |
| 263 | // Copy I444. |
| 264 | LIBYUV_API |
| 265 | int I444Copy(const uint8_t* src_y, |
| 266 | int src_stride_y, |
| 267 | const uint8_t* src_u, |
| 268 | int src_stride_u, |
| 269 | const uint8_t* src_v, |
| 270 | int src_stride_v, |
| 271 | uint8_t* dst_y, |
| 272 | int dst_stride_y, |
| 273 | uint8_t* dst_u, |
| 274 | int dst_stride_u, |
| 275 | uint8_t* dst_v, |
| 276 | int dst_stride_v, |
| 277 | int width, |
| 278 | int height) { |
| 279 | if (!src_u || !src_v || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 280 | return -1; |
| 281 | } |
| 282 | // Negative height means invert the image. |
| 283 | if (height < 0) { |
| 284 | height = -height; |
| 285 | src_y = src_y + (height - 1) * src_stride_y; |
| 286 | src_u = src_u + (height - 1) * src_stride_u; |
| 287 | src_v = src_v + (height - 1) * src_stride_v; |
| 288 | src_stride_y = -src_stride_y; |
| 289 | src_stride_u = -src_stride_u; |
| 290 | src_stride_v = -src_stride_v; |
| 291 | } |
| 292 |
| 293 | if (dst_y) { |
| 294 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 295 | } |
| 296 | CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 297 | CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 298 | return 0; |
| 299 | } |
| 300 |
| 301 | // Copy I400. |
| 302 | LIBYUV_API |
| 303 | int I400ToI400(const uint8_t* src_y, |
| 304 | int src_stride_y, |
| 305 | uint8_t* dst_y, |
| 306 | int dst_stride_y, |
| 307 | int width, |
| 308 | int height) { |
| 309 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 310 | return -1; |
| 311 | } |
| 312 | // Negative height means invert the image. |
| 313 | if (height < 0) { |
| 314 | height = -height; |
| 315 | src_y = src_y + (height - 1) * src_stride_y; |
| 316 | src_stride_y = -src_stride_y; |
| 317 | } |
| 318 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 319 | return 0; |
| 320 | } |
| 321 |
| 322 | // Convert I420 to I400. |
| 323 | LIBYUV_API |
| 324 | int I420ToI400(const uint8_t* src_y, |
| 325 | int src_stride_y, |
| 326 | const uint8_t* src_u, |
| 327 | int src_stride_u, |
| 328 | const uint8_t* src_v, |
| 329 | int src_stride_v, |
| 330 | uint8_t* dst_y, |
| 331 | int dst_stride_y, |
| 332 | int width, |
| 333 | int height) { |
| 334 | (void)src_u; |
| 335 | (void)src_stride_u; |
| 336 | (void)src_v; |
| 337 | (void)src_stride_v; |
| 338 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 339 | return -1; |
| 340 | } |
| 341 | // Negative height means invert the image. |
| 342 | if (height < 0) { |
| 343 | height = -height; |
| 344 | src_y = src_y + (height - 1) * src_stride_y; |
| 345 | src_stride_y = -src_stride_y; |
| 346 | } |
| 347 |
| 348 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 349 | return 0; |
| 350 | } |
| 351 |
| 352 | // Support function for NV12 etc UV channels. |
| 353 | // Width and height are plane sizes (typically half pixel width). |
| 354 | LIBYUV_API |
| 355 | void SplitUVPlane(const uint8_t* src_uv, |
| 356 | int src_stride_uv, |
| 357 | uint8_t* dst_u, |
| 358 | int dst_stride_u, |
| 359 | uint8_t* dst_v, |
| 360 | int dst_stride_v, |
| 361 | int width, |
| 362 | int height) { |
| 363 | int y; |
| 364 | void (*SplitUVRow)(const uint8_t* src_uv, uint8_t* dst_u, uint8_t* dst_v, |
| 365 | int width) = SplitUVRow_C; |
| 366 | // Negative height means invert the image. |
| 367 | if (height < 0) { |
| 368 | height = -height; |
| 369 | dst_u = dst_u + (height - 1) * dst_stride_u; |
| 370 | dst_v = dst_v + (height - 1) * dst_stride_v; |
| 371 | dst_stride_u = -dst_stride_u; |
| 372 | dst_stride_v = -dst_stride_v; |
| 373 | } |
| 374 | // Coalesce rows. |
| 375 | if (src_stride_uv == width * 2 && dst_stride_u == width && |
| 376 | dst_stride_v == width) { |
| 377 | width *= height; |
| 378 | height = 1; |
| 379 | src_stride_uv = dst_stride_u = dst_stride_v = 0; |
| 380 | } |
| 381 | #if defined(HAS_SPLITUVROW_SSE2) |
| 382 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 383 | SplitUVRow = SplitUVRow_Any_SSE2; |
| 384 | if (IS_ALIGNED(width, 16)) { |
| 385 | SplitUVRow = SplitUVRow_SSE2; |
| 386 | } |
| 387 | } |
| 388 | #endif |
| 389 | #if defined(HAS_SPLITUVROW_AVX2) |
| 390 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 391 | SplitUVRow = SplitUVRow_Any_AVX2; |
| 392 | if (IS_ALIGNED(width, 32)) { |
| 393 | SplitUVRow = SplitUVRow_AVX2; |
| 394 | } |
| 395 | } |
| 396 | #endif |
| 397 | #if defined(HAS_SPLITUVROW_NEON) |
| 398 | if (TestCpuFlag(kCpuHasNEON)) { |
| 399 | SplitUVRow = SplitUVRow_Any_NEON; |
| 400 | if (IS_ALIGNED(width, 16)) { |
| 401 | SplitUVRow = SplitUVRow_NEON; |
| 402 | } |
| 403 | } |
| 404 | #endif |
| 405 | #if defined(HAS_SPLITUVROW_MSA) |
| 406 | if (TestCpuFlag(kCpuHasMSA)) { |
| 407 | SplitUVRow = SplitUVRow_Any_MSA; |
| 408 | if (IS_ALIGNED(width, 32)) { |
| 409 | SplitUVRow = SplitUVRow_MSA; |
| 410 | } |
| 411 | } |
| 412 | #endif |
| 413 | #if defined(HAS_SPLITUVROW_MMI) |
| 414 | if (TestCpuFlag(kCpuHasMMI)) { |
| 415 | SplitUVRow = SplitUVRow_Any_MMI; |
| 416 | if (IS_ALIGNED(width, 8)) { |
| 417 | SplitUVRow = SplitUVRow_MMI; |
| 418 | } |
| 419 | } |
| 420 | #endif |
| 421 |
| 422 | for (y = 0; y < height; ++y) { |
| 423 | // Copy a row of UV. |
| 424 | SplitUVRow(src_uv, dst_u, dst_v, width); |
| 425 | dst_u += dst_stride_u; |
| 426 | dst_v += dst_stride_v; |
| 427 | src_uv += src_stride_uv; |
| 428 | } |
| 429 | } |
| 430 |
| 431 | LIBYUV_API |
| 432 | void MergeUVPlane(const uint8_t* src_u, |
| 433 | int src_stride_u, |
| 434 | const uint8_t* src_v, |
| 435 | int src_stride_v, |
| 436 | uint8_t* dst_uv, |
| 437 | int dst_stride_uv, |
| 438 | int width, |
| 439 | int height) { |
| 440 | int y; |
| 441 | void (*MergeUVRow)(const uint8_t* src_u, const uint8_t* src_v, |
| 442 | uint8_t* dst_uv, int width) = MergeUVRow_C; |
| 443 | // Negative height means invert the image. |
| 444 | if (height < 0) { |
| 445 | height = -height; |
| 446 | dst_uv = dst_uv + (height - 1) * dst_stride_uv; |
| 447 | dst_stride_uv = -dst_stride_uv; |
| 448 | } |
| 449 | // Coalesce rows. |
| 450 | if (src_stride_u == width && src_stride_v == width && |
| 451 | dst_stride_uv == width * 2) { |
| 452 | width *= height; |
| 453 | height = 1; |
| 454 | src_stride_u = src_stride_v = dst_stride_uv = 0; |
| 455 | } |
| 456 | #if defined(HAS_MERGEUVROW_SSE2) |
| 457 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 458 | MergeUVRow = MergeUVRow_Any_SSE2; |
| 459 | if (IS_ALIGNED(width, 16)) { |
| 460 | MergeUVRow = MergeUVRow_SSE2; |
| 461 | } |
| 462 | } |
| 463 | #endif |
| 464 | #if defined(HAS_MERGEUVROW_AVX2) |
| 465 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 466 | MergeUVRow = MergeUVRow_Any_AVX2; |
| 467 | if (IS_ALIGNED(width, 32)) { |
| 468 | MergeUVRow = MergeUVRow_AVX2; |
| 469 | } |
| 470 | } |
| 471 | #endif |
| 472 | #if defined(HAS_MERGEUVROW_NEON) |
| 473 | if (TestCpuFlag(kCpuHasNEON)) { |
| 474 | MergeUVRow = MergeUVRow_Any_NEON; |
| 475 | if (IS_ALIGNED(width, 16)) { |
| 476 | MergeUVRow = MergeUVRow_NEON; |
| 477 | } |
| 478 | } |
| 479 | #endif |
| 480 | #if defined(HAS_MERGEUVROW_MSA) |
| 481 | if (TestCpuFlag(kCpuHasMSA)) { |
| 482 | MergeUVRow = MergeUVRow_Any_MSA; |
| 483 | if (IS_ALIGNED(width, 16)) { |
| 484 | MergeUVRow = MergeUVRow_MSA; |
| 485 | } |
| 486 | } |
| 487 | #endif |
| 488 | #if defined(HAS_MERGEUVROW_MMI) |
| 489 | if (TestCpuFlag(kCpuHasMMI)) { |
| 490 | MergeUVRow = MergeUVRow_Any_MMI; |
| 491 | if (IS_ALIGNED(width, 8)) { |
| 492 | MergeUVRow = MergeUVRow_MMI; |
| 493 | } |
| 494 | } |
| 495 | #endif |
| 496 |
| 497 | for (y = 0; y < height; ++y) { |
| 498 | // Merge a row of U and V into a row of UV. |
| 499 | MergeUVRow(src_u, src_v, dst_uv, width); |
| 500 | src_u += src_stride_u; |
| 501 | src_v += src_stride_v; |
| 502 | dst_uv += dst_stride_uv; |
| 503 | } |
| 504 | } |
| 505 |
| 506 | // Swap U and V channels in interleaved UV plane. |
| 507 | LIBYUV_API |
| 508 | void SwapUVPlane(const uint8_t* src_uv, |
| 509 | int src_stride_uv, |
| 510 | uint8_t* dst_vu, |
| 511 | int dst_stride_vu, |
| 512 | int width, |
| 513 | int height) { |
| 514 | int y; |
| 515 | void (*SwapUVRow)(const uint8_t* src_uv, uint8_t* dst_vu, int width) = |
| 516 | SwapUVRow_C; |
| 517 | // Negative height means invert the image. |
| 518 | if (height < 0) { |
| 519 | height = -height; |
| 520 | src_uv = src_uv + (height - 1) * src_stride_uv; |
| 521 | src_stride_uv = -src_stride_uv; |
| 522 | } |
| 523 | // Coalesce rows. |
| 524 | if (src_stride_uv == width * 2 && dst_stride_vu == width * 2) { |
| 525 | width *= height; |
| 526 | height = 1; |
| 527 | src_stride_uv = dst_stride_vu = 0; |
| 528 | } |
| 529 |
| 530 | #if defined(HAS_SWAPUVROW_SSSE3) |
| 531 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 532 | SwapUVRow = SwapUVRow_Any_SSSE3; |
| 533 | if (IS_ALIGNED(width, 16)) { |
| 534 | SwapUVRow = SwapUVRow_SSSE3; |
| 535 | } |
| 536 | } |
| 537 | #endif |
| 538 | #if defined(HAS_SWAPUVROW_AVX2) |
| 539 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 540 | SwapUVRow = SwapUVRow_Any_AVX2; |
| 541 | if (IS_ALIGNED(width, 32)) { |
| 542 | SwapUVRow = SwapUVRow_AVX2; |
| 543 | } |
| 544 | } |
| 545 | #endif |
| 546 | #if defined(HAS_SWAPUVROW_NEON) |
| 547 | if (TestCpuFlag(kCpuHasNEON)) { |
| 548 | SwapUVRow = SwapUVRow_Any_NEON; |
| 549 | if (IS_ALIGNED(width, 16)) { |
| 550 | SwapUVRow = SwapUVRow_NEON; |
| 551 | } |
| 552 | } |
| 553 | #endif |
| 554 |
| 555 | for (y = 0; y < height; ++y) { |
| 556 | SwapUVRow(src_uv, dst_vu, width); |
| 557 | src_uv += src_stride_uv; |
| 558 | dst_vu += dst_stride_vu; |
| 559 | } |
| 560 | } |
| 561 |
| 562 | // Convert NV21 to NV12. |
| 563 | LIBYUV_API |
| 564 | int NV21ToNV12(const uint8_t* src_y, |
| 565 | int src_stride_y, |
| 566 | const uint8_t* src_vu, |
| 567 | int src_stride_vu, |
| 568 | uint8_t* dst_y, |
| 569 | int dst_stride_y, |
| 570 | uint8_t* dst_uv, |
| 571 | int dst_stride_uv, |
| 572 | int width, |
| 573 | int height) { |
| 574 | int halfwidth = (width + 1) >> 1; |
| 575 | int halfheight = (height + 1) >> 1; |
| 576 | if (!src_vu || !dst_uv || width <= 0 || height == 0) { |
| 577 | return -1; |
| 578 | } |
| 579 | if (dst_y) { |
| 580 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 581 | } |
| 582 | SwapUVPlane(src_vu, src_stride_vu, dst_uv, dst_stride_uv, halfwidth, |
| 583 | halfheight); |
| 584 | return 0; |
| 585 | } |
| 586 |
| 587 | // Support function for NV12 etc RGB channels. |
| 588 | // Width and height are plane sizes (typically half pixel width). |
| 589 | LIBYUV_API |
| 590 | void SplitRGBPlane(const uint8_t* src_rgb, |
| 591 | int src_stride_rgb, |
| 592 | uint8_t* dst_r, |
| 593 | int dst_stride_r, |
| 594 | uint8_t* dst_g, |
| 595 | int dst_stride_g, |
| 596 | uint8_t* dst_b, |
| 597 | int dst_stride_b, |
| 598 | int width, |
| 599 | int height) { |
| 600 | int y; |
| 601 | void (*SplitRGBRow)(const uint8_t* src_rgb, uint8_t* dst_r, uint8_t* dst_g, |
| 602 | uint8_t* dst_b, int width) = SplitRGBRow_C; |
| 603 | // Negative height means invert the image. |
| 604 | if (height < 0) { |
| 605 | height = -height; |
| 606 | dst_r = dst_r + (height - 1) * dst_stride_r; |
| 607 | dst_g = dst_g + (height - 1) * dst_stride_g; |
| 608 | dst_b = dst_b + (height - 1) * dst_stride_b; |
| 609 | dst_stride_r = -dst_stride_r; |
| 610 | dst_stride_g = -dst_stride_g; |
| 611 | dst_stride_b = -dst_stride_b; |
| 612 | } |
| 613 | // Coalesce rows. |
| 614 | if (src_stride_rgb == width * 3 && dst_stride_r == width && |
| 615 | dst_stride_g == width && dst_stride_b == width) { |
| 616 | width *= height; |
| 617 | height = 1; |
| 618 | src_stride_rgb = dst_stride_r = dst_stride_g = dst_stride_b = 0; |
| 619 | } |
| 620 | #if defined(HAS_SPLITRGBROW_SSSE3) |
| 621 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 622 | SplitRGBRow = SplitRGBRow_Any_SSSE3; |
| 623 | if (IS_ALIGNED(width, 16)) { |
| 624 | SplitRGBRow = SplitRGBRow_SSSE3; |
| 625 | } |
| 626 | } |
| 627 | #endif |
| 628 | #if defined(HAS_SPLITRGBROW_NEON) |
| 629 | if (TestCpuFlag(kCpuHasNEON)) { |
| 630 | SplitRGBRow = SplitRGBRow_Any_NEON; |
| 631 | if (IS_ALIGNED(width, 16)) { |
| 632 | SplitRGBRow = SplitRGBRow_NEON; |
| 633 | } |
| 634 | } |
| 635 | #endif |
| 636 | #if defined(HAS_SPLITRGBROW_MMI) |
| 637 | if (TestCpuFlag(kCpuHasMMI)) { |
| 638 | SplitRGBRow = SplitRGBRow_Any_MMI; |
| 639 | if (IS_ALIGNED(width, 4)) { |
| 640 | SplitRGBRow = SplitRGBRow_MMI; |
| 641 | } |
| 642 | } |
| 643 | #endif |
| 644 |
| 645 | for (y = 0; y < height; ++y) { |
| 646 | // Copy a row of RGB. |
| 647 | SplitRGBRow(src_rgb, dst_r, dst_g, dst_b, width); |
| 648 | dst_r += dst_stride_r; |
| 649 | dst_g += dst_stride_g; |
| 650 | dst_b += dst_stride_b; |
| 651 | src_rgb += src_stride_rgb; |
| 652 | } |
| 653 | } |
| 654 |
| 655 | LIBYUV_API |
| 656 | void MergeRGBPlane(const uint8_t* src_r, |
| 657 | int src_stride_r, |
| 658 | const uint8_t* src_g, |
| 659 | int src_stride_g, |
| 660 | const uint8_t* src_b, |
| 661 | int src_stride_b, |
| 662 | uint8_t* dst_rgb, |
| 663 | int dst_stride_rgb, |
| 664 | int width, |
| 665 | int height) { |
| 666 | int y; |
| 667 | void (*MergeRGBRow)(const uint8_t* src_r, const uint8_t* src_g, |
| 668 | const uint8_t* src_b, uint8_t* dst_rgb, int width) = |
| 669 | MergeRGBRow_C; |
| 670 | // Coalesce rows. |
| 671 | // Negative height means invert the image. |
| 672 | if (height < 0) { |
| 673 | height = -height; |
| 674 | dst_rgb = dst_rgb + (height - 1) * dst_stride_rgb; |
| 675 | dst_stride_rgb = -dst_stride_rgb; |
| 676 | } |
| 677 | // Coalesce rows. |
| 678 | if (src_stride_r == width && src_stride_g == width && src_stride_b == width && |
| 679 | dst_stride_rgb == width * 3) { |
| 680 | width *= height; |
| 681 | height = 1; |
| 682 | src_stride_r = src_stride_g = src_stride_b = dst_stride_rgb = 0; |
| 683 | } |
| 684 | #if defined(HAS_MERGERGBROW_SSSE3) |
| 685 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 686 | MergeRGBRow = MergeRGBRow_Any_SSSE3; |
| 687 | if (IS_ALIGNED(width, 16)) { |
| 688 | MergeRGBRow = MergeRGBRow_SSSE3; |
| 689 | } |
| 690 | } |
| 691 | #endif |
| 692 | #if defined(HAS_MERGERGBROW_NEON) |
| 693 | if (TestCpuFlag(kCpuHasNEON)) { |
| 694 | MergeRGBRow = MergeRGBRow_Any_NEON; |
| 695 | if (IS_ALIGNED(width, 16)) { |
| 696 | MergeRGBRow = MergeRGBRow_NEON; |
| 697 | } |
| 698 | } |
| 699 | #endif |
| 700 | #if defined(HAS_MERGERGBROW_MMI) |
| 701 | if (TestCpuFlag(kCpuHasMMI)) { |
| 702 | MergeRGBRow = MergeRGBRow_Any_MMI; |
| 703 | if (IS_ALIGNED(width, 8)) { |
| 704 | MergeRGBRow = MergeRGBRow_MMI; |
| 705 | } |
| 706 | } |
| 707 | #endif |
| 708 |
| 709 | for (y = 0; y < height; ++y) { |
| 710 | // Merge a row of U and V into a row of RGB. |
| 711 | MergeRGBRow(src_r, src_g, src_b, dst_rgb, width); |
| 712 | src_r += src_stride_r; |
| 713 | src_g += src_stride_g; |
| 714 | src_b += src_stride_b; |
| 715 | dst_rgb += dst_stride_rgb; |
| 716 | } |
| 717 | } |
| 718 |
| 719 | // Mirror a plane of data. |
| 720 | void MirrorPlane(const uint8_t* src_y, |
| 721 | int src_stride_y, |
| 722 | uint8_t* dst_y, |
| 723 | int dst_stride_y, |
| 724 | int width, |
| 725 | int height) { |
| 726 | int y; |
| 727 | void (*MirrorRow)(const uint8_t* src, uint8_t* dst, int width) = MirrorRow_C; |
| 728 | // Negative height means invert the image. |
| 729 | if (height < 0) { |
| 730 | height = -height; |
| 731 | src_y = src_y + (height - 1) * src_stride_y; |
| 732 | src_stride_y = -src_stride_y; |
| 733 | } |
| 734 | #if defined(HAS_MIRRORROW_NEON) |
| 735 | if (TestCpuFlag(kCpuHasNEON)) { |
| 736 | MirrorRow = MirrorRow_Any_NEON; |
| 737 | if (IS_ALIGNED(width, 16)) { |
| 738 | MirrorRow = MirrorRow_NEON; |
| 739 | } |
| 740 | } |
| 741 | #endif |
| 742 | #if defined(HAS_MIRRORROW_SSSE3) |
| 743 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 744 | MirrorRow = MirrorRow_Any_SSSE3; |
| 745 | if (IS_ALIGNED(width, 16)) { |
| 746 | MirrorRow = MirrorRow_SSSE3; |
| 747 | } |
| 748 | } |
| 749 | #endif |
| 750 | #if defined(HAS_MIRRORROW_AVX2) |
| 751 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 752 | MirrorRow = MirrorRow_Any_AVX2; |
| 753 | if (IS_ALIGNED(width, 32)) { |
| 754 | MirrorRow = MirrorRow_AVX2; |
| 755 | } |
| 756 | } |
| 757 | #endif |
| 758 | #if defined(HAS_MIRRORROW_MSA) |
| 759 | if (TestCpuFlag(kCpuHasMSA)) { |
| 760 | MirrorRow = MirrorRow_Any_MSA; |
| 761 | if (IS_ALIGNED(width, 64)) { |
| 762 | MirrorRow = MirrorRow_MSA; |
| 763 | } |
| 764 | } |
| 765 | #endif |
| 766 | #if defined(HAS_MIRRORROW_MMI) |
| 767 | if (TestCpuFlag(kCpuHasMMI)) { |
| 768 | MirrorRow = MirrorRow_Any_MMI; |
| 769 | if (IS_ALIGNED(width, 8)) { |
| 770 | MirrorRow = MirrorRow_MMI; |
| 771 | } |
| 772 | } |
| 773 | #endif |
| 774 |
| 775 | // Mirror plane |
| 776 | for (y = 0; y < height; ++y) { |
| 777 | MirrorRow(src_y, dst_y, width); |
| 778 | src_y += src_stride_y; |
| 779 | dst_y += dst_stride_y; |
| 780 | } |
| 781 | } |
| 782 |
| 783 | // Convert YUY2 to I422. |
| 784 | LIBYUV_API |
| 785 | int YUY2ToI422(const uint8_t* src_yuy2, |
| 786 | int src_stride_yuy2, |
| 787 | uint8_t* dst_y, |
| 788 | int dst_stride_y, |
| 789 | uint8_t* dst_u, |
| 790 | int dst_stride_u, |
| 791 | uint8_t* dst_v, |
| 792 | int dst_stride_v, |
| 793 | int width, |
| 794 | int height) { |
| 795 | int y; |
| 796 | void (*YUY2ToUV422Row)(const uint8_t* src_yuy2, uint8_t* dst_u, |
| 797 | uint8_t* dst_v, int width) = YUY2ToUV422Row_C; |
| 798 | void (*YUY2ToYRow)(const uint8_t* src_yuy2, uint8_t* dst_y, int width) = |
| 799 | YUY2ToYRow_C; |
| 800 | if (!src_yuy2 || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 801 | return -1; |
| 802 | } |
| 803 | // Negative height means invert the image. |
| 804 | if (height < 0) { |
| 805 | height = -height; |
| 806 | src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2; |
| 807 | src_stride_yuy2 = -src_stride_yuy2; |
| 808 | } |
| 809 | // Coalesce rows. |
| 810 | if (src_stride_yuy2 == width * 2 && dst_stride_y == width && |
| 811 | dst_stride_u * 2 == width && dst_stride_v * 2 == width && |
| 812 | width * height <= 32768) { |
| 813 | width *= height; |
| 814 | height = 1; |
| 815 | src_stride_yuy2 = dst_stride_y = dst_stride_u = dst_stride_v = 0; |
| 816 | } |
| 817 | #if defined(HAS_YUY2TOYROW_SSE2) |
| 818 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 819 | YUY2ToUV422Row = YUY2ToUV422Row_Any_SSE2; |
| 820 | YUY2ToYRow = YUY2ToYRow_Any_SSE2; |
| 821 | if (IS_ALIGNED(width, 16)) { |
| 822 | YUY2ToUV422Row = YUY2ToUV422Row_SSE2; |
| 823 | YUY2ToYRow = YUY2ToYRow_SSE2; |
| 824 | } |
| 825 | } |
| 826 | #endif |
| 827 | #if defined(HAS_YUY2TOYROW_AVX2) |
| 828 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 829 | YUY2ToUV422Row = YUY2ToUV422Row_Any_AVX2; |
| 830 | YUY2ToYRow = YUY2ToYRow_Any_AVX2; |
| 831 | if (IS_ALIGNED(width, 32)) { |
| 832 | YUY2ToUV422Row = YUY2ToUV422Row_AVX2; |
| 833 | YUY2ToYRow = YUY2ToYRow_AVX2; |
| 834 | } |
| 835 | } |
| 836 | #endif |
| 837 | #if defined(HAS_YUY2TOYROW_NEON) |
| 838 | if (TestCpuFlag(kCpuHasNEON)) { |
| 839 | YUY2ToYRow = YUY2ToYRow_Any_NEON; |
| 840 | YUY2ToUV422Row = YUY2ToUV422Row_Any_NEON; |
| 841 | if (IS_ALIGNED(width, 16)) { |
| 842 | YUY2ToYRow = YUY2ToYRow_NEON; |
| 843 | YUY2ToUV422Row = YUY2ToUV422Row_NEON; |
| 844 | } |
| 845 | } |
| 846 | #endif |
| 847 | #if defined(HAS_YUY2TOYROW_MSA) |
| 848 | if (TestCpuFlag(kCpuHasMSA)) { |
| 849 | YUY2ToYRow = YUY2ToYRow_Any_MSA; |
| 850 | YUY2ToUV422Row = YUY2ToUV422Row_Any_MSA; |
| 851 | if (IS_ALIGNED(width, 32)) { |
| 852 | YUY2ToYRow = YUY2ToYRow_MSA; |
| 853 | YUY2ToUV422Row = YUY2ToUV422Row_MSA; |
| 854 | } |
| 855 | } |
| 856 | #endif |
| 857 | #if defined(HAS_YUY2TOYROW_MMI) |
| 858 | if (TestCpuFlag(kCpuHasMMI)) { |
| 859 | YUY2ToYRow = YUY2ToYRow_Any_MMI; |
| 860 | YUY2ToUV422Row = YUY2ToUV422Row_Any_MMI; |
| 861 | if (IS_ALIGNED(width, 8)) { |
| 862 | YUY2ToYRow = YUY2ToYRow_MMI; |
| 863 | YUY2ToUV422Row = YUY2ToUV422Row_MMI; |
| 864 | } |
| 865 | } |
| 866 | #endif |
| 867 |
| 868 | for (y = 0; y < height; ++y) { |
| 869 | YUY2ToUV422Row(src_yuy2, dst_u, dst_v, width); |
| 870 | YUY2ToYRow(src_yuy2, dst_y, width); |
| 871 | src_yuy2 += src_stride_yuy2; |
| 872 | dst_y += dst_stride_y; |
| 873 | dst_u += dst_stride_u; |
| 874 | dst_v += dst_stride_v; |
| 875 | } |
| 876 | return 0; |
| 877 | } |
| 878 |
| 879 | // Convert UYVY to I422. |
| 880 | LIBYUV_API |
| 881 | int UYVYToI422(const uint8_t* src_uyvy, |
| 882 | int src_stride_uyvy, |
| 883 | uint8_t* dst_y, |
| 884 | int dst_stride_y, |
| 885 | uint8_t* dst_u, |
| 886 | int dst_stride_u, |
| 887 | uint8_t* dst_v, |
| 888 | int dst_stride_v, |
| 889 | int width, |
| 890 | int height) { |
| 891 | int y; |
| 892 | void (*UYVYToUV422Row)(const uint8_t* src_uyvy, uint8_t* dst_u, |
| 893 | uint8_t* dst_v, int width) = UYVYToUV422Row_C; |
| 894 | void (*UYVYToYRow)(const uint8_t* src_uyvy, uint8_t* dst_y, int width) = |
| 895 | UYVYToYRow_C; |
| 896 | if (!src_uyvy || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 897 | return -1; |
| 898 | } |
| 899 | // Negative height means invert the image. |
| 900 | if (height < 0) { |
| 901 | height = -height; |
| 902 | src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy; |
| 903 | src_stride_uyvy = -src_stride_uyvy; |
| 904 | } |
| 905 | // Coalesce rows. |
| 906 | if (src_stride_uyvy == width * 2 && dst_stride_y == width && |
| 907 | dst_stride_u * 2 == width && dst_stride_v * 2 == width && |
| 908 | width * height <= 32768) { |
| 909 | width *= height; |
| 910 | height = 1; |
| 911 | src_stride_uyvy = dst_stride_y = dst_stride_u = dst_stride_v = 0; |
| 912 | } |
| 913 | #if defined(HAS_UYVYTOYROW_SSE2) |
| 914 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 915 | UYVYToUV422Row = UYVYToUV422Row_Any_SSE2; |
| 916 | UYVYToYRow = UYVYToYRow_Any_SSE2; |
| 917 | if (IS_ALIGNED(width, 16)) { |
| 918 | UYVYToUV422Row = UYVYToUV422Row_SSE2; |
| 919 | UYVYToYRow = UYVYToYRow_SSE2; |
| 920 | } |
| 921 | } |
| 922 | #endif |
| 923 | #if defined(HAS_UYVYTOYROW_AVX2) |
| 924 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 925 | UYVYToUV422Row = UYVYToUV422Row_Any_AVX2; |
| 926 | UYVYToYRow = UYVYToYRow_Any_AVX2; |
| 927 | if (IS_ALIGNED(width, 32)) { |
| 928 | UYVYToUV422Row = UYVYToUV422Row_AVX2; |
| 929 | UYVYToYRow = UYVYToYRow_AVX2; |
| 930 | } |
| 931 | } |
| 932 | #endif |
| 933 | #if defined(HAS_UYVYTOYROW_NEON) |
| 934 | if (TestCpuFlag(kCpuHasNEON)) { |
| 935 | UYVYToYRow = UYVYToYRow_Any_NEON; |
| 936 | UYVYToUV422Row = UYVYToUV422Row_Any_NEON; |
| 937 | if (IS_ALIGNED(width, 16)) { |
| 938 | UYVYToYRow = UYVYToYRow_NEON; |
| 939 | UYVYToUV422Row = UYVYToUV422Row_NEON; |
| 940 | } |
| 941 | } |
| 942 | #endif |
| 943 | #if defined(HAS_UYVYTOYROW_MSA) |
| 944 | if (TestCpuFlag(kCpuHasMSA)) { |
| 945 | UYVYToYRow = UYVYToYRow_Any_MSA; |
| 946 | UYVYToUV422Row = UYVYToUV422Row_Any_MSA; |
| 947 | if (IS_ALIGNED(width, 32)) { |
| 948 | UYVYToYRow = UYVYToYRow_MSA; |
| 949 | UYVYToUV422Row = UYVYToUV422Row_MSA; |
| 950 | } |
| 951 | } |
| 952 | #endif |
| 953 | #if defined(HAS_UYVYTOYROW_MMI) |
| 954 | if (TestCpuFlag(kCpuHasMMI)) { |
| 955 | UYVYToYRow = UYVYToYRow_Any_MMI; |
| 956 | UYVYToUV422Row = UYVYToUV422Row_Any_MMI; |
| 957 | if (IS_ALIGNED(width, 16)) { |
| 958 | UYVYToYRow = UYVYToYRow_MMI; |
| 959 | UYVYToUV422Row = UYVYToUV422Row_MMI; |
| 960 | } |
| 961 | } |
| 962 | #endif |
| 963 |
| 964 | for (y = 0; y < height; ++y) { |
| 965 | UYVYToUV422Row(src_uyvy, dst_u, dst_v, width); |
| 966 | UYVYToYRow(src_uyvy, dst_y, width); |
| 967 | src_uyvy += src_stride_uyvy; |
| 968 | dst_y += dst_stride_y; |
| 969 | dst_u += dst_stride_u; |
| 970 | dst_v += dst_stride_v; |
| 971 | } |
| 972 | return 0; |
| 973 | } |
| 974 |
| 975 | // Convert YUY2 to Y. |
| 976 | LIBYUV_API |
| 977 | int YUY2ToY(const uint8_t* src_yuy2, |
| 978 | int src_stride_yuy2, |
| 979 | uint8_t* dst_y, |
| 980 | int dst_stride_y, |
| 981 | int width, |
| 982 | int height) { |
| 983 | int y; |
| 984 | void (*YUY2ToYRow)(const uint8_t* src_yuy2, uint8_t* dst_y, int width) = |
| 985 | YUY2ToYRow_C; |
| 986 | if (!src_yuy2 || !dst_y || width <= 0 || height == 0) { |
| 987 | return -1; |
| 988 | } |
| 989 | // Negative height means invert the image. |
| 990 | if (height < 0) { |
| 991 | height = -height; |
| 992 | src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2; |
| 993 | src_stride_yuy2 = -src_stride_yuy2; |
| 994 | } |
| 995 | // Coalesce rows. |
| 996 | if (src_stride_yuy2 == width * 2 && dst_stride_y == width) { |
| 997 | width *= height; |
| 998 | height = 1; |
| 999 | src_stride_yuy2 = dst_stride_y = 0; |
| 1000 | } |
| 1001 | #if defined(HAS_YUY2TOYROW_SSE2) |
| 1002 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1003 | YUY2ToYRow = YUY2ToYRow_Any_SSE2; |
| 1004 | if (IS_ALIGNED(width, 16)) { |
| 1005 | YUY2ToYRow = YUY2ToYRow_SSE2; |
| 1006 | } |
| 1007 | } |
| 1008 | #endif |
| 1009 | #if defined(HAS_YUY2TOYROW_AVX2) |
| 1010 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1011 | YUY2ToYRow = YUY2ToYRow_Any_AVX2; |
| 1012 | if (IS_ALIGNED(width, 32)) { |
| 1013 | YUY2ToYRow = YUY2ToYRow_AVX2; |
| 1014 | } |
| 1015 | } |
| 1016 | #endif |
| 1017 | #if defined(HAS_YUY2TOYROW_NEON) |
| 1018 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1019 | YUY2ToYRow = YUY2ToYRow_Any_NEON; |
| 1020 | if (IS_ALIGNED(width, 16)) { |
| 1021 | YUY2ToYRow = YUY2ToYRow_NEON; |
| 1022 | } |
| 1023 | } |
| 1024 | #endif |
| 1025 | #if defined(HAS_YUY2TOYROW_MSA) |
| 1026 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1027 | YUY2ToYRow = YUY2ToYRow_Any_MSA; |
| 1028 | if (IS_ALIGNED(width, 32)) { |
| 1029 | YUY2ToYRow = YUY2ToYRow_MSA; |
| 1030 | } |
| 1031 | } |
| 1032 | #endif |
| 1033 | #if defined(HAS_YUY2TOYROW_MMI) |
| 1034 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1035 | YUY2ToYRow = YUY2ToYRow_Any_MMI; |
| 1036 | if (IS_ALIGNED(width, 8)) { |
| 1037 | YUY2ToYRow = YUY2ToYRow_MMI; |
| 1038 | } |
| 1039 | } |
| 1040 | #endif |
| 1041 |
| 1042 | for (y = 0; y < height; ++y) { |
| 1043 | YUY2ToYRow(src_yuy2, dst_y, width); |
| 1044 | src_yuy2 += src_stride_yuy2; |
| 1045 | dst_y += dst_stride_y; |
| 1046 | } |
| 1047 | return 0; |
| 1048 | } |
| 1049 |
| 1050 | // Mirror I400 with optional flipping |
| 1051 | LIBYUV_API |
| 1052 | int I400Mirror(const uint8_t* src_y, |
| 1053 | int src_stride_y, |
| 1054 | uint8_t* dst_y, |
| 1055 | int dst_stride_y, |
| 1056 | int width, |
| 1057 | int height) { |
| 1058 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 1059 | return -1; |
| 1060 | } |
| 1061 | // Negative height means invert the image. |
| 1062 | if (height < 0) { |
| 1063 | height = -height; |
| 1064 | src_y = src_y + (height - 1) * src_stride_y; |
| 1065 | src_stride_y = -src_stride_y; |
| 1066 | } |
| 1067 |
| 1068 | MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 1069 | return 0; |
| 1070 | } |
| 1071 |
| 1072 | // Mirror I420 with optional flipping |
| 1073 | LIBYUV_API |
| 1074 | int I420Mirror(const uint8_t* src_y, |
| 1075 | int src_stride_y, |
| 1076 | const uint8_t* src_u, |
| 1077 | int src_stride_u, |
| 1078 | const uint8_t* src_v, |
| 1079 | int src_stride_v, |
| 1080 | uint8_t* dst_y, |
| 1081 | int dst_stride_y, |
| 1082 | uint8_t* dst_u, |
| 1083 | int dst_stride_u, |
| 1084 | uint8_t* dst_v, |
| 1085 | int dst_stride_v, |
| 1086 | int width, |
| 1087 | int height) { |
| 1088 | int halfwidth = (width + 1) >> 1; |
| 1089 | int halfheight = (height + 1) >> 1; |
| 1090 | if (!src_y || !src_u || !src_v || !dst_y || !dst_u || !dst_v || width <= 0 || |
| 1091 | height == 0) { |
| 1092 | return -1; |
| 1093 | } |
| 1094 | // Negative height means invert the image. |
| 1095 | if (height < 0) { |
| 1096 | height = -height; |
| 1097 | halfheight = (height + 1) >> 1; |
| 1098 | src_y = src_y + (height - 1) * src_stride_y; |
| 1099 | src_u = src_u + (halfheight - 1) * src_stride_u; |
| 1100 | src_v = src_v + (halfheight - 1) * src_stride_v; |
| 1101 | src_stride_y = -src_stride_y; |
| 1102 | src_stride_u = -src_stride_u; |
| 1103 | src_stride_v = -src_stride_v; |
| 1104 | } |
| 1105 |
| 1106 | if (dst_y) { |
| 1107 | MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 1108 | } |
| 1109 | MirrorPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, halfheight); |
| 1110 | MirrorPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, halfheight); |
| 1111 | return 0; |
| 1112 | } |
| 1113 |
| 1114 | // ARGB mirror. |
| 1115 | LIBYUV_API |
| 1116 | int ARGBMirror(const uint8_t* src_argb, |
| 1117 | int src_stride_argb, |
| 1118 | uint8_t* dst_argb, |
| 1119 | int dst_stride_argb, |
| 1120 | int width, |
| 1121 | int height) { |
| 1122 | int y; |
| 1123 | void (*ARGBMirrorRow)(const uint8_t* src, uint8_t* dst, int width) = |
| 1124 | ARGBMirrorRow_C; |
| 1125 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 1126 | return -1; |
| 1127 | } |
| 1128 | // Negative height means invert the image. |
| 1129 | if (height < 0) { |
| 1130 | height = -height; |
| 1131 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 1132 | src_stride_argb = -src_stride_argb; |
| 1133 | } |
| 1134 | #if defined(HAS_ARGBMIRRORROW_NEON) |
| 1135 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1136 | ARGBMirrorRow = ARGBMirrorRow_Any_NEON; |
| 1137 | if (IS_ALIGNED(width, 4)) { |
| 1138 | ARGBMirrorRow = ARGBMirrorRow_NEON; |
| 1139 | } |
| 1140 | } |
| 1141 | #endif |
| 1142 | #if defined(HAS_ARGBMIRRORROW_SSE2) |
| 1143 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1144 | ARGBMirrorRow = ARGBMirrorRow_Any_SSE2; |
| 1145 | if (IS_ALIGNED(width, 4)) { |
| 1146 | ARGBMirrorRow = ARGBMirrorRow_SSE2; |
| 1147 | } |
| 1148 | } |
| 1149 | #endif |
| 1150 | #if defined(HAS_ARGBMIRRORROW_AVX2) |
| 1151 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1152 | ARGBMirrorRow = ARGBMirrorRow_Any_AVX2; |
| 1153 | if (IS_ALIGNED(width, 8)) { |
| 1154 | ARGBMirrorRow = ARGBMirrorRow_AVX2; |
| 1155 | } |
| 1156 | } |
| 1157 | #endif |
| 1158 | #if defined(HAS_ARGBMIRRORROW_MSA) |
| 1159 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1160 | ARGBMirrorRow = ARGBMirrorRow_Any_MSA; |
| 1161 | if (IS_ALIGNED(width, 16)) { |
| 1162 | ARGBMirrorRow = ARGBMirrorRow_MSA; |
| 1163 | } |
| 1164 | } |
| 1165 | #endif |
| 1166 | #if defined(HAS_ARGBMIRRORROW_MMI) |
| 1167 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1168 | ARGBMirrorRow = ARGBMirrorRow_Any_MMI; |
| 1169 | if (IS_ALIGNED(width, 2)) { |
| 1170 | ARGBMirrorRow = ARGBMirrorRow_MMI; |
| 1171 | } |
| 1172 | } |
| 1173 | #endif |
| 1174 |
| 1175 | // Mirror plane |
| 1176 | for (y = 0; y < height; ++y) { |
| 1177 | ARGBMirrorRow(src_argb, dst_argb, width); |
| 1178 | src_argb += src_stride_argb; |
| 1179 | dst_argb += dst_stride_argb; |
| 1180 | } |
| 1181 | return 0; |
| 1182 | } |
| 1183 |
| 1184 | // Get a blender that optimized for the CPU and pixel count. |
| 1185 | // As there are 6 blenders to choose from, the caller should try to use |
| 1186 | // the same blend function for all pixels if possible. |
| 1187 | LIBYUV_API |
| 1188 | ARGBBlendRow GetARGBBlend() { |
| 1189 | void (*ARGBBlendRow)(const uint8_t* src_argb, const uint8_t* src_argb1, |
| 1190 | uint8_t* dst_argb, int width) = ARGBBlendRow_C; |
| 1191 | #if defined(HAS_ARGBBLENDROW_SSSE3) |
| 1192 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1193 | ARGBBlendRow = ARGBBlendRow_SSSE3; |
| 1194 | return ARGBBlendRow; |
| 1195 | } |
| 1196 | #endif |
| 1197 | #if defined(HAS_ARGBBLENDROW_NEON) |
| 1198 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1199 | ARGBBlendRow = ARGBBlendRow_NEON; |
| 1200 | } |
| 1201 | #endif |
| 1202 | #if defined(HAS_ARGBBLENDROW_MSA) |
| 1203 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1204 | ARGBBlendRow = ARGBBlendRow_MSA; |
| 1205 | } |
| 1206 | #endif |
| 1207 | #if defined(HAS_ARGBBLENDROW_MMI) |
| 1208 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1209 | ARGBBlendRow = ARGBBlendRow_MMI; |
| 1210 | } |
| 1211 | #endif |
| 1212 | return ARGBBlendRow; |
| 1213 | } |
| 1214 |
| 1215 | // Alpha Blend 2 ARGB images and store to destination. |
| 1216 | LIBYUV_API |
| 1217 | int ARGBBlend(const uint8_t* src_argb0, |
| 1218 | int src_stride_argb0, |
| 1219 | const uint8_t* src_argb1, |
| 1220 | int src_stride_argb1, |
| 1221 | uint8_t* dst_argb, |
| 1222 | int dst_stride_argb, |
| 1223 | int width, |
| 1224 | int height) { |
| 1225 | int y; |
| 1226 | void (*ARGBBlendRow)(const uint8_t* src_argb, const uint8_t* src_argb1, |
| 1227 | uint8_t* dst_argb, int width) = GetARGBBlend(); |
| 1228 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 1229 | return -1; |
| 1230 | } |
| 1231 | // Negative height means invert the image. |
| 1232 | if (height < 0) { |
| 1233 | height = -height; |
| 1234 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 1235 | dst_stride_argb = -dst_stride_argb; |
| 1236 | } |
| 1237 | // Coalesce rows. |
| 1238 | if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && |
| 1239 | dst_stride_argb == width * 4) { |
| 1240 | width *= height; |
| 1241 | height = 1; |
| 1242 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 1243 | } |
| 1244 |
| 1245 | for (y = 0; y < height; ++y) { |
| 1246 | ARGBBlendRow(src_argb0, src_argb1, dst_argb, width); |
| 1247 | src_argb0 += src_stride_argb0; |
| 1248 | src_argb1 += src_stride_argb1; |
| 1249 | dst_argb += dst_stride_argb; |
| 1250 | } |
| 1251 | return 0; |
| 1252 | } |
| 1253 |
| 1254 | // Alpha Blend plane and store to destination. |
| 1255 | LIBYUV_API |
| 1256 | int BlendPlane(const uint8_t* src_y0, |
| 1257 | int src_stride_y0, |
| 1258 | const uint8_t* src_y1, |
| 1259 | int src_stride_y1, |
| 1260 | const uint8_t* alpha, |
| 1261 | int alpha_stride, |
| 1262 | uint8_t* dst_y, |
| 1263 | int dst_stride_y, |
| 1264 | int width, |
| 1265 | int height) { |
| 1266 | int y; |
| 1267 | void (*BlendPlaneRow)(const uint8_t* src0, const uint8_t* src1, |
| 1268 | const uint8_t* alpha, uint8_t* dst, int width) = |
| 1269 | BlendPlaneRow_C; |
| 1270 | if (!src_y0 || !src_y1 || !alpha || !dst_y || width <= 0 || height == 0) { |
| 1271 | return -1; |
| 1272 | } |
| 1273 | // Negative height means invert the image. |
| 1274 | if (height < 0) { |
| 1275 | height = -height; |
| 1276 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 1277 | dst_stride_y = -dst_stride_y; |
| 1278 | } |
| 1279 |
| 1280 | // Coalesce rows for Y plane. |
| 1281 | if (src_stride_y0 == width && src_stride_y1 == width && |
| 1282 | alpha_stride == width && dst_stride_y == width) { |
| 1283 | width *= height; |
| 1284 | height = 1; |
| 1285 | src_stride_y0 = src_stride_y1 = alpha_stride = dst_stride_y = 0; |
| 1286 | } |
| 1287 |
| 1288 | #if defined(HAS_BLENDPLANEROW_SSSE3) |
| 1289 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1290 | BlendPlaneRow = BlendPlaneRow_Any_SSSE3; |
| 1291 | if (IS_ALIGNED(width, 8)) { |
| 1292 | BlendPlaneRow = BlendPlaneRow_SSSE3; |
| 1293 | } |
| 1294 | } |
| 1295 | #endif |
| 1296 | #if defined(HAS_BLENDPLANEROW_AVX2) |
| 1297 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1298 | BlendPlaneRow = BlendPlaneRow_Any_AVX2; |
| 1299 | if (IS_ALIGNED(width, 32)) { |
| 1300 | BlendPlaneRow = BlendPlaneRow_AVX2; |
| 1301 | } |
| 1302 | } |
| 1303 | #endif |
| 1304 | #if defined(HAS_BLENDPLANEROW_MMI) |
| 1305 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1306 | BlendPlaneRow = BlendPlaneRow_Any_MMI; |
| 1307 | if (IS_ALIGNED(width, 8)) { |
| 1308 | BlendPlaneRow = BlendPlaneRow_MMI; |
| 1309 | } |
| 1310 | } |
| 1311 | #endif |
| 1312 |
| 1313 | for (y = 0; y < height; ++y) { |
| 1314 | BlendPlaneRow(src_y0, src_y1, alpha, dst_y, width); |
| 1315 | src_y0 += src_stride_y0; |
| 1316 | src_y1 += src_stride_y1; |
| 1317 | alpha += alpha_stride; |
| 1318 | dst_y += dst_stride_y; |
| 1319 | } |
| 1320 | return 0; |
| 1321 | } |
| 1322 |
| 1323 | #define MAXTWIDTH 2048 |
| 1324 | // Alpha Blend YUV images and store to destination. |
| 1325 | LIBYUV_API |
| 1326 | int I420Blend(const uint8_t* src_y0, |
| 1327 | int src_stride_y0, |
| 1328 | const uint8_t* src_u0, |
| 1329 | int src_stride_u0, |
| 1330 | const uint8_t* src_v0, |
| 1331 | int src_stride_v0, |
| 1332 | const uint8_t* src_y1, |
| 1333 | int src_stride_y1, |
| 1334 | const uint8_t* src_u1, |
| 1335 | int src_stride_u1, |
| 1336 | const uint8_t* src_v1, |
| 1337 | int src_stride_v1, |
| 1338 | const uint8_t* alpha, |
| 1339 | int alpha_stride, |
| 1340 | uint8_t* dst_y, |
| 1341 | int dst_stride_y, |
| 1342 | uint8_t* dst_u, |
| 1343 | int dst_stride_u, |
| 1344 | uint8_t* dst_v, |
| 1345 | int dst_stride_v, |
| 1346 | int width, |
| 1347 | int height) { |
| 1348 | int y; |
| 1349 | // Half width/height for UV. |
| 1350 | int halfwidth = (width + 1) >> 1; |
| 1351 | void (*BlendPlaneRow)(const uint8_t* src0, const uint8_t* src1, |
| 1352 | const uint8_t* alpha, uint8_t* dst, int width) = |
| 1353 | BlendPlaneRow_C; |
| 1354 | void (*ScaleRowDown2)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 1355 | uint8_t* dst_ptr, int dst_width) = ScaleRowDown2Box_C; |
| 1356 | if (!src_y0 || !src_u0 || !src_v0 || !src_y1 || !src_u1 || !src_v1 || |
| 1357 | !alpha || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 1358 | return -1; |
| 1359 | } |
| 1360 |
| 1361 | // Negative height means invert the image. |
| 1362 | if (height < 0) { |
| 1363 | height = -height; |
| 1364 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 1365 | dst_stride_y = -dst_stride_y; |
| 1366 | } |
| 1367 |
| 1368 | // Blend Y plane. |
| 1369 | BlendPlane(src_y0, src_stride_y0, src_y1, src_stride_y1, alpha, alpha_stride, |
| 1370 | dst_y, dst_stride_y, width, height); |
| 1371 |
| 1372 | #if defined(HAS_BLENDPLANEROW_SSSE3) |
| 1373 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1374 | BlendPlaneRow = BlendPlaneRow_Any_SSSE3; |
| 1375 | if (IS_ALIGNED(halfwidth, 8)) { |
| 1376 | BlendPlaneRow = BlendPlaneRow_SSSE3; |
| 1377 | } |
| 1378 | } |
| 1379 | #endif |
| 1380 | #if defined(HAS_BLENDPLANEROW_AVX2) |
| 1381 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1382 | BlendPlaneRow = BlendPlaneRow_Any_AVX2; |
| 1383 | if (IS_ALIGNED(halfwidth, 32)) { |
| 1384 | BlendPlaneRow = BlendPlaneRow_AVX2; |
| 1385 | } |
| 1386 | } |
| 1387 | #endif |
| 1388 | #if defined(HAS_BLENDPLANEROW_MMI) |
| 1389 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1390 | BlendPlaneRow = BlendPlaneRow_Any_MMI; |
| 1391 | if (IS_ALIGNED(halfwidth, 8)) { |
| 1392 | BlendPlaneRow = BlendPlaneRow_MMI; |
| 1393 | } |
| 1394 | } |
| 1395 | #endif |
| 1396 | if (!IS_ALIGNED(width, 2)) { |
| 1397 | ScaleRowDown2 = ScaleRowDown2Box_Odd_C; |
| 1398 | } |
| 1399 | #if defined(HAS_SCALEROWDOWN2_NEON) |
| 1400 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1401 | ScaleRowDown2 = ScaleRowDown2Box_Odd_NEON; |
| 1402 | if (IS_ALIGNED(width, 2)) { |
| 1403 | ScaleRowDown2 = ScaleRowDown2Box_Any_NEON; |
| 1404 | if (IS_ALIGNED(halfwidth, 16)) { |
| 1405 | ScaleRowDown2 = ScaleRowDown2Box_NEON; |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | #endif |
| 1410 | #if defined(HAS_SCALEROWDOWN2_SSSE3) |
| 1411 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1412 | ScaleRowDown2 = ScaleRowDown2Box_Odd_SSSE3; |
| 1413 | if (IS_ALIGNED(width, 2)) { |
| 1414 | ScaleRowDown2 = ScaleRowDown2Box_Any_SSSE3; |
| 1415 | if (IS_ALIGNED(halfwidth, 16)) { |
| 1416 | ScaleRowDown2 = ScaleRowDown2Box_SSSE3; |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | #endif |
| 1421 | #if defined(HAS_SCALEROWDOWN2_AVX2) |
| 1422 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1423 | ScaleRowDown2 = ScaleRowDown2Box_Odd_AVX2; |
| 1424 | if (IS_ALIGNED(width, 2)) { |
| 1425 | ScaleRowDown2 = ScaleRowDown2Box_Any_AVX2; |
| 1426 | if (IS_ALIGNED(halfwidth, 32)) { |
| 1427 | ScaleRowDown2 = ScaleRowDown2Box_AVX2; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | #endif |
| 1432 | #if defined(HAS_SCALEROWDOWN2_MMI) |
| 1433 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1434 | ScaleRowDown2 = ScaleRowDown2Box_Odd_MMI; |
| 1435 | if (IS_ALIGNED(width, 2)) { |
| 1436 | ScaleRowDown2 = ScaleRowDown2Box_Any_MMI; |
| 1437 | if (IS_ALIGNED(halfwidth, 8)) { |
| 1438 | ScaleRowDown2 = ScaleRowDown2Box_MMI; |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | #endif |
| 1443 |
| 1444 | // Row buffer for intermediate alpha pixels. |
| 1445 | align_buffer_64(halfalpha, halfwidth); |
| 1446 | for (y = 0; y < height; y += 2) { |
| 1447 | // last row of odd height image use 1 row of alpha instead of 2. |
| 1448 | if (y == (height - 1)) { |
| 1449 | alpha_stride = 0; |
| 1450 | } |
| 1451 | // Subsample 2 rows of UV to half width and half height. |
| 1452 | ScaleRowDown2(alpha, alpha_stride, halfalpha, halfwidth); |
| 1453 | alpha += alpha_stride * 2; |
| 1454 | BlendPlaneRow(src_u0, src_u1, halfalpha, dst_u, halfwidth); |
| 1455 | BlendPlaneRow(src_v0, src_v1, halfalpha, dst_v, halfwidth); |
| 1456 | src_u0 += src_stride_u0; |
| 1457 | src_u1 += src_stride_u1; |
| 1458 | dst_u += dst_stride_u; |
| 1459 | src_v0 += src_stride_v0; |
| 1460 | src_v1 += src_stride_v1; |
| 1461 | dst_v += dst_stride_v; |
| 1462 | } |
| 1463 | free_aligned_buffer_64(halfalpha); |
| 1464 | return 0; |
| 1465 | } |
| 1466 |
| 1467 | // Multiply 2 ARGB images and store to destination. |
| 1468 | LIBYUV_API |
| 1469 | int ARGBMultiply(const uint8_t* src_argb0, |
| 1470 | int src_stride_argb0, |
| 1471 | const uint8_t* src_argb1, |
| 1472 | int src_stride_argb1, |
| 1473 | uint8_t* dst_argb, |
| 1474 | int dst_stride_argb, |
| 1475 | int width, |
| 1476 | int height) { |
| 1477 | int y; |
| 1478 | void (*ARGBMultiplyRow)(const uint8_t* src0, const uint8_t* src1, |
| 1479 | uint8_t* dst, int width) = ARGBMultiplyRow_C; |
| 1480 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 1481 | return -1; |
| 1482 | } |
| 1483 | // Negative height means invert the image. |
| 1484 | if (height < 0) { |
| 1485 | height = -height; |
| 1486 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 1487 | dst_stride_argb = -dst_stride_argb; |
| 1488 | } |
| 1489 | // Coalesce rows. |
| 1490 | if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && |
| 1491 | dst_stride_argb == width * 4) { |
| 1492 | width *= height; |
| 1493 | height = 1; |
| 1494 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 1495 | } |
| 1496 | #if defined(HAS_ARGBMULTIPLYROW_SSE2) |
| 1497 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1498 | ARGBMultiplyRow = ARGBMultiplyRow_Any_SSE2; |
| 1499 | if (IS_ALIGNED(width, 4)) { |
| 1500 | ARGBMultiplyRow = ARGBMultiplyRow_SSE2; |
| 1501 | } |
| 1502 | } |
| 1503 | #endif |
| 1504 | #if defined(HAS_ARGBMULTIPLYROW_AVX2) |
| 1505 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1506 | ARGBMultiplyRow = ARGBMultiplyRow_Any_AVX2; |
| 1507 | if (IS_ALIGNED(width, 8)) { |
| 1508 | ARGBMultiplyRow = ARGBMultiplyRow_AVX2; |
| 1509 | } |
| 1510 | } |
| 1511 | #endif |
| 1512 | #if defined(HAS_ARGBMULTIPLYROW_NEON) |
| 1513 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1514 | ARGBMultiplyRow = ARGBMultiplyRow_Any_NEON; |
| 1515 | if (IS_ALIGNED(width, 8)) { |
| 1516 | ARGBMultiplyRow = ARGBMultiplyRow_NEON; |
| 1517 | } |
| 1518 | } |
| 1519 | #endif |
| 1520 | #if defined(HAS_ARGBMULTIPLYROW_MSA) |
| 1521 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1522 | ARGBMultiplyRow = ARGBMultiplyRow_Any_MSA; |
| 1523 | if (IS_ALIGNED(width, 4)) { |
| 1524 | ARGBMultiplyRow = ARGBMultiplyRow_MSA; |
| 1525 | } |
| 1526 | } |
| 1527 | #endif |
| 1528 | #if defined(HAS_ARGBMULTIPLYROW_MMI) |
| 1529 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1530 | ARGBMultiplyRow = ARGBMultiplyRow_Any_MMI; |
| 1531 | if (IS_ALIGNED(width, 2)) { |
| 1532 | ARGBMultiplyRow = ARGBMultiplyRow_MMI; |
| 1533 | } |
| 1534 | } |
| 1535 | #endif |
| 1536 |
| 1537 | // Multiply plane |
| 1538 | for (y = 0; y < height; ++y) { |
| 1539 | ARGBMultiplyRow(src_argb0, src_argb1, dst_argb, width); |
| 1540 | src_argb0 += src_stride_argb0; |
| 1541 | src_argb1 += src_stride_argb1; |
| 1542 | dst_argb += dst_stride_argb; |
| 1543 | } |
| 1544 | return 0; |
| 1545 | } |
| 1546 |
| 1547 | // Add 2 ARGB images and store to destination. |
| 1548 | LIBYUV_API |
| 1549 | int ARGBAdd(const uint8_t* src_argb0, |
| 1550 | int src_stride_argb0, |
| 1551 | const uint8_t* src_argb1, |
| 1552 | int src_stride_argb1, |
| 1553 | uint8_t* dst_argb, |
| 1554 | int dst_stride_argb, |
| 1555 | int width, |
| 1556 | int height) { |
| 1557 | int y; |
| 1558 | void (*ARGBAddRow)(const uint8_t* src0, const uint8_t* src1, uint8_t* dst, |
| 1559 | int width) = ARGBAddRow_C; |
| 1560 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 1561 | return -1; |
| 1562 | } |
| 1563 | // Negative height means invert the image. |
| 1564 | if (height < 0) { |
| 1565 | height = -height; |
| 1566 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 1567 | dst_stride_argb = -dst_stride_argb; |
| 1568 | } |
| 1569 | // Coalesce rows. |
| 1570 | if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && |
| 1571 | dst_stride_argb == width * 4) { |
| 1572 | width *= height; |
| 1573 | height = 1; |
| 1574 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 1575 | } |
| 1576 | #if defined(HAS_ARGBADDROW_SSE2) && (defined(_MSC_VER) && !defined(__clang__)) |
| 1577 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1578 | ARGBAddRow = ARGBAddRow_SSE2; |
| 1579 | } |
| 1580 | #endif |
| 1581 | #if defined(HAS_ARGBADDROW_SSE2) && !(defined(_MSC_VER) && !defined(__clang__)) |
| 1582 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1583 | ARGBAddRow = ARGBAddRow_Any_SSE2; |
| 1584 | if (IS_ALIGNED(width, 4)) { |
| 1585 | ARGBAddRow = ARGBAddRow_SSE2; |
| 1586 | } |
| 1587 | } |
| 1588 | #endif |
| 1589 | #if defined(HAS_ARGBADDROW_AVX2) |
| 1590 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1591 | ARGBAddRow = ARGBAddRow_Any_AVX2; |
| 1592 | if (IS_ALIGNED(width, 8)) { |
| 1593 | ARGBAddRow = ARGBAddRow_AVX2; |
| 1594 | } |
| 1595 | } |
| 1596 | #endif |
| 1597 | #if defined(HAS_ARGBADDROW_NEON) |
| 1598 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1599 | ARGBAddRow = ARGBAddRow_Any_NEON; |
| 1600 | if (IS_ALIGNED(width, 8)) { |
| 1601 | ARGBAddRow = ARGBAddRow_NEON; |
| 1602 | } |
| 1603 | } |
| 1604 | #endif |
| 1605 | #if defined(HAS_ARGBADDROW_MSA) |
| 1606 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1607 | ARGBAddRow = ARGBAddRow_Any_MSA; |
| 1608 | if (IS_ALIGNED(width, 8)) { |
| 1609 | ARGBAddRow = ARGBAddRow_MSA; |
| 1610 | } |
| 1611 | } |
| 1612 | #endif |
| 1613 | #if defined(HAS_ARGBADDROW_MMI) |
| 1614 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1615 | ARGBAddRow = ARGBAddRow_Any_MMI; |
| 1616 | if (IS_ALIGNED(width, 2)) { |
| 1617 | ARGBAddRow = ARGBAddRow_MMI; |
| 1618 | } |
| 1619 | } |
| 1620 | #endif |
| 1621 |
| 1622 | // Add plane |
| 1623 | for (y = 0; y < height; ++y) { |
| 1624 | ARGBAddRow(src_argb0, src_argb1, dst_argb, width); |
| 1625 | src_argb0 += src_stride_argb0; |
| 1626 | src_argb1 += src_stride_argb1; |
| 1627 | dst_argb += dst_stride_argb; |
| 1628 | } |
| 1629 | return 0; |
| 1630 | } |
| 1631 |
| 1632 | // Subtract 2 ARGB images and store to destination. |
| 1633 | LIBYUV_API |
| 1634 | int ARGBSubtract(const uint8_t* src_argb0, |
| 1635 | int src_stride_argb0, |
| 1636 | const uint8_t* src_argb1, |
| 1637 | int src_stride_argb1, |
| 1638 | uint8_t* dst_argb, |
| 1639 | int dst_stride_argb, |
| 1640 | int width, |
| 1641 | int height) { |
| 1642 | int y; |
| 1643 | void (*ARGBSubtractRow)(const uint8_t* src0, const uint8_t* src1, |
| 1644 | uint8_t* dst, int width) = ARGBSubtractRow_C; |
| 1645 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 1646 | return -1; |
| 1647 | } |
| 1648 | // Negative height means invert the image. |
| 1649 | if (height < 0) { |
| 1650 | height = -height; |
| 1651 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 1652 | dst_stride_argb = -dst_stride_argb; |
| 1653 | } |
| 1654 | // Coalesce rows. |
| 1655 | if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && |
| 1656 | dst_stride_argb == width * 4) { |
| 1657 | width *= height; |
| 1658 | height = 1; |
| 1659 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 1660 | } |
| 1661 | #if defined(HAS_ARGBSUBTRACTROW_SSE2) |
| 1662 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1663 | ARGBSubtractRow = ARGBSubtractRow_Any_SSE2; |
| 1664 | if (IS_ALIGNED(width, 4)) { |
| 1665 | ARGBSubtractRow = ARGBSubtractRow_SSE2; |
| 1666 | } |
| 1667 | } |
| 1668 | #endif |
| 1669 | #if defined(HAS_ARGBSUBTRACTROW_AVX2) |
| 1670 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1671 | ARGBSubtractRow = ARGBSubtractRow_Any_AVX2; |
| 1672 | if (IS_ALIGNED(width, 8)) { |
| 1673 | ARGBSubtractRow = ARGBSubtractRow_AVX2; |
| 1674 | } |
| 1675 | } |
| 1676 | #endif |
| 1677 | #if defined(HAS_ARGBSUBTRACTROW_NEON) |
| 1678 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1679 | ARGBSubtractRow = ARGBSubtractRow_Any_NEON; |
| 1680 | if (IS_ALIGNED(width, 8)) { |
| 1681 | ARGBSubtractRow = ARGBSubtractRow_NEON; |
| 1682 | } |
| 1683 | } |
| 1684 | #endif |
| 1685 | #if defined(HAS_ARGBSUBTRACTROW_MSA) |
| 1686 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1687 | ARGBSubtractRow = ARGBSubtractRow_Any_MSA; |
| 1688 | if (IS_ALIGNED(width, 8)) { |
| 1689 | ARGBSubtractRow = ARGBSubtractRow_MSA; |
| 1690 | } |
| 1691 | } |
| 1692 | #endif |
| 1693 | #if defined(HAS_ARGBSUBTRACTROW_MMI) |
| 1694 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1695 | ARGBSubtractRow = ARGBSubtractRow_Any_MMI; |
| 1696 | if (IS_ALIGNED(width, 2)) { |
| 1697 | ARGBSubtractRow = ARGBSubtractRow_MMI; |
| 1698 | } |
| 1699 | } |
| 1700 | #endif |
| 1701 |
| 1702 | // Subtract plane |
| 1703 | for (y = 0; y < height; ++y) { |
| 1704 | ARGBSubtractRow(src_argb0, src_argb1, dst_argb, width); |
| 1705 | src_argb0 += src_stride_argb0; |
| 1706 | src_argb1 += src_stride_argb1; |
| 1707 | dst_argb += dst_stride_argb; |
| 1708 | } |
| 1709 | return 0; |
| 1710 | } |
| 1711 | // Convert I422 to RGBA with matrix |
| 1712 | static int I422ToRGBAMatrix(const uint8_t* src_y, |
| 1713 | int src_stride_y, |
| 1714 | const uint8_t* src_u, |
| 1715 | int src_stride_u, |
| 1716 | const uint8_t* src_v, |
| 1717 | int src_stride_v, |
| 1718 | uint8_t* dst_rgba, |
| 1719 | int dst_stride_rgba, |
| 1720 | const struct YuvConstants* yuvconstants, |
| 1721 | int width, |
| 1722 | int height) { |
| 1723 | int y; |
| 1724 | void (*I422ToRGBARow)(const uint8_t* y_buf, const uint8_t* u_buf, |
| 1725 | const uint8_t* v_buf, uint8_t* rgb_buf, |
| 1726 | const struct YuvConstants* yuvconstants, int width) = |
| 1727 | I422ToRGBARow_C; |
| 1728 | if (!src_y || !src_u || !src_v || !dst_rgba || width <= 0 || height == 0) { |
| 1729 | return -1; |
| 1730 | } |
| 1731 | // Negative height means invert the image. |
| 1732 | if (height < 0) { |
| 1733 | height = -height; |
| 1734 | dst_rgba = dst_rgba + (height - 1) * dst_stride_rgba; |
| 1735 | dst_stride_rgba = -dst_stride_rgba; |
| 1736 | } |
| 1737 | #if defined(HAS_I422TORGBAROW_SSSE3) |
| 1738 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1739 | I422ToRGBARow = I422ToRGBARow_Any_SSSE3; |
| 1740 | if (IS_ALIGNED(width, 8)) { |
| 1741 | I422ToRGBARow = I422ToRGBARow_SSSE3; |
| 1742 | } |
| 1743 | } |
| 1744 | #endif |
| 1745 | #if defined(HAS_I422TORGBAROW_AVX2) |
| 1746 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1747 | I422ToRGBARow = I422ToRGBARow_Any_AVX2; |
| 1748 | if (IS_ALIGNED(width, 16)) { |
| 1749 | I422ToRGBARow = I422ToRGBARow_AVX2; |
| 1750 | } |
| 1751 | } |
| 1752 | #endif |
| 1753 | #if defined(HAS_I422TORGBAROW_NEON) |
| 1754 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1755 | I422ToRGBARow = I422ToRGBARow_Any_NEON; |
| 1756 | if (IS_ALIGNED(width, 8)) { |
| 1757 | I422ToRGBARow = I422ToRGBARow_NEON; |
| 1758 | } |
| 1759 | } |
| 1760 | #endif |
| 1761 | #if defined(HAS_I422TORGBAROW_MSA) |
| 1762 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1763 | I422ToRGBARow = I422ToRGBARow_Any_MSA; |
| 1764 | if (IS_ALIGNED(width, 8)) { |
| 1765 | I422ToRGBARow = I422ToRGBARow_MSA; |
| 1766 | } |
| 1767 | } |
| 1768 | #endif |
| 1769 | #if defined(HAS_I422TORGBAROW_MMI) |
| 1770 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1771 | I422ToRGBARow = I422ToRGBARow_Any_MMI; |
| 1772 | if (IS_ALIGNED(width, 4)) { |
| 1773 | I422ToRGBARow = I422ToRGBARow_MMI; |
| 1774 | } |
| 1775 | } |
| 1776 | #endif |
| 1777 |
| 1778 | for (y = 0; y < height; ++y) { |
| 1779 | I422ToRGBARow(src_y, src_u, src_v, dst_rgba, yuvconstants, width); |
| 1780 | dst_rgba += dst_stride_rgba; |
| 1781 | src_y += src_stride_y; |
| 1782 | src_u += src_stride_u; |
| 1783 | src_v += src_stride_v; |
| 1784 | } |
| 1785 | return 0; |
| 1786 | } |
| 1787 |
| 1788 | // Convert I422 to RGBA. |
| 1789 | LIBYUV_API |
| 1790 | int I422ToRGBA(const uint8_t* src_y, |
| 1791 | int src_stride_y, |
| 1792 | const uint8_t* src_u, |
| 1793 | int src_stride_u, |
| 1794 | const uint8_t* src_v, |
| 1795 | int src_stride_v, |
| 1796 | uint8_t* dst_rgba, |
| 1797 | int dst_stride_rgba, |
| 1798 | int width, |
| 1799 | int height) { |
| 1800 | return I422ToRGBAMatrix(src_y, src_stride_y, src_u, src_stride_u, src_v, |
| 1801 | src_stride_v, dst_rgba, dst_stride_rgba, |
| 1802 | &kYuvI601Constants, width, height); |
| 1803 | } |
| 1804 |
| 1805 | // Convert I422 to BGRA. |
| 1806 | LIBYUV_API |
| 1807 | int I422ToBGRA(const uint8_t* src_y, |
| 1808 | int src_stride_y, |
| 1809 | const uint8_t* src_u, |
| 1810 | int src_stride_u, |
| 1811 | const uint8_t* src_v, |
| 1812 | int src_stride_v, |
| 1813 | uint8_t* dst_bgra, |
| 1814 | int dst_stride_bgra, |
| 1815 | int width, |
| 1816 | int height) { |
| 1817 | return I422ToRGBAMatrix(src_y, src_stride_y, src_v, |
| 1818 | src_stride_v, // Swap U and V |
| 1819 | src_u, src_stride_u, dst_bgra, dst_stride_bgra, |
| 1820 | &kYvuI601Constants, // Use Yvu matrix |
| 1821 | width, height); |
| 1822 | } |
| 1823 |
| 1824 | // Convert NV12 to RGB565. |
| 1825 | LIBYUV_API |
| 1826 | int NV12ToRGB565(const uint8_t* src_y, |
| 1827 | int src_stride_y, |
| 1828 | const uint8_t* src_uv, |
| 1829 | int src_stride_uv, |
| 1830 | uint8_t* dst_rgb565, |
| 1831 | int dst_stride_rgb565, |
| 1832 | int width, |
| 1833 | int height) { |
| 1834 | int y; |
| 1835 | void (*NV12ToRGB565Row)( |
| 1836 | const uint8_t* y_buf, const uint8_t* uv_buf, uint8_t* rgb_buf, |
| 1837 | const struct YuvConstants* yuvconstants, int width) = NV12ToRGB565Row_C; |
| 1838 | if (!src_y || !src_uv || !dst_rgb565 || width <= 0 || height == 0) { |
| 1839 | return -1; |
| 1840 | } |
| 1841 | // Negative height means invert the image. |
| 1842 | if (height < 0) { |
| 1843 | height = -height; |
| 1844 | dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565; |
| 1845 | dst_stride_rgb565 = -dst_stride_rgb565; |
| 1846 | } |
| 1847 | #if defined(HAS_NV12TORGB565ROW_SSSE3) |
| 1848 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1849 | NV12ToRGB565Row = NV12ToRGB565Row_Any_SSSE3; |
| 1850 | if (IS_ALIGNED(width, 8)) { |
| 1851 | NV12ToRGB565Row = NV12ToRGB565Row_SSSE3; |
| 1852 | } |
| 1853 | } |
| 1854 | #endif |
| 1855 | #if defined(HAS_NV12TORGB565ROW_AVX2) |
| 1856 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1857 | NV12ToRGB565Row = NV12ToRGB565Row_Any_AVX2; |
| 1858 | if (IS_ALIGNED(width, 16)) { |
| 1859 | NV12ToRGB565Row = NV12ToRGB565Row_AVX2; |
| 1860 | } |
| 1861 | } |
| 1862 | #endif |
| 1863 | #if defined(HAS_NV12TORGB565ROW_NEON) |
| 1864 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1865 | NV12ToRGB565Row = NV12ToRGB565Row_Any_NEON; |
| 1866 | if (IS_ALIGNED(width, 8)) { |
| 1867 | NV12ToRGB565Row = NV12ToRGB565Row_NEON; |
| 1868 | } |
| 1869 | } |
| 1870 | #endif |
| 1871 | #if defined(HAS_NV12TORGB565ROW_MSA) |
| 1872 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1873 | NV12ToRGB565Row = NV12ToRGB565Row_Any_MSA; |
| 1874 | if (IS_ALIGNED(width, 8)) { |
| 1875 | NV12ToRGB565Row = NV12ToRGB565Row_MSA; |
| 1876 | } |
| 1877 | } |
| 1878 | #endif |
| 1879 | #if defined(HAS_NV12TORGB565ROW_MMI) |
| 1880 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1881 | NV12ToRGB565Row = NV12ToRGB565Row_Any_MMI; |
| 1882 | if (IS_ALIGNED(width, 4)) { |
| 1883 | NV12ToRGB565Row = NV12ToRGB565Row_MMI; |
| 1884 | } |
| 1885 | } |
| 1886 | #endif |
| 1887 |
| 1888 | for (y = 0; y < height; ++y) { |
| 1889 | NV12ToRGB565Row(src_y, src_uv, dst_rgb565, &kYuvI601Constants, width); |
| 1890 | dst_rgb565 += dst_stride_rgb565; |
| 1891 | src_y += src_stride_y; |
| 1892 | if (y & 1) { |
| 1893 | src_uv += src_stride_uv; |
| 1894 | } |
| 1895 | } |
| 1896 | return 0; |
| 1897 | } |
| 1898 |
| 1899 | // Convert RAW to RGB24. |
| 1900 | LIBYUV_API |
| 1901 | int RAWToRGB24(const uint8_t* src_raw, |
| 1902 | int src_stride_raw, |
| 1903 | uint8_t* dst_rgb24, |
| 1904 | int dst_stride_rgb24, |
| 1905 | int width, |
| 1906 | int height) { |
| 1907 | int y; |
| 1908 | void (*RAWToRGB24Row)(const uint8_t* src_rgb, uint8_t* dst_rgb24, int width) = |
| 1909 | RAWToRGB24Row_C; |
| 1910 | if (!src_raw || !dst_rgb24 || width <= 0 || height == 0) { |
| 1911 | return -1; |
| 1912 | } |
| 1913 | // Negative height means invert the image. |
| 1914 | if (height < 0) { |
| 1915 | height = -height; |
| 1916 | src_raw = src_raw + (height - 1) * src_stride_raw; |
| 1917 | src_stride_raw = -src_stride_raw; |
| 1918 | } |
| 1919 | // Coalesce rows. |
| 1920 | if (src_stride_raw == width * 3 && dst_stride_rgb24 == width * 3) { |
| 1921 | width *= height; |
| 1922 | height = 1; |
| 1923 | src_stride_raw = dst_stride_rgb24 = 0; |
| 1924 | } |
| 1925 | #if defined(HAS_RAWTORGB24ROW_SSSE3) |
| 1926 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1927 | RAWToRGB24Row = RAWToRGB24Row_Any_SSSE3; |
| 1928 | if (IS_ALIGNED(width, 8)) { |
| 1929 | RAWToRGB24Row = RAWToRGB24Row_SSSE3; |
| 1930 | } |
| 1931 | } |
| 1932 | #endif |
| 1933 | #if defined(HAS_RAWTORGB24ROW_NEON) |
| 1934 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1935 | RAWToRGB24Row = RAWToRGB24Row_Any_NEON; |
| 1936 | if (IS_ALIGNED(width, 8)) { |
| 1937 | RAWToRGB24Row = RAWToRGB24Row_NEON; |
| 1938 | } |
| 1939 | } |
| 1940 | #endif |
| 1941 | #if defined(HAS_RAWTORGB24ROW_MSA) |
| 1942 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1943 | RAWToRGB24Row = RAWToRGB24Row_Any_MSA; |
| 1944 | if (IS_ALIGNED(width, 16)) { |
| 1945 | RAWToRGB24Row = RAWToRGB24Row_MSA; |
| 1946 | } |
| 1947 | } |
| 1948 | #endif |
| 1949 | #if defined(HAS_RAWTORGB24ROW_MMI) |
| 1950 | if (TestCpuFlag(kCpuHasMMI)) { |
| 1951 | RAWToRGB24Row = RAWToRGB24Row_Any_MMI; |
| 1952 | if (IS_ALIGNED(width, 4)) { |
| 1953 | RAWToRGB24Row = RAWToRGB24Row_MMI; |
| 1954 | } |
| 1955 | } |
| 1956 | #endif |
| 1957 |
| 1958 | for (y = 0; y < height; ++y) { |
| 1959 | RAWToRGB24Row(src_raw, dst_rgb24, width); |
| 1960 | src_raw += src_stride_raw; |
| 1961 | dst_rgb24 += dst_stride_rgb24; |
| 1962 | } |
| 1963 | return 0; |
| 1964 | } |
| 1965 |
| 1966 | LIBYUV_API |
| 1967 | void SetPlane(uint8_t* dst_y, |
| 1968 | int dst_stride_y, |
| 1969 | int width, |
| 1970 | int height, |
| 1971 | uint32_t value) { |
| 1972 | int y; |
| 1973 | void (*SetRow)(uint8_t * dst, uint8_t value, int width) = SetRow_C; |
| 1974 | if (height < 0) { |
| 1975 | height = -height; |
| 1976 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 1977 | dst_stride_y = -dst_stride_y; |
| 1978 | } |
| 1979 | // Coalesce rows. |
| 1980 | if (dst_stride_y == width) { |
| 1981 | width *= height; |
| 1982 | height = 1; |
| 1983 | dst_stride_y = 0; |
| 1984 | } |
| 1985 | #if defined(HAS_SETROW_NEON) |
| 1986 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1987 | SetRow = SetRow_Any_NEON; |
| 1988 | if (IS_ALIGNED(width, 16)) { |
| 1989 | SetRow = SetRow_NEON; |
| 1990 | } |
| 1991 | } |
| 1992 | #endif |
| 1993 | #if defined(HAS_SETROW_X86) |
| 1994 | if (TestCpuFlag(kCpuHasX86)) { |
| 1995 | SetRow = SetRow_Any_X86; |
| 1996 | if (IS_ALIGNED(width, 4)) { |
| 1997 | SetRow = SetRow_X86; |
| 1998 | } |
| 1999 | } |
| 2000 | #endif |
| 2001 | #if defined(HAS_SETROW_ERMS) |
| 2002 | if (TestCpuFlag(kCpuHasERMS)) { |
| 2003 | SetRow = SetRow_ERMS; |
| 2004 | } |
| 2005 | #endif |
| 2006 | #if defined(HAS_SETROW_MSA) |
| 2007 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 16)) { |
| 2008 | SetRow = SetRow_MSA; |
| 2009 | } |
| 2010 | #endif |
| 2011 |
| 2012 | // Set plane |
| 2013 | for (y = 0; y < height; ++y) { |
| 2014 | SetRow(dst_y, value, width); |
| 2015 | dst_y += dst_stride_y; |
| 2016 | } |
| 2017 | } |
| 2018 |
| 2019 | // Draw a rectangle into I420 |
| 2020 | LIBYUV_API |
| 2021 | int I420Rect(uint8_t* dst_y, |
| 2022 | int dst_stride_y, |
| 2023 | uint8_t* dst_u, |
| 2024 | int dst_stride_u, |
| 2025 | uint8_t* dst_v, |
| 2026 | int dst_stride_v, |
| 2027 | int x, |
| 2028 | int y, |
| 2029 | int width, |
| 2030 | int height, |
| 2031 | int value_y, |
| 2032 | int value_u, |
| 2033 | int value_v) { |
| 2034 | int halfwidth = (width + 1) >> 1; |
| 2035 | int halfheight = (height + 1) >> 1; |
| 2036 | uint8_t* start_y = dst_y + y * dst_stride_y + x; |
| 2037 | uint8_t* start_u = dst_u + (y / 2) * dst_stride_u + (x / 2); |
| 2038 | uint8_t* start_v = dst_v + (y / 2) * dst_stride_v + (x / 2); |
| 2039 | if (!dst_y || !dst_u || !dst_v || width <= 0 || height == 0 || x < 0 || |
| 2040 | y < 0 || value_y < 0 || value_y > 255 || value_u < 0 || value_u > 255 || |
| 2041 | value_v < 0 || value_v > 255) { |
| 2042 | return -1; |
| 2043 | } |
| 2044 |
| 2045 | SetPlane(start_y, dst_stride_y, width, height, value_y); |
| 2046 | SetPlane(start_u, dst_stride_u, halfwidth, halfheight, value_u); |
| 2047 | SetPlane(start_v, dst_stride_v, halfwidth, halfheight, value_v); |
| 2048 | return 0; |
| 2049 | } |
| 2050 |
| 2051 | // Draw a rectangle into ARGB |
| 2052 | LIBYUV_API |
| 2053 | int ARGBRect(uint8_t* dst_argb, |
| 2054 | int dst_stride_argb, |
| 2055 | int dst_x, |
| 2056 | int dst_y, |
| 2057 | int width, |
| 2058 | int height, |
| 2059 | uint32_t value) { |
| 2060 | int y; |
| 2061 | void (*ARGBSetRow)(uint8_t * dst_argb, uint32_t value, int width) = |
| 2062 | ARGBSetRow_C; |
| 2063 | if (!dst_argb || width <= 0 || height == 0 || dst_x < 0 || dst_y < 0) { |
| 2064 | return -1; |
| 2065 | } |
| 2066 | if (height < 0) { |
| 2067 | height = -height; |
| 2068 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 2069 | dst_stride_argb = -dst_stride_argb; |
| 2070 | } |
| 2071 | dst_argb += dst_y * dst_stride_argb + dst_x * 4; |
| 2072 | // Coalesce rows. |
| 2073 | if (dst_stride_argb == width * 4) { |
| 2074 | width *= height; |
| 2075 | height = 1; |
| 2076 | dst_stride_argb = 0; |
| 2077 | } |
| 2078 |
| 2079 | #if defined(HAS_ARGBSETROW_NEON) |
| 2080 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2081 | ARGBSetRow = ARGBSetRow_Any_NEON; |
| 2082 | if (IS_ALIGNED(width, 4)) { |
| 2083 | ARGBSetRow = ARGBSetRow_NEON; |
| 2084 | } |
| 2085 | } |
| 2086 | #endif |
| 2087 | #if defined(HAS_ARGBSETROW_X86) |
| 2088 | if (TestCpuFlag(kCpuHasX86)) { |
| 2089 | ARGBSetRow = ARGBSetRow_X86; |
| 2090 | } |
| 2091 | #endif |
| 2092 | #if defined(HAS_ARGBSETROW_MSA) |
| 2093 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2094 | ARGBSetRow = ARGBSetRow_Any_MSA; |
| 2095 | if (IS_ALIGNED(width, 4)) { |
| 2096 | ARGBSetRow = ARGBSetRow_MSA; |
| 2097 | } |
| 2098 | } |
| 2099 | #endif |
| 2100 | #if defined(HAS_ARGBSETROW_MMI) |
| 2101 | if (TestCpuFlag(kCpuHasMMI)) { |
| 2102 | ARGBSetRow = ARGBSetRow_Any_MMI; |
| 2103 | if (IS_ALIGNED(width, 4)) { |
| 2104 | ARGBSetRow = ARGBSetRow_MMI; |
| 2105 | } |
| 2106 | } |
| 2107 | #endif |
| 2108 |
| 2109 | // Set plane |
| 2110 | for (y = 0; y < height; ++y) { |
| 2111 | ARGBSetRow(dst_argb, value, width); |
| 2112 | dst_argb += dst_stride_argb; |
| 2113 | } |
| 2114 | return 0; |
| 2115 | } |
| 2116 |
| 2117 | // Convert unattentuated ARGB to preattenuated ARGB. |
| 2118 | // An unattenutated ARGB alpha blend uses the formula |
| 2119 | // p = a * f + (1 - a) * b |
| 2120 | // where |
| 2121 | // p is output pixel |
| 2122 | // f is foreground pixel |
| 2123 | // b is background pixel |
| 2124 | // a is alpha value from foreground pixel |
| 2125 | // An preattenutated ARGB alpha blend uses the formula |
| 2126 | // p = f + (1 - a) * b |
| 2127 | // where |
| 2128 | // f is foreground pixel premultiplied by alpha |
| 2129 |
| 2130 | LIBYUV_API |
| 2131 | int ARGBAttenuate(const uint8_t* src_argb, |
| 2132 | int src_stride_argb, |
| 2133 | uint8_t* dst_argb, |
| 2134 | int dst_stride_argb, |
| 2135 | int width, |
| 2136 | int height) { |
| 2137 | int y; |
| 2138 | void (*ARGBAttenuateRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 2139 | int width) = ARGBAttenuateRow_C; |
| 2140 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 2141 | return -1; |
| 2142 | } |
| 2143 | if (height < 0) { |
| 2144 | height = -height; |
| 2145 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2146 | src_stride_argb = -src_stride_argb; |
| 2147 | } |
| 2148 | // Coalesce rows. |
| 2149 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 2150 | width *= height; |
| 2151 | height = 1; |
| 2152 | src_stride_argb = dst_stride_argb = 0; |
| 2153 | } |
| 2154 | #if defined(HAS_ARGBATTENUATEROW_SSSE3) |
| 2155 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2156 | ARGBAttenuateRow = ARGBAttenuateRow_Any_SSSE3; |
| 2157 | if (IS_ALIGNED(width, 4)) { |
| 2158 | ARGBAttenuateRow = ARGBAttenuateRow_SSSE3; |
| 2159 | } |
| 2160 | } |
| 2161 | #endif |
| 2162 | #if defined(HAS_ARGBATTENUATEROW_AVX2) |
| 2163 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2164 | ARGBAttenuateRow = ARGBAttenuateRow_Any_AVX2; |
| 2165 | if (IS_ALIGNED(width, 8)) { |
| 2166 | ARGBAttenuateRow = ARGBAttenuateRow_AVX2; |
| 2167 | } |
| 2168 | } |
| 2169 | #endif |
| 2170 | #if defined(HAS_ARGBATTENUATEROW_NEON) |
| 2171 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2172 | ARGBAttenuateRow = ARGBAttenuateRow_Any_NEON; |
| 2173 | if (IS_ALIGNED(width, 8)) { |
| 2174 | ARGBAttenuateRow = ARGBAttenuateRow_NEON; |
| 2175 | } |
| 2176 | } |
| 2177 | #endif |
| 2178 | #if defined(HAS_ARGBATTENUATEROW_MSA) |
| 2179 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2180 | ARGBAttenuateRow = ARGBAttenuateRow_Any_MSA; |
| 2181 | if (IS_ALIGNED(width, 8)) { |
| 2182 | ARGBAttenuateRow = ARGBAttenuateRow_MSA; |
| 2183 | } |
| 2184 | } |
| 2185 | #endif |
| 2186 | #if defined(HAS_ARGBATTENUATEROW_MMI) |
| 2187 | if (TestCpuFlag(kCpuHasMMI)) { |
| 2188 | ARGBAttenuateRow = ARGBAttenuateRow_Any_MMI; |
| 2189 | if (IS_ALIGNED(width, 2)) { |
| 2190 | ARGBAttenuateRow = ARGBAttenuateRow_MMI; |
| 2191 | } |
| 2192 | } |
| 2193 | #endif |
| 2194 |
| 2195 | for (y = 0; y < height; ++y) { |
| 2196 | ARGBAttenuateRow(src_argb, dst_argb, width); |
| 2197 | src_argb += src_stride_argb; |
| 2198 | dst_argb += dst_stride_argb; |
| 2199 | } |
| 2200 | return 0; |
| 2201 | } |
| 2202 |
| 2203 | // Convert preattentuated ARGB to unattenuated ARGB. |
| 2204 | LIBYUV_API |
| 2205 | int ARGBUnattenuate(const uint8_t* src_argb, |
| 2206 | int src_stride_argb, |
| 2207 | uint8_t* dst_argb, |
| 2208 | int dst_stride_argb, |
| 2209 | int width, |
| 2210 | int height) { |
| 2211 | int y; |
| 2212 | void (*ARGBUnattenuateRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 2213 | int width) = ARGBUnattenuateRow_C; |
| 2214 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 2215 | return -1; |
| 2216 | } |
| 2217 | if (height < 0) { |
| 2218 | height = -height; |
| 2219 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2220 | src_stride_argb = -src_stride_argb; |
| 2221 | } |
| 2222 | // Coalesce rows. |
| 2223 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 2224 | width *= height; |
| 2225 | height = 1; |
| 2226 | src_stride_argb = dst_stride_argb = 0; |
| 2227 | } |
| 2228 | #if defined(HAS_ARGBUNATTENUATEROW_SSE2) |
| 2229 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2230 | ARGBUnattenuateRow = ARGBUnattenuateRow_Any_SSE2; |
| 2231 | if (IS_ALIGNED(width, 4)) { |
| 2232 | ARGBUnattenuateRow = ARGBUnattenuateRow_SSE2; |
| 2233 | } |
| 2234 | } |
| 2235 | #endif |
| 2236 | #if defined(HAS_ARGBUNATTENUATEROW_AVX2) |
| 2237 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2238 | ARGBUnattenuateRow = ARGBUnattenuateRow_Any_AVX2; |
| 2239 | if (IS_ALIGNED(width, 8)) { |
| 2240 | ARGBUnattenuateRow = ARGBUnattenuateRow_AVX2; |
| 2241 | } |
| 2242 | } |
| 2243 | #endif |
| 2244 | // TODO(fbarchard): Neon version. |
| 2245 |
| 2246 | for (y = 0; y < height; ++y) { |
| 2247 | ARGBUnattenuateRow(src_argb, dst_argb, width); |
| 2248 | src_argb += src_stride_argb; |
| 2249 | dst_argb += dst_stride_argb; |
| 2250 | } |
| 2251 | return 0; |
| 2252 | } |
| 2253 |
| 2254 | // Convert ARGB to Grayed ARGB. |
| 2255 | LIBYUV_API |
| 2256 | int ARGBGrayTo(const uint8_t* src_argb, |
| 2257 | int src_stride_argb, |
| 2258 | uint8_t* dst_argb, |
| 2259 | int dst_stride_argb, |
| 2260 | int width, |
| 2261 | int height) { |
| 2262 | int y; |
| 2263 | void (*ARGBGrayRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) = |
| 2264 | ARGBGrayRow_C; |
| 2265 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 2266 | return -1; |
| 2267 | } |
| 2268 | if (height < 0) { |
| 2269 | height = -height; |
| 2270 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2271 | src_stride_argb = -src_stride_argb; |
| 2272 | } |
| 2273 | // Coalesce rows. |
| 2274 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 2275 | width *= height; |
| 2276 | height = 1; |
| 2277 | src_stride_argb = dst_stride_argb = 0; |
| 2278 | } |
| 2279 | #if defined(HAS_ARGBGRAYROW_SSSE3) |
| 2280 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 2281 | ARGBGrayRow = ARGBGrayRow_SSSE3; |
| 2282 | } |
| 2283 | #endif |
| 2284 | #if defined(HAS_ARGBGRAYROW_NEON) |
| 2285 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 2286 | ARGBGrayRow = ARGBGrayRow_NEON; |
| 2287 | } |
| 2288 | #endif |
| 2289 | #if defined(HAS_ARGBGRAYROW_MSA) |
| 2290 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 2291 | ARGBGrayRow = ARGBGrayRow_MSA; |
| 2292 | } |
| 2293 | #endif |
| 2294 | #if defined(HAS_ARGBGRAYROW_MMI) |
| 2295 | if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) { |
| 2296 | ARGBGrayRow = ARGBGrayRow_MMI; |
| 2297 | } |
| 2298 | #endif |
| 2299 |
| 2300 | for (y = 0; y < height; ++y) { |
| 2301 | ARGBGrayRow(src_argb, dst_argb, width); |
| 2302 | src_argb += src_stride_argb; |
| 2303 | dst_argb += dst_stride_argb; |
| 2304 | } |
| 2305 | return 0; |
| 2306 | } |
| 2307 |
| 2308 | // Make a rectangle of ARGB gray scale. |
| 2309 | LIBYUV_API |
| 2310 | int ARGBGray(uint8_t* dst_argb, |
| 2311 | int dst_stride_argb, |
| 2312 | int dst_x, |
| 2313 | int dst_y, |
| 2314 | int width, |
| 2315 | int height) { |
| 2316 | int y; |
| 2317 | void (*ARGBGrayRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) = |
| 2318 | ARGBGrayRow_C; |
| 2319 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 2320 | if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) { |
| 2321 | return -1; |
| 2322 | } |
| 2323 | // Coalesce rows. |
| 2324 | if (dst_stride_argb == width * 4) { |
| 2325 | width *= height; |
| 2326 | height = 1; |
| 2327 | dst_stride_argb = 0; |
| 2328 | } |
| 2329 | #if defined(HAS_ARGBGRAYROW_SSSE3) |
| 2330 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 2331 | ARGBGrayRow = ARGBGrayRow_SSSE3; |
| 2332 | } |
| 2333 | #endif |
| 2334 | #if defined(HAS_ARGBGRAYROW_NEON) |
| 2335 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 2336 | ARGBGrayRow = ARGBGrayRow_NEON; |
| 2337 | } |
| 2338 | #endif |
| 2339 | #if defined(HAS_ARGBGRAYROW_MSA) |
| 2340 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 2341 | ARGBGrayRow = ARGBGrayRow_MSA; |
| 2342 | } |
| 2343 | #endif |
| 2344 | #if defined(HAS_ARGBGRAYROW_MMI) |
| 2345 | if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) { |
| 2346 | ARGBGrayRow = ARGBGrayRow_MMI; |
| 2347 | } |
| 2348 | #endif |
| 2349 |
| 2350 | for (y = 0; y < height; ++y) { |
| 2351 | ARGBGrayRow(dst, dst, width); |
| 2352 | dst += dst_stride_argb; |
| 2353 | } |
| 2354 | return 0; |
| 2355 | } |
| 2356 |
| 2357 | // Make a rectangle of ARGB Sepia tone. |
| 2358 | LIBYUV_API |
| 2359 | int ARGBSepia(uint8_t* dst_argb, |
| 2360 | int dst_stride_argb, |
| 2361 | int dst_x, |
| 2362 | int dst_y, |
| 2363 | int width, |
| 2364 | int height) { |
| 2365 | int y; |
| 2366 | void (*ARGBSepiaRow)(uint8_t * dst_argb, int width) = ARGBSepiaRow_C; |
| 2367 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 2368 | if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) { |
| 2369 | return -1; |
| 2370 | } |
| 2371 | // Coalesce rows. |
| 2372 | if (dst_stride_argb == width * 4) { |
| 2373 | width *= height; |
| 2374 | height = 1; |
| 2375 | dst_stride_argb = 0; |
| 2376 | } |
| 2377 | #if defined(HAS_ARGBSEPIAROW_SSSE3) |
| 2378 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 2379 | ARGBSepiaRow = ARGBSepiaRow_SSSE3; |
| 2380 | } |
| 2381 | #endif |
| 2382 | #if defined(HAS_ARGBSEPIAROW_NEON) |
| 2383 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 2384 | ARGBSepiaRow = ARGBSepiaRow_NEON; |
| 2385 | } |
| 2386 | #endif |
| 2387 | #if defined(HAS_ARGBSEPIAROW_MSA) |
| 2388 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 2389 | ARGBSepiaRow = ARGBSepiaRow_MSA; |
| 2390 | } |
| 2391 | #endif |
| 2392 | #if defined(HAS_ARGBSEPIAROW_MMI) |
| 2393 | if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) { |
| 2394 | ARGBSepiaRow = ARGBSepiaRow_MMI; |
| 2395 | } |
| 2396 | #endif |
| 2397 |
| 2398 | for (y = 0; y < height; ++y) { |
| 2399 | ARGBSepiaRow(dst, width); |
| 2400 | dst += dst_stride_argb; |
| 2401 | } |
| 2402 | return 0; |
| 2403 | } |
| 2404 |
| 2405 | // Apply a 4x4 matrix to each ARGB pixel. |
| 2406 | // Note: Normally for shading, but can be used to swizzle or invert. |
| 2407 | LIBYUV_API |
| 2408 | int ARGBColorMatrix(const uint8_t* src_argb, |
| 2409 | int src_stride_argb, |
| 2410 | uint8_t* dst_argb, |
| 2411 | int dst_stride_argb, |
| 2412 | const int8_t* matrix_argb, |
| 2413 | int width, |
| 2414 | int height) { |
| 2415 | int y; |
| 2416 | void (*ARGBColorMatrixRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 2417 | const int8_t* matrix_argb, int width) = |
| 2418 | ARGBColorMatrixRow_C; |
| 2419 | if (!src_argb || !dst_argb || !matrix_argb || width <= 0 || height == 0) { |
| 2420 | return -1; |
| 2421 | } |
| 2422 | if (height < 0) { |
| 2423 | height = -height; |
| 2424 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2425 | src_stride_argb = -src_stride_argb; |
| 2426 | } |
| 2427 | // Coalesce rows. |
| 2428 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 2429 | width *= height; |
| 2430 | height = 1; |
| 2431 | src_stride_argb = dst_stride_argb = 0; |
| 2432 | } |
| 2433 | #if defined(HAS_ARGBCOLORMATRIXROW_SSSE3) |
| 2434 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 2435 | ARGBColorMatrixRow = ARGBColorMatrixRow_SSSE3; |
| 2436 | } |
| 2437 | #endif |
| 2438 | #if defined(HAS_ARGBCOLORMATRIXROW_NEON) |
| 2439 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 2440 | ARGBColorMatrixRow = ARGBColorMatrixRow_NEON; |
| 2441 | } |
| 2442 | #endif |
| 2443 | #if defined(HAS_ARGBCOLORMATRIXROW_MSA) |
| 2444 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 2445 | ARGBColorMatrixRow = ARGBColorMatrixRow_MSA; |
| 2446 | } |
| 2447 | #endif |
| 2448 | #if defined(HAS_ARGBCOLORMATRIXROW_MMI) |
| 2449 | if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) { |
| 2450 | ARGBColorMatrixRow = ARGBColorMatrixRow_MMI; |
| 2451 | } |
| 2452 | #endif |
| 2453 | for (y = 0; y < height; ++y) { |
| 2454 | ARGBColorMatrixRow(src_argb, dst_argb, matrix_argb, width); |
| 2455 | src_argb += src_stride_argb; |
| 2456 | dst_argb += dst_stride_argb; |
| 2457 | } |
| 2458 | return 0; |
| 2459 | } |
| 2460 |
| 2461 | // Apply a 4x3 matrix to each ARGB pixel. |
| 2462 | // Deprecated. |
| 2463 | LIBYUV_API |
| 2464 | int RGBColorMatrix(uint8_t* dst_argb, |
| 2465 | int dst_stride_argb, |
| 2466 | const int8_t* matrix_rgb, |
| 2467 | int dst_x, |
| 2468 | int dst_y, |
| 2469 | int width, |
| 2470 | int height) { |
| 2471 | SIMD_ALIGNED(int8_t matrix_argb[16]); |
| 2472 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 2473 | if (!dst_argb || !matrix_rgb || width <= 0 || height <= 0 || dst_x < 0 || |
| 2474 | dst_y < 0) { |
| 2475 | return -1; |
| 2476 | } |
| 2477 |
| 2478 | // Convert 4x3 7 bit matrix to 4x4 6 bit matrix. |
| 2479 | matrix_argb[0] = matrix_rgb[0] / 2; |
| 2480 | matrix_argb[1] = matrix_rgb[1] / 2; |
| 2481 | matrix_argb[2] = matrix_rgb[2] / 2; |
| 2482 | matrix_argb[3] = matrix_rgb[3] / 2; |
| 2483 | matrix_argb[4] = matrix_rgb[4] / 2; |
| 2484 | matrix_argb[5] = matrix_rgb[5] / 2; |
| 2485 | matrix_argb[6] = matrix_rgb[6] / 2; |
| 2486 | matrix_argb[7] = matrix_rgb[7] / 2; |
| 2487 | matrix_argb[8] = matrix_rgb[8] / 2; |
| 2488 | matrix_argb[9] = matrix_rgb[9] / 2; |
| 2489 | matrix_argb[10] = matrix_rgb[10] / 2; |
| 2490 | matrix_argb[11] = matrix_rgb[11] / 2; |
| 2491 | matrix_argb[14] = matrix_argb[13] = matrix_argb[12] = 0; |
| 2492 | matrix_argb[15] = 64; // 1.0 |
| 2493 |
| 2494 | return ARGBColorMatrix((const uint8_t*)(dst), dst_stride_argb, dst, |
| 2495 | dst_stride_argb, &matrix_argb[0], width, height); |
| 2496 | } |
| 2497 |
| 2498 | // Apply a color table each ARGB pixel. |
| 2499 | // Table contains 256 ARGB values. |
| 2500 | LIBYUV_API |
| 2501 | int ARGBColorTable(uint8_t* dst_argb, |
| 2502 | int dst_stride_argb, |
| 2503 | const uint8_t* table_argb, |
| 2504 | int dst_x, |
| 2505 | int dst_y, |
| 2506 | int width, |
| 2507 | int height) { |
| 2508 | int y; |
| 2509 | void (*ARGBColorTableRow)(uint8_t * dst_argb, const uint8_t* table_argb, |
| 2510 | int width) = ARGBColorTableRow_C; |
| 2511 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 2512 | if (!dst_argb || !table_argb || width <= 0 || height <= 0 || dst_x < 0 || |
| 2513 | dst_y < 0) { |
| 2514 | return -1; |
| 2515 | } |
| 2516 | // Coalesce rows. |
| 2517 | if (dst_stride_argb == width * 4) { |
| 2518 | width *= height; |
| 2519 | height = 1; |
| 2520 | dst_stride_argb = 0; |
| 2521 | } |
| 2522 | #if defined(HAS_ARGBCOLORTABLEROW_X86) |
| 2523 | if (TestCpuFlag(kCpuHasX86)) { |
| 2524 | ARGBColorTableRow = ARGBColorTableRow_X86; |
| 2525 | } |
| 2526 | #endif |
| 2527 | for (y = 0; y < height; ++y) { |
| 2528 | ARGBColorTableRow(dst, table_argb, width); |
| 2529 | dst += dst_stride_argb; |
| 2530 | } |
| 2531 | return 0; |
| 2532 | } |
| 2533 |
| 2534 | // Apply a color table each ARGB pixel but preserve destination alpha. |
| 2535 | // Table contains 256 ARGB values. |
| 2536 | LIBYUV_API |
| 2537 | int RGBColorTable(uint8_t* dst_argb, |
| 2538 | int dst_stride_argb, |
| 2539 | const uint8_t* table_argb, |
| 2540 | int dst_x, |
| 2541 | int dst_y, |
| 2542 | int width, |
| 2543 | int height) { |
| 2544 | int y; |
| 2545 | void (*RGBColorTableRow)(uint8_t * dst_argb, const uint8_t* table_argb, |
| 2546 | int width) = RGBColorTableRow_C; |
| 2547 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 2548 | if (!dst_argb || !table_argb || width <= 0 || height <= 0 || dst_x < 0 || |
| 2549 | dst_y < 0) { |
| 2550 | return -1; |
| 2551 | } |
| 2552 | // Coalesce rows. |
| 2553 | if (dst_stride_argb == width * 4) { |
| 2554 | width *= height; |
| 2555 | height = 1; |
| 2556 | dst_stride_argb = 0; |
| 2557 | } |
| 2558 | #if defined(HAS_RGBCOLORTABLEROW_X86) |
| 2559 | if (TestCpuFlag(kCpuHasX86)) { |
| 2560 | RGBColorTableRow = RGBColorTableRow_X86; |
| 2561 | } |
| 2562 | #endif |
| 2563 | for (y = 0; y < height; ++y) { |
| 2564 | RGBColorTableRow(dst, table_argb, width); |
| 2565 | dst += dst_stride_argb; |
| 2566 | } |
| 2567 | return 0; |
| 2568 | } |
| 2569 |
| 2570 | // ARGBQuantize is used to posterize art. |
| 2571 | // e.g. rgb / qvalue * qvalue + qvalue / 2 |
| 2572 | // But the low levels implement efficiently with 3 parameters, and could be |
| 2573 | // used for other high level operations. |
| 2574 | // dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset; |
| 2575 | // where scale is 1 / interval_size as a fixed point value. |
| 2576 | // The divide is replaces with a multiply by reciprocal fixed point multiply. |
| 2577 | // Caveat - although SSE2 saturates, the C function does not and should be used |
| 2578 | // with care if doing anything but quantization. |
| 2579 | LIBYUV_API |
| 2580 | int ARGBQuantize(uint8_t* dst_argb, |
| 2581 | int dst_stride_argb, |
| 2582 | int scale, |
| 2583 | int interval_size, |
| 2584 | int interval_offset, |
| 2585 | int dst_x, |
| 2586 | int dst_y, |
| 2587 | int width, |
| 2588 | int height) { |
| 2589 | int y; |
| 2590 | void (*ARGBQuantizeRow)(uint8_t * dst_argb, int scale, int interval_size, |
| 2591 | int interval_offset, int width) = ARGBQuantizeRow_C; |
| 2592 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 2593 | if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0 || |
| 2594 | interval_size < 1 || interval_size > 255) { |
| 2595 | return -1; |
| 2596 | } |
| 2597 | // Coalesce rows. |
| 2598 | if (dst_stride_argb == width * 4) { |
| 2599 | width *= height; |
| 2600 | height = 1; |
| 2601 | dst_stride_argb = 0; |
| 2602 | } |
| 2603 | #if defined(HAS_ARGBQUANTIZEROW_SSE2) |
| 2604 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) { |
| 2605 | ARGBQuantizeRow = ARGBQuantizeRow_SSE2; |
| 2606 | } |
| 2607 | #endif |
| 2608 | #if defined(HAS_ARGBQUANTIZEROW_NEON) |
| 2609 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 2610 | ARGBQuantizeRow = ARGBQuantizeRow_NEON; |
| 2611 | } |
| 2612 | #endif |
| 2613 | #if defined(HAS_ARGBQUANTIZEROW_MSA) |
| 2614 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 2615 | ARGBQuantizeRow = ARGBQuantizeRow_MSA; |
| 2616 | } |
| 2617 | #endif |
| 2618 | for (y = 0; y < height; ++y) { |
| 2619 | ARGBQuantizeRow(dst, scale, interval_size, interval_offset, width); |
| 2620 | dst += dst_stride_argb; |
| 2621 | } |
| 2622 | return 0; |
| 2623 | } |
| 2624 |
| 2625 | // Computes table of cumulative sum for image where the value is the sum |
| 2626 | // of all values above and to the left of the entry. Used by ARGBBlur. |
| 2627 | LIBYUV_API |
| 2628 | int ARGBComputeCumulativeSum(const uint8_t* src_argb, |
| 2629 | int src_stride_argb, |
| 2630 | int32_t* dst_cumsum, |
| 2631 | int dst_stride32_cumsum, |
| 2632 | int width, |
| 2633 | int height) { |
| 2634 | int y; |
| 2635 | void (*ComputeCumulativeSumRow)(const uint8_t* row, int32_t* cumsum, |
| 2636 | const int32_t* previous_cumsum, int width) = |
| 2637 | ComputeCumulativeSumRow_C; |
| 2638 | int32_t* previous_cumsum = dst_cumsum; |
| 2639 | if (!dst_cumsum || !src_argb || width <= 0 || height <= 0) { |
| 2640 | return -1; |
| 2641 | } |
| 2642 | #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2) |
| 2643 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2644 | ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2; |
| 2645 | } |
| 2646 | #endif |
| 2647 | #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_MMI) |
| 2648 | if (TestCpuFlag(kCpuHasMMI)) { |
| 2649 | ComputeCumulativeSumRow = ComputeCumulativeSumRow_MMI; |
| 2650 | } |
| 2651 | #endif |
| 2652 |
| 2653 | memset(dst_cumsum, 0, width * sizeof(dst_cumsum[0]) * 4); // 4 int per pixel. |
| 2654 | for (y = 0; y < height; ++y) { |
| 2655 | ComputeCumulativeSumRow(src_argb, dst_cumsum, previous_cumsum, width); |
| 2656 | previous_cumsum = dst_cumsum; |
| 2657 | dst_cumsum += dst_stride32_cumsum; |
| 2658 | src_argb += src_stride_argb; |
| 2659 | } |
| 2660 | return 0; |
| 2661 | } |
| 2662 |
| 2663 | // Blur ARGB image. |
| 2664 | // Caller should allocate CumulativeSum table of width * height * 16 bytes |
| 2665 | // aligned to 16 byte boundary. height can be radius * 2 + 2 to save memory |
| 2666 | // as the buffer is treated as circular. |
| 2667 | LIBYUV_API |
| 2668 | int ARGBBlur(const uint8_t* src_argb, |
| 2669 | int src_stride_argb, |
| 2670 | uint8_t* dst_argb, |
| 2671 | int dst_stride_argb, |
| 2672 | int32_t* dst_cumsum, |
| 2673 | int dst_stride32_cumsum, |
| 2674 | int width, |
| 2675 | int height, |
| 2676 | int radius) { |
| 2677 | int y; |
| 2678 | void (*ComputeCumulativeSumRow)(const uint8_t* row, int32_t* cumsum, |
| 2679 | const int32_t* previous_cumsum, int width) = |
| 2680 | ComputeCumulativeSumRow_C; |
| 2681 | void (*CumulativeSumToAverageRow)( |
| 2682 | const int32_t* topleft, const int32_t* botleft, int width, int area, |
| 2683 | uint8_t* dst, int count) = CumulativeSumToAverageRow_C; |
| 2684 | int32_t* cumsum_bot_row; |
| 2685 | int32_t* max_cumsum_bot_row; |
| 2686 | int32_t* cumsum_top_row; |
| 2687 |
| 2688 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 2689 | return -1; |
| 2690 | } |
| 2691 | if (height < 0) { |
| 2692 | height = -height; |
| 2693 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2694 | src_stride_argb = -src_stride_argb; |
| 2695 | } |
| 2696 | if (radius > height) { |
| 2697 | radius = height; |
| 2698 | } |
| 2699 | if (radius > (width / 2 - 1)) { |
| 2700 | radius = width / 2 - 1; |
| 2701 | } |
| 2702 | if (radius <= 0) { |
| 2703 | return -1; |
| 2704 | } |
| 2705 | #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2) |
| 2706 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2707 | ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2; |
| 2708 | CumulativeSumToAverageRow = CumulativeSumToAverageRow_SSE2; |
| 2709 | } |
| 2710 | #endif |
| 2711 | #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_MMI) |
| 2712 | if (TestCpuFlag(kCpuHasMMI)) { |
| 2713 | ComputeCumulativeSumRow = ComputeCumulativeSumRow_MMI; |
| 2714 | } |
| 2715 | #endif |
| 2716 | // Compute enough CumulativeSum for first row to be blurred. After this |
| 2717 | // one row of CumulativeSum is updated at a time. |
| 2718 | ARGBComputeCumulativeSum(src_argb, src_stride_argb, dst_cumsum, |
| 2719 | dst_stride32_cumsum, width, radius); |
| 2720 |
| 2721 | src_argb = src_argb + radius * src_stride_argb; |
| 2722 | cumsum_bot_row = &dst_cumsum[(radius - 1) * dst_stride32_cumsum]; |
| 2723 |
| 2724 | max_cumsum_bot_row = &dst_cumsum[(radius * 2 + 2) * dst_stride32_cumsum]; |
| 2725 | cumsum_top_row = &dst_cumsum[0]; |
| 2726 |
| 2727 | for (y = 0; y < height; ++y) { |
| 2728 | int top_y = ((y - radius - 1) >= 0) ? (y - radius - 1) : 0; |
| 2729 | int bot_y = ((y + radius) < height) ? (y + radius) : (height - 1); |
| 2730 | int area = radius * (bot_y - top_y); |
| 2731 | int boxwidth = radius * 4; |
| 2732 | int x; |
| 2733 | int n; |
| 2734 |
| 2735 | // Increment cumsum_top_row pointer with circular buffer wrap around. |
| 2736 | if (top_y) { |
| 2737 | cumsum_top_row += dst_stride32_cumsum; |
| 2738 | if (cumsum_top_row >= max_cumsum_bot_row) { |
| 2739 | cumsum_top_row = dst_cumsum; |
| 2740 | } |
| 2741 | } |
| 2742 | // Increment cumsum_bot_row pointer with circular buffer wrap around and |
| 2743 | // then fill in a row of CumulativeSum. |
| 2744 | if ((y + radius) < height) { |
| 2745 | const int32_t* prev_cumsum_bot_row = cumsum_bot_row; |
| 2746 | cumsum_bot_row += dst_stride32_cumsum; |
| 2747 | if (cumsum_bot_row >= max_cumsum_bot_row) { |
| 2748 | cumsum_bot_row = dst_cumsum; |
| 2749 | } |
| 2750 | ComputeCumulativeSumRow(src_argb, cumsum_bot_row, prev_cumsum_bot_row, |
| 2751 | width); |
| 2752 | src_argb += src_stride_argb; |
| 2753 | } |
| 2754 |
| 2755 | // Left clipped. |
| 2756 | for (x = 0; x < radius + 1; ++x) { |
| 2757 | CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, boxwidth, area, |
| 2758 | &dst_argb[x * 4], 1); |
| 2759 | area += (bot_y - top_y); |
| 2760 | boxwidth += 4; |
| 2761 | } |
| 2762 |
| 2763 | // Middle unclipped. |
| 2764 | n = (width - 1) - radius - x + 1; |
| 2765 | CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, boxwidth, area, |
| 2766 | &dst_argb[x * 4], n); |
| 2767 |
| 2768 | // Right clipped. |
| 2769 | for (x += n; x <= width - 1; ++x) { |
| 2770 | area -= (bot_y - top_y); |
| 2771 | boxwidth -= 4; |
| 2772 | CumulativeSumToAverageRow(cumsum_top_row + (x - radius - 1) * 4, |
| 2773 | cumsum_bot_row + (x - radius - 1) * 4, boxwidth, |
| 2774 | area, &dst_argb[x * 4], 1); |
| 2775 | } |
| 2776 | dst_argb += dst_stride_argb; |
| 2777 | } |
| 2778 | return 0; |
| 2779 | } |
| 2780 |
| 2781 | // Multiply ARGB image by a specified ARGB value. |
| 2782 | LIBYUV_API |
| 2783 | int ARGBShade(const uint8_t* src_argb, |
| 2784 | int src_stride_argb, |
| 2785 | uint8_t* dst_argb, |
| 2786 | int dst_stride_argb, |
| 2787 | int width, |
| 2788 | int height, |
| 2789 | uint32_t value) { |
| 2790 | int y; |
| 2791 | void (*ARGBShadeRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width, |
| 2792 | uint32_t value) = ARGBShadeRow_C; |
| 2793 | if (!src_argb || !dst_argb || width <= 0 || height == 0 || value == 0u) { |
| 2794 | return -1; |
| 2795 | } |
| 2796 | if (height < 0) { |
| 2797 | height = -height; |
| 2798 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2799 | src_stride_argb = -src_stride_argb; |
| 2800 | } |
| 2801 | // Coalesce rows. |
| 2802 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 2803 | width *= height; |
| 2804 | height = 1; |
| 2805 | src_stride_argb = dst_stride_argb = 0; |
| 2806 | } |
| 2807 | #if defined(HAS_ARGBSHADEROW_SSE2) |
| 2808 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) { |
| 2809 | ARGBShadeRow = ARGBShadeRow_SSE2; |
| 2810 | } |
| 2811 | #endif |
| 2812 | #if defined(HAS_ARGBSHADEROW_NEON) |
| 2813 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 2814 | ARGBShadeRow = ARGBShadeRow_NEON; |
| 2815 | } |
| 2816 | #endif |
| 2817 | #if defined(HAS_ARGBSHADEROW_MSA) |
| 2818 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 4)) { |
| 2819 | ARGBShadeRow = ARGBShadeRow_MSA; |
| 2820 | } |
| 2821 | #endif |
| 2822 | #if defined(HAS_ARGBSHADEROW_MMI) |
| 2823 | if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) { |
| 2824 | ARGBShadeRow = ARGBShadeRow_MMI; |
| 2825 | } |
| 2826 | #endif |
| 2827 |
| 2828 | for (y = 0; y < height; ++y) { |
| 2829 | ARGBShadeRow(src_argb, dst_argb, width, value); |
| 2830 | src_argb += src_stride_argb; |
| 2831 | dst_argb += dst_stride_argb; |
| 2832 | } |
| 2833 | return 0; |
| 2834 | } |
| 2835 |
| 2836 | // Interpolate 2 planes by specified amount (0 to 255). |
| 2837 | LIBYUV_API |
| 2838 | int InterpolatePlane(const uint8_t* src0, |
| 2839 | int src_stride0, |
| 2840 | const uint8_t* src1, |
| 2841 | int src_stride1, |
| 2842 | uint8_t* dst, |
| 2843 | int dst_stride, |
| 2844 | int width, |
| 2845 | int height, |
| 2846 | int interpolation) { |
| 2847 | int y; |
| 2848 | void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr, |
| 2849 | ptrdiff_t src_stride, int dst_width, |
| 2850 | int source_y_fraction) = InterpolateRow_C; |
| 2851 | if (!src0 || !src1 || !dst || width <= 0 || height == 0) { |
| 2852 | return -1; |
| 2853 | } |
| 2854 | // Negative height means invert the image. |
| 2855 | if (height < 0) { |
| 2856 | height = -height; |
| 2857 | dst = dst + (height - 1) * dst_stride; |
| 2858 | dst_stride = -dst_stride; |
| 2859 | } |
| 2860 | // Coalesce rows. |
| 2861 | if (src_stride0 == width && src_stride1 == width && dst_stride == width) { |
| 2862 | width *= height; |
| 2863 | height = 1; |
| 2864 | src_stride0 = src_stride1 = dst_stride = 0; |
| 2865 | } |
| 2866 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 2867 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2868 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 2869 | if (IS_ALIGNED(width, 16)) { |
| 2870 | InterpolateRow = InterpolateRow_SSSE3; |
| 2871 | } |
| 2872 | } |
| 2873 | #endif |
| 2874 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 2875 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2876 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 2877 | if (IS_ALIGNED(width, 32)) { |
| 2878 | InterpolateRow = InterpolateRow_AVX2; |
| 2879 | } |
| 2880 | } |
| 2881 | #endif |
| 2882 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 2883 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2884 | InterpolateRow = InterpolateRow_Any_NEON; |
| 2885 | if (IS_ALIGNED(width, 16)) { |
| 2886 | InterpolateRow = InterpolateRow_NEON; |
| 2887 | } |
| 2888 | } |
| 2889 | #endif |
| 2890 | #if defined(HAS_INTERPOLATEROW_MSA) |
| 2891 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2892 | InterpolateRow = InterpolateRow_Any_MSA; |
| 2893 | if (IS_ALIGNED(width, 32)) { |
| 2894 | InterpolateRow = InterpolateRow_MSA; |
| 2895 | } |
| 2896 | } |
| 2897 | #endif |
| 2898 | #if defined(HAS_INTERPOLATEROW_MMI) |
| 2899 | if (TestCpuFlag(kCpuHasMMI)) { |
| 2900 | InterpolateRow = InterpolateRow_Any_MMI; |
| 2901 | if (IS_ALIGNED(width, 8)) { |
| 2902 | InterpolateRow = InterpolateRow_MMI; |
| 2903 | } |
| 2904 | } |
| 2905 | #endif |
| 2906 |
| 2907 | for (y = 0; y < height; ++y) { |
| 2908 | InterpolateRow(dst, src0, src1 - src0, width, interpolation); |
| 2909 | src0 += src_stride0; |
| 2910 | src1 += src_stride1; |
| 2911 | dst += dst_stride; |
| 2912 | } |
| 2913 | return 0; |
| 2914 | } |
| 2915 |
| 2916 | // Interpolate 2 ARGB images by specified amount (0 to 255). |
| 2917 | LIBYUV_API |
| 2918 | int ARGBInterpolate(const uint8_t* src_argb0, |
| 2919 | int src_stride_argb0, |
| 2920 | const uint8_t* src_argb1, |
| 2921 | int src_stride_argb1, |
| 2922 | uint8_t* dst_argb, |
| 2923 | int dst_stride_argb, |
| 2924 | int width, |
| 2925 | int height, |
| 2926 | int interpolation) { |
| 2927 | return InterpolatePlane(src_argb0, src_stride_argb0, src_argb1, |
| 2928 | src_stride_argb1, dst_argb, dst_stride_argb, |
| 2929 | width * 4, height, interpolation); |
| 2930 | } |
| 2931 |
| 2932 | // Interpolate 2 YUV images by specified amount (0 to 255). |
| 2933 | LIBYUV_API |
| 2934 | int I420Interpolate(const uint8_t* src0_y, |
| 2935 | int src0_stride_y, |
| 2936 | const uint8_t* src0_u, |
| 2937 | int src0_stride_u, |
| 2938 | const uint8_t* src0_v, |
| 2939 | int src0_stride_v, |
| 2940 | const uint8_t* src1_y, |
| 2941 | int src1_stride_y, |
| 2942 | const uint8_t* src1_u, |
| 2943 | int src1_stride_u, |
| 2944 | const uint8_t* src1_v, |
| 2945 | int src1_stride_v, |
| 2946 | uint8_t* dst_y, |
| 2947 | int dst_stride_y, |
| 2948 | uint8_t* dst_u, |
| 2949 | int dst_stride_u, |
| 2950 | uint8_t* dst_v, |
| 2951 | int dst_stride_v, |
| 2952 | int width, |
| 2953 | int height, |
| 2954 | int interpolation) { |
| 2955 | int halfwidth = (width + 1) >> 1; |
| 2956 | int halfheight = (height + 1) >> 1; |
| 2957 | if (!src0_y || !src0_u || !src0_v || !src1_y || !src1_u || !src1_v || |
| 2958 | !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 2959 | return -1; |
| 2960 | } |
| 2961 | InterpolatePlane(src0_y, src0_stride_y, src1_y, src1_stride_y, dst_y, |
| 2962 | dst_stride_y, width, height, interpolation); |
| 2963 | InterpolatePlane(src0_u, src0_stride_u, src1_u, src1_stride_u, dst_u, |
| 2964 | dst_stride_u, halfwidth, halfheight, interpolation); |
| 2965 | InterpolatePlane(src0_v, src0_stride_v, src1_v, src1_stride_v, dst_v, |
| 2966 | dst_stride_v, halfwidth, halfheight, interpolation); |
| 2967 | return 0; |
| 2968 | } |
| 2969 |
| 2970 | // Shuffle ARGB channel order. e.g. BGRA to ARGB. |
| 2971 | LIBYUV_API |
| 2972 | int ARGBShuffle(const uint8_t* src_bgra, |
| 2973 | int src_stride_bgra, |
| 2974 | uint8_t* dst_argb, |
| 2975 | int dst_stride_argb, |
| 2976 | const uint8_t* shuffler, |
| 2977 | int width, |
| 2978 | int height) { |
| 2979 | int y; |
| 2980 | void (*ARGBShuffleRow)(const uint8_t* src_bgra, uint8_t* dst_argb, |
| 2981 | const uint8_t* shuffler, int width) = ARGBShuffleRow_C; |
| 2982 | if (!src_bgra || !dst_argb || width <= 0 || height == 0) { |
| 2983 | return -1; |
| 2984 | } |
| 2985 | // Negative height means invert the image. |
| 2986 | if (height < 0) { |
| 2987 | height = -height; |
| 2988 | src_bgra = src_bgra + (height - 1) * src_stride_bgra; |
| 2989 | src_stride_bgra = -src_stride_bgra; |
| 2990 | } |
| 2991 | // Coalesce rows. |
| 2992 | if (src_stride_bgra == width * 4 && dst_stride_argb == width * 4) { |
| 2993 | width *= height; |
| 2994 | height = 1; |
| 2995 | src_stride_bgra = dst_stride_argb = 0; |
| 2996 | } |
| 2997 | #if defined(HAS_ARGBSHUFFLEROW_SSSE3) |
| 2998 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2999 | ARGBShuffleRow = ARGBShuffleRow_Any_SSSE3; |
| 3000 | if (IS_ALIGNED(width, 8)) { |
| 3001 | ARGBShuffleRow = ARGBShuffleRow_SSSE3; |
| 3002 | } |
| 3003 | } |
| 3004 | #endif |
| 3005 | #if defined(HAS_ARGBSHUFFLEROW_AVX2) |
| 3006 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3007 | ARGBShuffleRow = ARGBShuffleRow_Any_AVX2; |
| 3008 | if (IS_ALIGNED(width, 16)) { |
| 3009 | ARGBShuffleRow = ARGBShuffleRow_AVX2; |
| 3010 | } |
| 3011 | } |
| 3012 | #endif |
| 3013 | #if defined(HAS_ARGBSHUFFLEROW_NEON) |
| 3014 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3015 | ARGBShuffleRow = ARGBShuffleRow_Any_NEON; |
| 3016 | if (IS_ALIGNED(width, 4)) { |
| 3017 | ARGBShuffleRow = ARGBShuffleRow_NEON; |
| 3018 | } |
| 3019 | } |
| 3020 | #endif |
| 3021 | #if defined(HAS_ARGBSHUFFLEROW_MSA) |
| 3022 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3023 | ARGBShuffleRow = ARGBShuffleRow_Any_MSA; |
| 3024 | if (IS_ALIGNED(width, 8)) { |
| 3025 | ARGBShuffleRow = ARGBShuffleRow_MSA; |
| 3026 | } |
| 3027 | } |
| 3028 | #endif |
| 3029 | #if defined(HAS_ARGBSHUFFLEROW_MMI) |
| 3030 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3031 | ARGBShuffleRow = ARGBShuffleRow_Any_MMI; |
| 3032 | if (IS_ALIGNED(width, 2)) { |
| 3033 | ARGBShuffleRow = ARGBShuffleRow_MMI; |
| 3034 | } |
| 3035 | } |
| 3036 | #endif |
| 3037 |
| 3038 | for (y = 0; y < height; ++y) { |
| 3039 | ARGBShuffleRow(src_bgra, dst_argb, shuffler, width); |
| 3040 | src_bgra += src_stride_bgra; |
| 3041 | dst_argb += dst_stride_argb; |
| 3042 | } |
| 3043 | return 0; |
| 3044 | } |
| 3045 |
| 3046 | // Sobel ARGB effect. |
| 3047 | static int ARGBSobelize(const uint8_t* src_argb, |
| 3048 | int src_stride_argb, |
| 3049 | uint8_t* dst_argb, |
| 3050 | int dst_stride_argb, |
| 3051 | int width, |
| 3052 | int height, |
| 3053 | void (*SobelRow)(const uint8_t* src_sobelx, |
| 3054 | const uint8_t* src_sobely, |
| 3055 | uint8_t* dst, |
| 3056 | int width)) { |
| 3057 | int y; |
| 3058 | void (*ARGBToYJRow)(const uint8_t* src_argb, uint8_t* dst_g, int width) = |
| 3059 | ARGBToYJRow_C; |
| 3060 | void (*SobelYRow)(const uint8_t* src_y0, const uint8_t* src_y1, |
| 3061 | uint8_t* dst_sobely, int width) = SobelYRow_C; |
| 3062 | void (*SobelXRow)(const uint8_t* src_y0, const uint8_t* src_y1, |
| 3063 | const uint8_t* src_y2, uint8_t* dst_sobely, int width) = |
| 3064 | SobelXRow_C; |
| 3065 | const int kEdge = 16; // Extra pixels at start of row for extrude/align. |
| 3066 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 3067 | return -1; |
| 3068 | } |
| 3069 | // Negative height means invert the image. |
| 3070 | if (height < 0) { |
| 3071 | height = -height; |
| 3072 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 3073 | src_stride_argb = -src_stride_argb; |
| 3074 | } |
| 3075 |
| 3076 | #if defined(HAS_ARGBTOYJROW_SSSE3) |
| 3077 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 3078 | ARGBToYJRow = ARGBToYJRow_Any_SSSE3; |
| 3079 | if (IS_ALIGNED(width, 16)) { |
| 3080 | ARGBToYJRow = ARGBToYJRow_SSSE3; |
| 3081 | } |
| 3082 | } |
| 3083 | #endif |
| 3084 | #if defined(HAS_ARGBTOYJROW_AVX2) |
| 3085 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3086 | ARGBToYJRow = ARGBToYJRow_Any_AVX2; |
| 3087 | if (IS_ALIGNED(width, 32)) { |
| 3088 | ARGBToYJRow = ARGBToYJRow_AVX2; |
| 3089 | } |
| 3090 | } |
| 3091 | #endif |
| 3092 | #if defined(HAS_ARGBTOYJROW_NEON) |
| 3093 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3094 | ARGBToYJRow = ARGBToYJRow_Any_NEON; |
| 3095 | if (IS_ALIGNED(width, 8)) { |
| 3096 | ARGBToYJRow = ARGBToYJRow_NEON; |
| 3097 | } |
| 3098 | } |
| 3099 | #endif |
| 3100 | #if defined(HAS_ARGBTOYJROW_MSA) |
| 3101 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3102 | ARGBToYJRow = ARGBToYJRow_Any_MSA; |
| 3103 | if (IS_ALIGNED(width, 16)) { |
| 3104 | ARGBToYJRow = ARGBToYJRow_MSA; |
| 3105 | } |
| 3106 | } |
| 3107 | #endif |
| 3108 | #if defined(HAS_ARGBTOYJROW_MMI) |
| 3109 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3110 | ARGBToYJRow = ARGBToYJRow_Any_MMI; |
| 3111 | if (IS_ALIGNED(width, 8)) { |
| 3112 | ARGBToYJRow = ARGBToYJRow_MMI; |
| 3113 | } |
| 3114 | } |
| 3115 | #endif |
| 3116 |
| 3117 | #if defined(HAS_SOBELYROW_SSE2) |
| 3118 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3119 | SobelYRow = SobelYRow_SSE2; |
| 3120 | } |
| 3121 | #endif |
| 3122 | #if defined(HAS_SOBELYROW_NEON) |
| 3123 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3124 | SobelYRow = SobelYRow_NEON; |
| 3125 | } |
| 3126 | #endif |
| 3127 | #if defined(HAS_SOBELYROW_MSA) |
| 3128 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3129 | SobelYRow = SobelYRow_MSA; |
| 3130 | } |
| 3131 | #endif |
| 3132 | #if defined(HAS_SOBELYROW_MMI) |
| 3133 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3134 | SobelYRow = SobelYRow_MMI; |
| 3135 | } |
| 3136 | #endif |
| 3137 | #if defined(HAS_SOBELXROW_SSE2) |
| 3138 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3139 | SobelXRow = SobelXRow_SSE2; |
| 3140 | } |
| 3141 | #endif |
| 3142 | #if defined(HAS_SOBELXROW_NEON) |
| 3143 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3144 | SobelXRow = SobelXRow_NEON; |
| 3145 | } |
| 3146 | #endif |
| 3147 | #if defined(HAS_SOBELXROW_MSA) |
| 3148 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3149 | SobelXRow = SobelXRow_MSA; |
| 3150 | } |
| 3151 | #endif |
| 3152 | #if defined(HAS_SOBELXROW_MMI) |
| 3153 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3154 | SobelXRow = SobelXRow_MMI; |
| 3155 | } |
| 3156 | #endif |
| 3157 | { |
| 3158 | // 3 rows with edges before/after. |
| 3159 | const int kRowSize = (width + kEdge + 31) & ~31; |
| 3160 | align_buffer_64(rows, kRowSize * 2 + (kEdge + kRowSize * 3 + kEdge)); |
| 3161 | uint8_t* row_sobelx = rows; |
| 3162 | uint8_t* row_sobely = rows + kRowSize; |
| 3163 | uint8_t* row_y = rows + kRowSize * 2; |
| 3164 |
| 3165 | // Convert first row. |
| 3166 | uint8_t* row_y0 = row_y + kEdge; |
| 3167 | uint8_t* row_y1 = row_y0 + kRowSize; |
| 3168 | uint8_t* row_y2 = row_y1 + kRowSize; |
| 3169 | ARGBToYJRow(src_argb, row_y0, width); |
| 3170 | row_y0[-1] = row_y0[0]; |
| 3171 | memset(row_y0 + width, row_y0[width - 1], 16); // Extrude 16 for valgrind. |
| 3172 | ARGBToYJRow(src_argb, row_y1, width); |
| 3173 | row_y1[-1] = row_y1[0]; |
| 3174 | memset(row_y1 + width, row_y1[width - 1], 16); |
| 3175 | memset(row_y2 + width, 0, 16); |
| 3176 |
| 3177 | for (y = 0; y < height; ++y) { |
| 3178 | // Convert next row of ARGB to G. |
| 3179 | if (y < (height - 1)) { |
| 3180 | src_argb += src_stride_argb; |
| 3181 | } |
| 3182 | ARGBToYJRow(src_argb, row_y2, width); |
| 3183 | row_y2[-1] = row_y2[0]; |
| 3184 | row_y2[width] = row_y2[width - 1]; |
| 3185 |
| 3186 | SobelXRow(row_y0 - 1, row_y1 - 1, row_y2 - 1, row_sobelx, width); |
| 3187 | SobelYRow(row_y0 - 1, row_y2 - 1, row_sobely, width); |
| 3188 | SobelRow(row_sobelx, row_sobely, dst_argb, width); |
| 3189 |
| 3190 | // Cycle thru circular queue of 3 row_y buffers. |
| 3191 | { |
| 3192 | uint8_t* row_yt = row_y0; |
| 3193 | row_y0 = row_y1; |
| 3194 | row_y1 = row_y2; |
| 3195 | row_y2 = row_yt; |
| 3196 | } |
| 3197 |
| 3198 | dst_argb += dst_stride_argb; |
| 3199 | } |
| 3200 | free_aligned_buffer_64(rows); |
| 3201 | } |
| 3202 | return 0; |
| 3203 | } |
| 3204 |
| 3205 | // Sobel ARGB effect. |
| 3206 | LIBYUV_API |
| 3207 | int ARGBSobel(const uint8_t* src_argb, |
| 3208 | int src_stride_argb, |
| 3209 | uint8_t* dst_argb, |
| 3210 | int dst_stride_argb, |
| 3211 | int width, |
| 3212 | int height) { |
| 3213 | void (*SobelRow)(const uint8_t* src_sobelx, const uint8_t* src_sobely, |
| 3214 | uint8_t* dst_argb, int width) = SobelRow_C; |
| 3215 | #if defined(HAS_SOBELROW_SSE2) |
| 3216 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3217 | SobelRow = SobelRow_Any_SSE2; |
| 3218 | if (IS_ALIGNED(width, 16)) { |
| 3219 | SobelRow = SobelRow_SSE2; |
| 3220 | } |
| 3221 | } |
| 3222 | #endif |
| 3223 | #if defined(HAS_SOBELROW_NEON) |
| 3224 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3225 | SobelRow = SobelRow_Any_NEON; |
| 3226 | if (IS_ALIGNED(width, 8)) { |
| 3227 | SobelRow = SobelRow_NEON; |
| 3228 | } |
| 3229 | } |
| 3230 | #endif |
| 3231 | #if defined(HAS_SOBELROW_MSA) |
| 3232 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3233 | SobelRow = SobelRow_Any_MSA; |
| 3234 | if (IS_ALIGNED(width, 16)) { |
| 3235 | SobelRow = SobelRow_MSA; |
| 3236 | } |
| 3237 | } |
| 3238 | #endif |
| 3239 | #if defined(HAS_SOBELROW_MMI) |
| 3240 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3241 | SobelRow = SobelRow_Any_MMI; |
| 3242 | if (IS_ALIGNED(width, 8)) { |
| 3243 | SobelRow = SobelRow_MMI; |
| 3244 | } |
| 3245 | } |
| 3246 | #endif |
| 3247 | return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb, |
| 3248 | width, height, SobelRow); |
| 3249 | } |
| 3250 |
| 3251 | // Sobel ARGB effect with planar output. |
| 3252 | LIBYUV_API |
| 3253 | int ARGBSobelToPlane(const uint8_t* src_argb, |
| 3254 | int src_stride_argb, |
| 3255 | uint8_t* dst_y, |
| 3256 | int dst_stride_y, |
| 3257 | int width, |
| 3258 | int height) { |
| 3259 | void (*SobelToPlaneRow)(const uint8_t* src_sobelx, const uint8_t* src_sobely, |
| 3260 | uint8_t* dst_, int width) = SobelToPlaneRow_C; |
| 3261 | #if defined(HAS_SOBELTOPLANEROW_SSE2) |
| 3262 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3263 | SobelToPlaneRow = SobelToPlaneRow_Any_SSE2; |
| 3264 | if (IS_ALIGNED(width, 16)) { |
| 3265 | SobelToPlaneRow = SobelToPlaneRow_SSE2; |
| 3266 | } |
| 3267 | } |
| 3268 | #endif |
| 3269 | #if defined(HAS_SOBELTOPLANEROW_NEON) |
| 3270 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3271 | SobelToPlaneRow = SobelToPlaneRow_Any_NEON; |
| 3272 | if (IS_ALIGNED(width, 16)) { |
| 3273 | SobelToPlaneRow = SobelToPlaneRow_NEON; |
| 3274 | } |
| 3275 | } |
| 3276 | #endif |
| 3277 | #if defined(HAS_SOBELTOPLANEROW_MSA) |
| 3278 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3279 | SobelToPlaneRow = SobelToPlaneRow_Any_MSA; |
| 3280 | if (IS_ALIGNED(width, 32)) { |
| 3281 | SobelToPlaneRow = SobelToPlaneRow_MSA; |
| 3282 | } |
| 3283 | } |
| 3284 | #endif |
| 3285 | #if defined(HAS_SOBELTOPLANEROW_MMI) |
| 3286 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3287 | SobelToPlaneRow = SobelToPlaneRow_Any_MMI; |
| 3288 | if (IS_ALIGNED(width, 8)) { |
| 3289 | SobelToPlaneRow = SobelToPlaneRow_MMI; |
| 3290 | } |
| 3291 | } |
| 3292 | #endif |
| 3293 | return ARGBSobelize(src_argb, src_stride_argb, dst_y, dst_stride_y, width, |
| 3294 | height, SobelToPlaneRow); |
| 3295 | } |
| 3296 |
| 3297 | // SobelXY ARGB effect. |
| 3298 | // Similar to Sobel, but also stores Sobel X in R and Sobel Y in B. G = Sobel. |
| 3299 | LIBYUV_API |
| 3300 | int ARGBSobelXY(const uint8_t* src_argb, |
| 3301 | int src_stride_argb, |
| 3302 | uint8_t* dst_argb, |
| 3303 | int dst_stride_argb, |
| 3304 | int width, |
| 3305 | int height) { |
| 3306 | void (*SobelXYRow)(const uint8_t* src_sobelx, const uint8_t* src_sobely, |
| 3307 | uint8_t* dst_argb, int width) = SobelXYRow_C; |
| 3308 | #if defined(HAS_SOBELXYROW_SSE2) |
| 3309 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3310 | SobelXYRow = SobelXYRow_Any_SSE2; |
| 3311 | if (IS_ALIGNED(width, 16)) { |
| 3312 | SobelXYRow = SobelXYRow_SSE2; |
| 3313 | } |
| 3314 | } |
| 3315 | #endif |
| 3316 | #if defined(HAS_SOBELXYROW_NEON) |
| 3317 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3318 | SobelXYRow = SobelXYRow_Any_NEON; |
| 3319 | if (IS_ALIGNED(width, 8)) { |
| 3320 | SobelXYRow = SobelXYRow_NEON; |
| 3321 | } |
| 3322 | } |
| 3323 | #endif |
| 3324 | #if defined(HAS_SOBELXYROW_MSA) |
| 3325 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3326 | SobelXYRow = SobelXYRow_Any_MSA; |
| 3327 | if (IS_ALIGNED(width, 16)) { |
| 3328 | SobelXYRow = SobelXYRow_MSA; |
| 3329 | } |
| 3330 | } |
| 3331 | #endif |
| 3332 | #if defined(HAS_SOBELXYROW_MMI) |
| 3333 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3334 | SobelXYRow = SobelXYRow_Any_MMI; |
| 3335 | if (IS_ALIGNED(width, 8)) { |
| 3336 | SobelXYRow = SobelXYRow_MMI; |
| 3337 | } |
| 3338 | } |
| 3339 | #endif |
| 3340 | return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb, |
| 3341 | width, height, SobelXYRow); |
| 3342 | } |
| 3343 |
| 3344 | // Apply a 4x4 polynomial to each ARGB pixel. |
| 3345 | LIBYUV_API |
| 3346 | int ARGBPolynomial(const uint8_t* src_argb, |
| 3347 | int src_stride_argb, |
| 3348 | uint8_t* dst_argb, |
| 3349 | int dst_stride_argb, |
| 3350 | const float* poly, |
| 3351 | int width, |
| 3352 | int height) { |
| 3353 | int y; |
| 3354 | void (*ARGBPolynomialRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 3355 | const float* poly, int width) = ARGBPolynomialRow_C; |
| 3356 | if (!src_argb || !dst_argb || !poly || width <= 0 || height == 0) { |
| 3357 | return -1; |
| 3358 | } |
| 3359 | // Negative height means invert the image. |
| 3360 | if (height < 0) { |
| 3361 | height = -height; |
| 3362 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 3363 | src_stride_argb = -src_stride_argb; |
| 3364 | } |
| 3365 | // Coalesce rows. |
| 3366 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 3367 | width *= height; |
| 3368 | height = 1; |
| 3369 | src_stride_argb = dst_stride_argb = 0; |
| 3370 | } |
| 3371 | #if defined(HAS_ARGBPOLYNOMIALROW_SSE2) |
| 3372 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 2)) { |
| 3373 | ARGBPolynomialRow = ARGBPolynomialRow_SSE2; |
| 3374 | } |
| 3375 | #endif |
| 3376 | #if defined(HAS_ARGBPOLYNOMIALROW_AVX2) |
| 3377 | if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasFMA3) && |
| 3378 | IS_ALIGNED(width, 2)) { |
| 3379 | ARGBPolynomialRow = ARGBPolynomialRow_AVX2; |
| 3380 | } |
| 3381 | #endif |
| 3382 |
| 3383 | for (y = 0; y < height; ++y) { |
| 3384 | ARGBPolynomialRow(src_argb, dst_argb, poly, width); |
| 3385 | src_argb += src_stride_argb; |
| 3386 | dst_argb += dst_stride_argb; |
| 3387 | } |
| 3388 | return 0; |
| 3389 | } |
| 3390 |
| 3391 | // Convert plane of 16 bit shorts to half floats. |
| 3392 | // Source values are multiplied by scale before storing as half float. |
| 3393 | LIBYUV_API |
| 3394 | int HalfFloatPlane(const uint16_t* src_y, |
| 3395 | int src_stride_y, |
| 3396 | uint16_t* dst_y, |
| 3397 | int dst_stride_y, |
| 3398 | float scale, |
| 3399 | int width, |
| 3400 | int height) { |
| 3401 | int y; |
| 3402 | void (*HalfFloatRow)(const uint16_t* src, uint16_t* dst, float scale, |
| 3403 | int width) = HalfFloatRow_C; |
| 3404 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 3405 | return -1; |
| 3406 | } |
| 3407 | src_stride_y >>= 1; |
| 3408 | dst_stride_y >>= 1; |
| 3409 | // Negative height means invert the image. |
| 3410 | if (height < 0) { |
| 3411 | height = -height; |
| 3412 | src_y = src_y + (height - 1) * src_stride_y; |
| 3413 | src_stride_y = -src_stride_y; |
| 3414 | } |
| 3415 | // Coalesce rows. |
| 3416 | if (src_stride_y == width && dst_stride_y == width) { |
| 3417 | width *= height; |
| 3418 | height = 1; |
| 3419 | src_stride_y = dst_stride_y = 0; |
| 3420 | } |
| 3421 | #if defined(HAS_HALFFLOATROW_SSE2) |
| 3422 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3423 | HalfFloatRow = HalfFloatRow_Any_SSE2; |
| 3424 | if (IS_ALIGNED(width, 8)) { |
| 3425 | HalfFloatRow = HalfFloatRow_SSE2; |
| 3426 | } |
| 3427 | } |
| 3428 | #endif |
| 3429 | #if defined(HAS_HALFFLOATROW_AVX2) |
| 3430 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3431 | HalfFloatRow = HalfFloatRow_Any_AVX2; |
| 3432 | if (IS_ALIGNED(width, 16)) { |
| 3433 | HalfFloatRow = HalfFloatRow_AVX2; |
| 3434 | } |
| 3435 | } |
| 3436 | #endif |
| 3437 | #if defined(HAS_HALFFLOATROW_F16C) |
| 3438 | if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasF16C)) { |
| 3439 | HalfFloatRow = |
| 3440 | (scale == 1.0f) ? HalfFloat1Row_Any_F16C : HalfFloatRow_Any_F16C; |
| 3441 | if (IS_ALIGNED(width, 16)) { |
| 3442 | HalfFloatRow = (scale == 1.0f) ? HalfFloat1Row_F16C : HalfFloatRow_F16C; |
| 3443 | } |
| 3444 | } |
| 3445 | #endif |
| 3446 | #if defined(HAS_HALFFLOATROW_NEON) |
| 3447 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3448 | HalfFloatRow = |
| 3449 | (scale == 1.0f) ? HalfFloat1Row_Any_NEON : HalfFloatRow_Any_NEON; |
| 3450 | if (IS_ALIGNED(width, 8)) { |
| 3451 | HalfFloatRow = (scale == 1.0f) ? HalfFloat1Row_NEON : HalfFloatRow_NEON; |
| 3452 | } |
| 3453 | } |
| 3454 | #endif |
| 3455 | #if defined(HAS_HALFFLOATROW_MSA) |
| 3456 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3457 | HalfFloatRow = HalfFloatRow_Any_MSA; |
| 3458 | if (IS_ALIGNED(width, 32)) { |
| 3459 | HalfFloatRow = HalfFloatRow_MSA; |
| 3460 | } |
| 3461 | } |
| 3462 | #endif |
| 3463 |
| 3464 | for (y = 0; y < height; ++y) { |
| 3465 | HalfFloatRow(src_y, dst_y, scale, width); |
| 3466 | src_y += src_stride_y; |
| 3467 | dst_y += dst_stride_y; |
| 3468 | } |
| 3469 | return 0; |
| 3470 | } |
| 3471 |
| 3472 | // Convert a buffer of bytes to floats, scale the values and store as floats. |
| 3473 | LIBYUV_API |
| 3474 | int ByteToFloat(const uint8_t* src_y, float* dst_y, float scale, int width) { |
| 3475 | void (*ByteToFloatRow)(const uint8_t* src, float* dst, float scale, |
| 3476 | int width) = ByteToFloatRow_C; |
| 3477 | if (!src_y || !dst_y || width <= 0) { |
| 3478 | return -1; |
| 3479 | } |
| 3480 | #if defined(HAS_BYTETOFLOATROW_NEON) |
| 3481 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3482 | ByteToFloatRow = ByteToFloatRow_Any_NEON; |
| 3483 | if (IS_ALIGNED(width, 8)) { |
| 3484 | ByteToFloatRow = ByteToFloatRow_NEON; |
| 3485 | } |
| 3486 | } |
| 3487 | #endif |
| 3488 |
| 3489 | ByteToFloatRow(src_y, dst_y, scale, width); |
| 3490 | return 0; |
| 3491 | } |
| 3492 |
| 3493 | // Apply a lumacolortable to each ARGB pixel. |
| 3494 | LIBYUV_API |
| 3495 | int ARGBLumaColorTable(const uint8_t* src_argb, |
| 3496 | int src_stride_argb, |
| 3497 | uint8_t* dst_argb, |
| 3498 | int dst_stride_argb, |
| 3499 | const uint8_t* luma, |
| 3500 | int width, |
| 3501 | int height) { |
| 3502 | int y; |
| 3503 | void (*ARGBLumaColorTableRow)( |
| 3504 | const uint8_t* src_argb, uint8_t* dst_argb, int width, |
| 3505 | const uint8_t* luma, const uint32_t lumacoeff) = ARGBLumaColorTableRow_C; |
| 3506 | if (!src_argb || !dst_argb || !luma || width <= 0 || height == 0) { |
| 3507 | return -1; |
| 3508 | } |
| 3509 | // Negative height means invert the image. |
| 3510 | if (height < 0) { |
| 3511 | height = -height; |
| 3512 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 3513 | src_stride_argb = -src_stride_argb; |
| 3514 | } |
| 3515 | // Coalesce rows. |
| 3516 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 3517 | width *= height; |
| 3518 | height = 1; |
| 3519 | src_stride_argb = dst_stride_argb = 0; |
| 3520 | } |
| 3521 | #if defined(HAS_ARGBLUMACOLORTABLEROW_SSSE3) |
| 3522 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4)) { |
| 3523 | ARGBLumaColorTableRow = ARGBLumaColorTableRow_SSSE3; |
| 3524 | } |
| 3525 | #endif |
| 3526 |
| 3527 | for (y = 0; y < height; ++y) { |
| 3528 | ARGBLumaColorTableRow(src_argb, dst_argb, width, luma, 0x00264b0f); |
| 3529 | src_argb += src_stride_argb; |
| 3530 | dst_argb += dst_stride_argb; |
| 3531 | } |
| 3532 | return 0; |
| 3533 | } |
| 3534 |
| 3535 | // Copy Alpha from one ARGB image to another. |
| 3536 | LIBYUV_API |
| 3537 | int ARGBCopyAlpha(const uint8_t* src_argb, |
| 3538 | int src_stride_argb, |
| 3539 | uint8_t* dst_argb, |
| 3540 | int dst_stride_argb, |
| 3541 | int width, |
| 3542 | int height) { |
| 3543 | int y; |
| 3544 | void (*ARGBCopyAlphaRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 3545 | int width) = ARGBCopyAlphaRow_C; |
| 3546 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 3547 | return -1; |
| 3548 | } |
| 3549 | // Negative height means invert the image. |
| 3550 | if (height < 0) { |
| 3551 | height = -height; |
| 3552 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 3553 | src_stride_argb = -src_stride_argb; |
| 3554 | } |
| 3555 | // Coalesce rows. |
| 3556 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 3557 | width *= height; |
| 3558 | height = 1; |
| 3559 | src_stride_argb = dst_stride_argb = 0; |
| 3560 | } |
| 3561 | #if defined(HAS_ARGBCOPYALPHAROW_SSE2) |
| 3562 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3563 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_SSE2; |
| 3564 | if (IS_ALIGNED(width, 8)) { |
| 3565 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_SSE2; |
| 3566 | } |
| 3567 | } |
| 3568 | #endif |
| 3569 | #if defined(HAS_ARGBCOPYALPHAROW_AVX2) |
| 3570 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3571 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_AVX2; |
| 3572 | if (IS_ALIGNED(width, 16)) { |
| 3573 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_AVX2; |
| 3574 | } |
| 3575 | } |
| 3576 | #endif |
| 3577 | #if defined(HAS_ARGBCOPYALPHAROW_MMI) |
| 3578 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3579 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_MMI; |
| 3580 | if (IS_ALIGNED(width, 2)) { |
| 3581 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_MMI; |
| 3582 | } |
| 3583 | } |
| 3584 | #endif |
| 3585 |
| 3586 | for (y = 0; y < height; ++y) { |
| 3587 | ARGBCopyAlphaRow(src_argb, dst_argb, width); |
| 3588 | src_argb += src_stride_argb; |
| 3589 | dst_argb += dst_stride_argb; |
| 3590 | } |
| 3591 | return 0; |
| 3592 | } |
| 3593 |
| 3594 | // Extract just the alpha channel from ARGB. |
| 3595 | LIBYUV_API |
| 3596 | int ARGBExtractAlpha(const uint8_t* src_argb, |
| 3597 | int src_stride_argb, |
| 3598 | uint8_t* dst_a, |
| 3599 | int dst_stride_a, |
| 3600 | int width, |
| 3601 | int height) { |
| 3602 | if (!src_argb || !dst_a || width <= 0 || height == 0) { |
| 3603 | return -1; |
| 3604 | } |
| 3605 | // Negative height means invert the image. |
| 3606 | if (height < 0) { |
| 3607 | height = -height; |
| 3608 | src_argb += (height - 1) * src_stride_argb; |
| 3609 | src_stride_argb = -src_stride_argb; |
| 3610 | } |
| 3611 | // Coalesce rows. |
| 3612 | if (src_stride_argb == width * 4 && dst_stride_a == width) { |
| 3613 | width *= height; |
| 3614 | height = 1; |
| 3615 | src_stride_argb = dst_stride_a = 0; |
| 3616 | } |
| 3617 | void (*ARGBExtractAlphaRow)(const uint8_t* src_argb, uint8_t* dst_a, |
| 3618 | int width) = ARGBExtractAlphaRow_C; |
| 3619 | #if defined(HAS_ARGBEXTRACTALPHAROW_SSE2) |
| 3620 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3621 | ARGBExtractAlphaRow = IS_ALIGNED(width, 8) ? ARGBExtractAlphaRow_SSE2 |
| 3622 | : ARGBExtractAlphaRow_Any_SSE2; |
| 3623 | } |
| 3624 | #endif |
| 3625 | #if defined(HAS_ARGBEXTRACTALPHAROW_AVX2) |
| 3626 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3627 | ARGBExtractAlphaRow = IS_ALIGNED(width, 32) ? ARGBExtractAlphaRow_AVX2 |
| 3628 | : ARGBExtractAlphaRow_Any_AVX2; |
| 3629 | } |
| 3630 | #endif |
| 3631 | #if defined(HAS_ARGBEXTRACTALPHAROW_NEON) |
| 3632 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3633 | ARGBExtractAlphaRow = IS_ALIGNED(width, 16) ? ARGBExtractAlphaRow_NEON |
| 3634 | : ARGBExtractAlphaRow_Any_NEON; |
| 3635 | } |
| 3636 | #endif |
| 3637 | #if defined(HAS_ARGBEXTRACTALPHAROW_MSA) |
| 3638 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3639 | ARGBExtractAlphaRow = IS_ALIGNED(width, 16) ? ARGBExtractAlphaRow_MSA |
| 3640 | : ARGBExtractAlphaRow_Any_MSA; |
| 3641 | } |
| 3642 | #endif |
| 3643 | #if defined(HAS_ARGBEXTRACTALPHAROW_MMI) |
| 3644 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3645 | ARGBExtractAlphaRow = IS_ALIGNED(width, 8) ? ARGBExtractAlphaRow_MMI |
| 3646 | : ARGBExtractAlphaRow_Any_MMI; |
| 3647 | } |
| 3648 | #endif |
| 3649 |
| 3650 | for (int y = 0; y < height; ++y) { |
| 3651 | ARGBExtractAlphaRow(src_argb, dst_a, width); |
| 3652 | src_argb += src_stride_argb; |
| 3653 | dst_a += dst_stride_a; |
| 3654 | } |
| 3655 | return 0; |
| 3656 | } |
| 3657 |
| 3658 | // Copy a planar Y channel to the alpha channel of a destination ARGB image. |
| 3659 | LIBYUV_API |
| 3660 | int ARGBCopyYToAlpha(const uint8_t* src_y, |
| 3661 | int src_stride_y, |
| 3662 | uint8_t* dst_argb, |
| 3663 | int dst_stride_argb, |
| 3664 | int width, |
| 3665 | int height) { |
| 3666 | int y; |
| 3667 | void (*ARGBCopyYToAlphaRow)(const uint8_t* src_y, uint8_t* dst_argb, |
| 3668 | int width) = ARGBCopyYToAlphaRow_C; |
| 3669 | if (!src_y || !dst_argb || width <= 0 || height == 0) { |
| 3670 | return -1; |
| 3671 | } |
| 3672 | // Negative height means invert the image. |
| 3673 | if (height < 0) { |
| 3674 | height = -height; |
| 3675 | src_y = src_y + (height - 1) * src_stride_y; |
| 3676 | src_stride_y = -src_stride_y; |
| 3677 | } |
| 3678 | // Coalesce rows. |
| 3679 | if (src_stride_y == width && dst_stride_argb == width * 4) { |
| 3680 | width *= height; |
| 3681 | height = 1; |
| 3682 | src_stride_y = dst_stride_argb = 0; |
| 3683 | } |
| 3684 | #if defined(HAS_ARGBCOPYYTOALPHAROW_SSE2) |
| 3685 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3686 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_SSE2; |
| 3687 | if (IS_ALIGNED(width, 8)) { |
| 3688 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_SSE2; |
| 3689 | } |
| 3690 | } |
| 3691 | #endif |
| 3692 | #if defined(HAS_ARGBCOPYYTOALPHAROW_AVX2) |
| 3693 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3694 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_AVX2; |
| 3695 | if (IS_ALIGNED(width, 16)) { |
| 3696 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_AVX2; |
| 3697 | } |
| 3698 | } |
| 3699 | #endif |
| 3700 | #if defined(HAS_ARGBCOPYYTOALPHAROW_MMI) |
| 3701 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3702 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_MMI; |
| 3703 | if (IS_ALIGNED(width, 8)) { |
| 3704 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_MMI; |
| 3705 | } |
| 3706 | } |
| 3707 | #endif |
| 3708 |
| 3709 | for (y = 0; y < height; ++y) { |
| 3710 | ARGBCopyYToAlphaRow(src_y, dst_argb, width); |
| 3711 | src_y += src_stride_y; |
| 3712 | dst_argb += dst_stride_argb; |
| 3713 | } |
| 3714 | return 0; |
| 3715 | } |
| 3716 |
| 3717 | // TODO(fbarchard): Consider if width is even Y channel can be split |
| 3718 | // directly. A SplitUVRow_Odd function could copy the remaining chroma. |
| 3719 |
| 3720 | LIBYUV_API |
| 3721 | int YUY2ToNV12(const uint8_t* src_yuy2, |
| 3722 | int src_stride_yuy2, |
| 3723 | uint8_t* dst_y, |
| 3724 | int dst_stride_y, |
| 3725 | uint8_t* dst_uv, |
| 3726 | int dst_stride_uv, |
| 3727 | int width, |
| 3728 | int height) { |
| 3729 | int y; |
| 3730 | int halfwidth = (width + 1) >> 1; |
| 3731 | void (*SplitUVRow)(const uint8_t* src_uv, uint8_t* dst_u, uint8_t* dst_v, |
| 3732 | int width) = SplitUVRow_C; |
| 3733 | void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr, |
| 3734 | ptrdiff_t src_stride, int dst_width, |
| 3735 | int source_y_fraction) = InterpolateRow_C; |
| 3736 | if (!src_yuy2 || !dst_y || !dst_uv || width <= 0 || height == 0) { |
| 3737 | return -1; |
| 3738 | } |
| 3739 | // Negative height means invert the image. |
| 3740 | if (height < 0) { |
| 3741 | height = -height; |
| 3742 | src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2; |
| 3743 | src_stride_yuy2 = -src_stride_yuy2; |
| 3744 | } |
| 3745 | #if defined(HAS_SPLITUVROW_SSE2) |
| 3746 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3747 | SplitUVRow = SplitUVRow_Any_SSE2; |
| 3748 | if (IS_ALIGNED(width, 16)) { |
| 3749 | SplitUVRow = SplitUVRow_SSE2; |
| 3750 | } |
| 3751 | } |
| 3752 | #endif |
| 3753 | #if defined(HAS_SPLITUVROW_AVX2) |
| 3754 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3755 | SplitUVRow = SplitUVRow_Any_AVX2; |
| 3756 | if (IS_ALIGNED(width, 32)) { |
| 3757 | SplitUVRow = SplitUVRow_AVX2; |
| 3758 | } |
| 3759 | } |
| 3760 | #endif |
| 3761 | #if defined(HAS_SPLITUVROW_NEON) |
| 3762 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3763 | SplitUVRow = SplitUVRow_Any_NEON; |
| 3764 | if (IS_ALIGNED(width, 16)) { |
| 3765 | SplitUVRow = SplitUVRow_NEON; |
| 3766 | } |
| 3767 | } |
| 3768 | #endif |
| 3769 | #if defined(HAS_SPLITUVROW_MSA) |
| 3770 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3771 | SplitUVRow = SplitUVRow_Any_MSA; |
| 3772 | if (IS_ALIGNED(width, 32)) { |
| 3773 | SplitUVRow = SplitUVRow_MSA; |
| 3774 | } |
| 3775 | } |
| 3776 | #endif |
| 3777 | #if defined(HAS_SPLITUVROW_MMI) |
| 3778 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3779 | SplitUVRow = SplitUVRow_Any_MMI; |
| 3780 | if (IS_ALIGNED(width, 8)) { |
| 3781 | SplitUVRow = SplitUVRow_MMI; |
| 3782 | } |
| 3783 | } |
| 3784 | #endif |
| 3785 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 3786 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 3787 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 3788 | if (IS_ALIGNED(width, 16)) { |
| 3789 | InterpolateRow = InterpolateRow_SSSE3; |
| 3790 | } |
| 3791 | } |
| 3792 | #endif |
| 3793 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 3794 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3795 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 3796 | if (IS_ALIGNED(width, 32)) { |
| 3797 | InterpolateRow = InterpolateRow_AVX2; |
| 3798 | } |
| 3799 | } |
| 3800 | #endif |
| 3801 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 3802 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3803 | InterpolateRow = InterpolateRow_Any_NEON; |
| 3804 | if (IS_ALIGNED(width, 16)) { |
| 3805 | InterpolateRow = InterpolateRow_NEON; |
| 3806 | } |
| 3807 | } |
| 3808 | #endif |
| 3809 | #if defined(HAS_INTERPOLATEROW_MSA) |
| 3810 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3811 | InterpolateRow = InterpolateRow_Any_MSA; |
| 3812 | if (IS_ALIGNED(width, 32)) { |
| 3813 | InterpolateRow = InterpolateRow_MSA; |
| 3814 | } |
| 3815 | } |
| 3816 | #endif |
| 3817 | #if defined(HAS_INTERPOLATEROW_MMI) |
| 3818 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3819 | InterpolateRow = InterpolateRow_Any_MMI; |
| 3820 | if (IS_ALIGNED(width, 8)) { |
| 3821 | InterpolateRow = InterpolateRow_MMI; |
| 3822 | } |
| 3823 | } |
| 3824 | #endif |
| 3825 |
| 3826 | { |
| 3827 | int awidth = halfwidth * 2; |
| 3828 | // row of y and 2 rows of uv |
| 3829 | align_buffer_64(rows, awidth * 3); |
| 3830 |
| 3831 | for (y = 0; y < height - 1; y += 2) { |
| 3832 | // Split Y from UV. |
| 3833 | SplitUVRow(src_yuy2, rows, rows + awidth, awidth); |
| 3834 | memcpy(dst_y, rows, width); |
| 3835 | SplitUVRow(src_yuy2 + src_stride_yuy2, rows, rows + awidth * 2, awidth); |
| 3836 | memcpy(dst_y + dst_stride_y, rows, width); |
| 3837 | InterpolateRow(dst_uv, rows + awidth, awidth, awidth, 128); |
| 3838 | src_yuy2 += src_stride_yuy2 * 2; |
| 3839 | dst_y += dst_stride_y * 2; |
| 3840 | dst_uv += dst_stride_uv; |
| 3841 | } |
| 3842 | if (height & 1) { |
| 3843 | // Split Y from UV. |
| 3844 | SplitUVRow(src_yuy2, rows, dst_uv, awidth); |
| 3845 | memcpy(dst_y, rows, width); |
| 3846 | } |
| 3847 | free_aligned_buffer_64(rows); |
| 3848 | } |
| 3849 | return 0; |
| 3850 | } |
| 3851 |
| 3852 | LIBYUV_API |
| 3853 | int UYVYToNV12(const uint8_t* src_uyvy, |
| 3854 | int src_stride_uyvy, |
| 3855 | uint8_t* dst_y, |
| 3856 | int dst_stride_y, |
| 3857 | uint8_t* dst_uv, |
| 3858 | int dst_stride_uv, |
| 3859 | int width, |
| 3860 | int height) { |
| 3861 | int y; |
| 3862 | int halfwidth = (width + 1) >> 1; |
| 3863 | void (*SplitUVRow)(const uint8_t* src_uv, uint8_t* dst_u, uint8_t* dst_v, |
| 3864 | int width) = SplitUVRow_C; |
| 3865 | void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr, |
| 3866 | ptrdiff_t src_stride, int dst_width, |
| 3867 | int source_y_fraction) = InterpolateRow_C; |
| 3868 | if (!src_uyvy || !dst_y || !dst_uv || width <= 0 || height == 0) { |
| 3869 | return -1; |
| 3870 | } |
| 3871 | // Negative height means invert the image. |
| 3872 | if (height < 0) { |
| 3873 | height = -height; |
| 3874 | src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy; |
| 3875 | src_stride_uyvy = -src_stride_uyvy; |
| 3876 | } |
| 3877 | #if defined(HAS_SPLITUVROW_SSE2) |
| 3878 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3879 | SplitUVRow = SplitUVRow_Any_SSE2; |
| 3880 | if (IS_ALIGNED(width, 16)) { |
| 3881 | SplitUVRow = SplitUVRow_SSE2; |
| 3882 | } |
| 3883 | } |
| 3884 | #endif |
| 3885 | #if defined(HAS_SPLITUVROW_AVX2) |
| 3886 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3887 | SplitUVRow = SplitUVRow_Any_AVX2; |
| 3888 | if (IS_ALIGNED(width, 32)) { |
| 3889 | SplitUVRow = SplitUVRow_AVX2; |
| 3890 | } |
| 3891 | } |
| 3892 | #endif |
| 3893 | #if defined(HAS_SPLITUVROW_NEON) |
| 3894 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3895 | SplitUVRow = SplitUVRow_Any_NEON; |
| 3896 | if (IS_ALIGNED(width, 16)) { |
| 3897 | SplitUVRow = SplitUVRow_NEON; |
| 3898 | } |
| 3899 | } |
| 3900 | #endif |
| 3901 | #if defined(HAS_SPLITUVROW_MSA) |
| 3902 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3903 | SplitUVRow = SplitUVRow_Any_MSA; |
| 3904 | if (IS_ALIGNED(width, 32)) { |
| 3905 | SplitUVRow = SplitUVRow_MSA; |
| 3906 | } |
| 3907 | } |
| 3908 | #endif |
| 3909 | #if defined(HAS_SPLITUVROW_MMI) |
| 3910 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3911 | SplitUVRow = SplitUVRow_Any_MMI; |
| 3912 | if (IS_ALIGNED(width, 8)) { |
| 3913 | SplitUVRow = SplitUVRow_MMI; |
| 3914 | } |
| 3915 | } |
| 3916 | #endif |
| 3917 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 3918 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 3919 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 3920 | if (IS_ALIGNED(width, 16)) { |
| 3921 | InterpolateRow = InterpolateRow_SSSE3; |
| 3922 | } |
| 3923 | } |
| 3924 | #endif |
| 3925 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 3926 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3927 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 3928 | if (IS_ALIGNED(width, 32)) { |
| 3929 | InterpolateRow = InterpolateRow_AVX2; |
| 3930 | } |
| 3931 | } |
| 3932 | #endif |
| 3933 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 3934 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3935 | InterpolateRow = InterpolateRow_Any_NEON; |
| 3936 | if (IS_ALIGNED(width, 16)) { |
| 3937 | InterpolateRow = InterpolateRow_NEON; |
| 3938 | } |
| 3939 | } |
| 3940 | #endif |
| 3941 | #if defined(HAS_INTERPOLATEROW_MSA) |
| 3942 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3943 | InterpolateRow = InterpolateRow_Any_MSA; |
| 3944 | if (IS_ALIGNED(width, 32)) { |
| 3945 | InterpolateRow = InterpolateRow_MSA; |
| 3946 | } |
| 3947 | } |
| 3948 | #endif |
| 3949 | #if defined(HAS_INTERPOLATEROW_MMI) |
| 3950 | if (TestCpuFlag(kCpuHasMMI)) { |
| 3951 | InterpolateRow = InterpolateRow_Any_MMI; |
| 3952 | if (IS_ALIGNED(width, 8)) { |
| 3953 | InterpolateRow = InterpolateRow_MMI; |
| 3954 | } |
| 3955 | } |
| 3956 | #endif |
| 3957 |
| 3958 | { |
| 3959 | int awidth = halfwidth * 2; |
| 3960 | // row of y and 2 rows of uv |
| 3961 | align_buffer_64(rows, awidth * 3); |
| 3962 |
| 3963 | for (y = 0; y < height - 1; y += 2) { |
| 3964 | // Split Y from UV. |
| 3965 | SplitUVRow(src_uyvy, rows + awidth, rows, awidth); |
| 3966 | memcpy(dst_y, rows, width); |
| 3967 | SplitUVRow(src_uyvy + src_stride_uyvy, rows + awidth * 2, rows, awidth); |
| 3968 | memcpy(dst_y + dst_stride_y, rows, width); |
| 3969 | InterpolateRow(dst_uv, rows + awidth, awidth, awidth, 128); |
| 3970 | src_uyvy += src_stride_uyvy * 2; |
| 3971 | dst_y += dst_stride_y * 2; |
| 3972 | dst_uv += dst_stride_uv; |
| 3973 | } |
| 3974 | if (height & 1) { |
| 3975 | // Split Y from UV. |
| 3976 | SplitUVRow(src_uyvy, dst_uv, rows, awidth); |
| 3977 | memcpy(dst_y, rows, width); |
| 3978 | } |
| 3979 | free_aligned_buffer_64(rows); |
| 3980 | } |
| 3981 | return 0; |
| 3982 | } |
| 3983 |
| 3984 | #ifdef __cplusplus |
| 3985 | } // extern "C" |
| 3986 | } // namespace libyuv |
| 3987 | #endif |
| 3988 |