| 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 <assert.h> |
| 14 | #include <string.h> // for memset() |
| 15 |
| 16 | #include "libyuv/cpu_id.h" |
| 17 | #ifdef HAVE_JPEG |
| 18 | #include "libyuv/mjpeg_decoder.h" |
| 19 | #endif |
| 20 | #include "libyuv/row.h" |
| 21 | #include "libyuv/scale_row.h" // for ScaleRowDown2 |
| 22 |
| 23 | #ifdef __cplusplus |
| 24 | namespace libyuv { |
| 25 | extern "C" { |
| 26 | #endif |
| 27 |
| 28 | // Copy a plane of data |
| 29 | LIBYUV_API |
| 30 | void CopyPlane(const uint8_t* src_y, |
| 31 | int src_stride_y, |
| 32 | uint8_t* dst_y, |
| 33 | int dst_stride_y, |
| 34 | int width, |
| 35 | int height) { |
| 36 | int y; |
| 37 | void (*CopyRow)(const uint8_t* src, uint8_t* dst, int width) = CopyRow_C; |
| 38 | if (width <= 0 || height == 0) { |
| 39 | return; |
| 40 | } |
| 41 | // Negative height means invert the image. |
| 42 | if (height < 0) { |
| 43 | height = -height; |
| 44 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 45 | dst_stride_y = -dst_stride_y; |
| 46 | } |
| 47 | // Coalesce rows. |
| 48 | if (src_stride_y == width && dst_stride_y == width) { |
| 49 | width *= height; |
| 50 | height = 1; |
| 51 | src_stride_y = dst_stride_y = 0; |
| 52 | } |
| 53 | // Nothing to do. |
| 54 | if (src_y == dst_y && src_stride_y == dst_stride_y) { |
| 55 | return; |
| 56 | } |
| 57 |
| 58 | #if defined(HAS_COPYROW_SSE2) |
| 59 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 60 | CopyRow = IS_ALIGNED(width, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2; |
| 61 | } |
| 62 | #endif |
| 63 | #if defined(HAS_COPYROW_AVX) |
| 64 | if (TestCpuFlag(kCpuHasAVX)) { |
| 65 | CopyRow = IS_ALIGNED(width, 64) ? CopyRow_AVX : CopyRow_Any_AVX; |
| 66 | } |
| 67 | #endif |
| 68 | #if defined(HAS_COPYROW_ERMS) |
| 69 | if (TestCpuFlag(kCpuHasERMS)) { |
| 70 | CopyRow = CopyRow_ERMS; |
| 71 | } |
| 72 | #endif |
| 73 | #if defined(HAS_COPYROW_NEON) |
| 74 | if (TestCpuFlag(kCpuHasNEON)) { |
| 75 | CopyRow = IS_ALIGNED(width, 32) ? CopyRow_NEON : CopyRow_Any_NEON; |
| 76 | } |
| 77 | #endif |
| 78 |
| 79 | // Copy plane |
| 80 | for (y = 0; y < height; ++y) { |
| 81 | CopyRow(src_y, dst_y, width); |
| 82 | src_y += src_stride_y; |
| 83 | dst_y += dst_stride_y; |
| 84 | } |
| 85 | } |
| 86 |
| 87 | LIBYUV_API |
| 88 | void CopyPlane_16(const uint16_t* src_y, |
| 89 | int src_stride_y, |
| 90 | uint16_t* dst_y, |
| 91 | int dst_stride_y, |
| 92 | int width, |
| 93 | int height) { |
| 94 | CopyPlane((const uint8_t*)src_y, src_stride_y * 2, (uint8_t*)dst_y, |
| 95 | dst_stride_y * 2, width * 2, height); |
| 96 | } |
| 97 |
| 98 | // Convert a plane of 16 bit data to 8 bit |
| 99 | LIBYUV_API |
| 100 | void Convert16To8Plane(const uint16_t* src_y, |
| 101 | int src_stride_y, |
| 102 | uint8_t* dst_y, |
| 103 | int dst_stride_y, |
| 104 | int scale, // 16384 for 10 bits |
| 105 | int width, |
| 106 | int height) { |
| 107 | int y; |
| 108 | void (*Convert16To8Row)(const uint16_t* src_y, uint8_t* dst_y, int scale, |
| 109 | int width) = Convert16To8Row_C; |
| 110 |
| 111 | if (width <= 0 || height == 0) { |
| 112 | return; |
| 113 | } |
| 114 | // Negative height means invert the image. |
| 115 | if (height < 0) { |
| 116 | height = -height; |
| 117 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 118 | dst_stride_y = -dst_stride_y; |
| 119 | } |
| 120 | // Coalesce rows. |
| 121 | if (src_stride_y == width && dst_stride_y == width) { |
| 122 | width *= height; |
| 123 | height = 1; |
| 124 | src_stride_y = dst_stride_y = 0; |
| 125 | } |
| 126 | #if defined(HAS_CONVERT16TO8ROW_NEON) |
| 127 | if (TestCpuFlag(kCpuHasNEON)) { |
| 128 | Convert16To8Row = Convert16To8Row_Any_NEON; |
| 129 | if (IS_ALIGNED(width, 16)) { |
| 130 | Convert16To8Row = Convert16To8Row_NEON; |
| 131 | } |
| 132 | } |
| 133 | #endif |
| 134 | #if defined(HAS_CONVERT16TO8ROW_SSSE3) |
| 135 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 136 | Convert16To8Row = Convert16To8Row_Any_SSSE3; |
| 137 | if (IS_ALIGNED(width, 16)) { |
| 138 | Convert16To8Row = Convert16To8Row_SSSE3; |
| 139 | } |
| 140 | } |
| 141 | #endif |
| 142 | #if defined(HAS_CONVERT16TO8ROW_AVX2) |
| 143 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 144 | Convert16To8Row = Convert16To8Row_Any_AVX2; |
| 145 | if (IS_ALIGNED(width, 32)) { |
| 146 | Convert16To8Row = Convert16To8Row_AVX2; |
| 147 | } |
| 148 | } |
| 149 | #endif |
| 150 |
| 151 | // Convert plane |
| 152 | for (y = 0; y < height; ++y) { |
| 153 | Convert16To8Row(src_y, dst_y, scale, width); |
| 154 | src_y += src_stride_y; |
| 155 | dst_y += dst_stride_y; |
| 156 | } |
| 157 | } |
| 158 |
| 159 | // Convert a plane of 8 bit data to 16 bit |
| 160 | LIBYUV_API |
| 161 | void Convert8To16Plane(const uint8_t* src_y, |
| 162 | int src_stride_y, |
| 163 | uint16_t* dst_y, |
| 164 | int dst_stride_y, |
| 165 | int scale, // 16384 for 10 bits |
| 166 | int width, |
| 167 | int height) { |
| 168 | int y; |
| 169 | void (*Convert8To16Row)(const uint8_t* src_y, uint16_t* dst_y, int scale, |
| 170 | int width) = Convert8To16Row_C; |
| 171 |
| 172 | if (width <= 0 || height == 0) { |
| 173 | return; |
| 174 | } |
| 175 | // Negative height means invert the image. |
| 176 | if (height < 0) { |
| 177 | height = -height; |
| 178 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 179 | dst_stride_y = -dst_stride_y; |
| 180 | } |
| 181 | // Coalesce rows. |
| 182 | if (src_stride_y == width && dst_stride_y == width) { |
| 183 | width *= height; |
| 184 | height = 1; |
| 185 | src_stride_y = dst_stride_y = 0; |
| 186 | } |
| 187 | #if defined(HAS_CONVERT8TO16ROW_SSE2) |
| 188 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 189 | Convert8To16Row = Convert8To16Row_Any_SSE2; |
| 190 | if (IS_ALIGNED(width, 16)) { |
| 191 | Convert8To16Row = Convert8To16Row_SSE2; |
| 192 | } |
| 193 | } |
| 194 | #endif |
| 195 | #if defined(HAS_CONVERT8TO16ROW_AVX2) |
| 196 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 197 | Convert8To16Row = Convert8To16Row_Any_AVX2; |
| 198 | if (IS_ALIGNED(width, 32)) { |
| 199 | Convert8To16Row = Convert8To16Row_AVX2; |
| 200 | } |
| 201 | } |
| 202 | #endif |
| 203 |
| 204 | // Convert plane |
| 205 | for (y = 0; y < height; ++y) { |
| 206 | Convert8To16Row(src_y, dst_y, scale, width); |
| 207 | src_y += src_stride_y; |
| 208 | dst_y += dst_stride_y; |
| 209 | } |
| 210 | } |
| 211 |
| 212 | // Copy I422. |
| 213 | LIBYUV_API |
| 214 | int I422Copy(const uint8_t* src_y, |
| 215 | int src_stride_y, |
| 216 | const uint8_t* src_u, |
| 217 | int src_stride_u, |
| 218 | const uint8_t* src_v, |
| 219 | int src_stride_v, |
| 220 | uint8_t* dst_y, |
| 221 | int dst_stride_y, |
| 222 | uint8_t* dst_u, |
| 223 | int dst_stride_u, |
| 224 | uint8_t* dst_v, |
| 225 | int dst_stride_v, |
| 226 | int width, |
| 227 | int height) { |
| 228 | int halfwidth = (width + 1) >> 1; |
| 229 |
| 230 | if ((!src_y && dst_y) || !src_u || !src_v || !dst_u || !dst_v || width <= 0 || |
| 231 | height == 0) { |
| 232 | return -1; |
| 233 | } |
| 234 |
| 235 | // Negative height means invert the image. |
| 236 | if (height < 0) { |
| 237 | height = -height; |
| 238 | src_y = src_y + (height - 1) * src_stride_y; |
| 239 | src_u = src_u + (height - 1) * src_stride_u; |
| 240 | src_v = src_v + (height - 1) * src_stride_v; |
| 241 | src_stride_y = -src_stride_y; |
| 242 | src_stride_u = -src_stride_u; |
| 243 | src_stride_v = -src_stride_v; |
| 244 | } |
| 245 |
| 246 | if (dst_y) { |
| 247 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 248 | } |
| 249 | CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height); |
| 250 | CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height); |
| 251 | return 0; |
| 252 | } |
| 253 |
| 254 | // Copy I444. |
| 255 | LIBYUV_API |
| 256 | int I444Copy(const uint8_t* src_y, |
| 257 | int src_stride_y, |
| 258 | const uint8_t* src_u, |
| 259 | int src_stride_u, |
| 260 | const uint8_t* src_v, |
| 261 | int src_stride_v, |
| 262 | uint8_t* dst_y, |
| 263 | int dst_stride_y, |
| 264 | uint8_t* dst_u, |
| 265 | int dst_stride_u, |
| 266 | uint8_t* dst_v, |
| 267 | int dst_stride_v, |
| 268 | int width, |
| 269 | int height) { |
| 270 | if ((!src_y && dst_y) || !src_u || !src_v || !dst_u || !dst_v || width <= 0 || |
| 271 | height == 0) { |
| 272 | return -1; |
| 273 | } |
| 274 | // Negative height means invert the image. |
| 275 | if (height < 0) { |
| 276 | height = -height; |
| 277 | src_y = src_y + (height - 1) * src_stride_y; |
| 278 | src_u = src_u + (height - 1) * src_stride_u; |
| 279 | src_v = src_v + (height - 1) * src_stride_v; |
| 280 | src_stride_y = -src_stride_y; |
| 281 | src_stride_u = -src_stride_u; |
| 282 | src_stride_v = -src_stride_v; |
| 283 | } |
| 284 |
| 285 | if (dst_y) { |
| 286 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 287 | } |
| 288 | CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 289 | CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 290 | return 0; |
| 291 | } |
| 292 |
| 293 | // Copy I210. |
| 294 | LIBYUV_API |
| 295 | int I210Copy(const uint16_t* src_y, |
| 296 | int src_stride_y, |
| 297 | const uint16_t* src_u, |
| 298 | int src_stride_u, |
| 299 | const uint16_t* src_v, |
| 300 | int src_stride_v, |
| 301 | uint16_t* dst_y, |
| 302 | int dst_stride_y, |
| 303 | uint16_t* dst_u, |
| 304 | int dst_stride_u, |
| 305 | uint16_t* dst_v, |
| 306 | int dst_stride_v, |
| 307 | int width, |
| 308 | int height) { |
| 309 | int halfwidth = (width + 1) >> 1; |
| 310 |
| 311 | if ((!src_y && dst_y) || !src_u || !src_v || !dst_u || !dst_v || width <= 0 || |
| 312 | height == 0) { |
| 313 | return -1; |
| 314 | } |
| 315 |
| 316 | // Negative height means invert the image. |
| 317 | if (height < 0) { |
| 318 | height = -height; |
| 319 | src_y = src_y + (height - 1) * src_stride_y; |
| 320 | src_u = src_u + (height - 1) * src_stride_u; |
| 321 | src_v = src_v + (height - 1) * src_stride_v; |
| 322 | src_stride_y = -src_stride_y; |
| 323 | src_stride_u = -src_stride_u; |
| 324 | src_stride_v = -src_stride_v; |
| 325 | } |
| 326 |
| 327 | if (dst_y) { |
| 328 | CopyPlane_16(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 329 | } |
| 330 | // Copy UV planes. |
| 331 | CopyPlane_16(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height); |
| 332 | CopyPlane_16(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height); |
| 333 | return 0; |
| 334 | } |
| 335 |
| 336 | // Copy I410. |
| 337 | LIBYUV_API |
| 338 | int I410Copy(const uint16_t* src_y, |
| 339 | int src_stride_y, |
| 340 | const uint16_t* src_u, |
| 341 | int src_stride_u, |
| 342 | const uint16_t* src_v, |
| 343 | int src_stride_v, |
| 344 | uint16_t* dst_y, |
| 345 | int dst_stride_y, |
| 346 | uint16_t* dst_u, |
| 347 | int dst_stride_u, |
| 348 | uint16_t* dst_v, |
| 349 | int dst_stride_v, |
| 350 | int width, |
| 351 | int height) { |
| 352 | if ((!src_y && dst_y) || !src_u || !src_v || !dst_u || !dst_v || width <= 0 || |
| 353 | height == 0) { |
| 354 | return -1; |
| 355 | } |
| 356 | // Negative height means invert the image. |
| 357 | if (height < 0) { |
| 358 | height = -height; |
| 359 | src_y = src_y + (height - 1) * src_stride_y; |
| 360 | src_u = src_u + (height - 1) * src_stride_u; |
| 361 | src_v = src_v + (height - 1) * src_stride_v; |
| 362 | src_stride_y = -src_stride_y; |
| 363 | src_stride_u = -src_stride_u; |
| 364 | src_stride_v = -src_stride_v; |
| 365 | } |
| 366 |
| 367 | if (dst_y) { |
| 368 | CopyPlane_16(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 369 | } |
| 370 | CopyPlane_16(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 371 | CopyPlane_16(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 372 | return 0; |
| 373 | } |
| 374 |
| 375 | // Copy I400. |
| 376 | LIBYUV_API |
| 377 | int I400ToI400(const uint8_t* src_y, |
| 378 | int src_stride_y, |
| 379 | uint8_t* dst_y, |
| 380 | int dst_stride_y, |
| 381 | int width, |
| 382 | int height) { |
| 383 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 384 | return -1; |
| 385 | } |
| 386 | // Negative height means invert the image. |
| 387 | if (height < 0) { |
| 388 | height = -height; |
| 389 | src_y = src_y + (height - 1) * src_stride_y; |
| 390 | src_stride_y = -src_stride_y; |
| 391 | } |
| 392 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 393 | return 0; |
| 394 | } |
| 395 |
| 396 | // Convert I420 to I400. |
| 397 | LIBYUV_API |
| 398 | int I420ToI400(const uint8_t* src_y, |
| 399 | int src_stride_y, |
| 400 | const uint8_t* src_u, |
| 401 | int src_stride_u, |
| 402 | const uint8_t* src_v, |
| 403 | int src_stride_v, |
| 404 | uint8_t* dst_y, |
| 405 | int dst_stride_y, |
| 406 | int width, |
| 407 | int height) { |
| 408 | (void)src_u; |
| 409 | (void)src_stride_u; |
| 410 | (void)src_v; |
| 411 | (void)src_stride_v; |
| 412 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 413 | return -1; |
| 414 | } |
| 415 | // Negative height means invert the image. |
| 416 | if (height < 0) { |
| 417 | height = -height; |
| 418 | src_y = src_y + (height - 1) * src_stride_y; |
| 419 | src_stride_y = -src_stride_y; |
| 420 | } |
| 421 |
| 422 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 423 | return 0; |
| 424 | } |
| 425 |
| 426 | // Copy NV12. Supports inverting. |
| 427 | LIBYUV_API |
| 428 | int NV12Copy(const uint8_t* src_y, |
| 429 | int src_stride_y, |
| 430 | const uint8_t* src_uv, |
| 431 | int src_stride_uv, |
| 432 | uint8_t* dst_y, |
| 433 | int dst_stride_y, |
| 434 | uint8_t* dst_uv, |
| 435 | int dst_stride_uv, |
| 436 | int width, |
| 437 | int height) { |
| 438 | int halfwidth = (width + 1) >> 1; |
| 439 | int halfheight = (height + 1) >> 1; |
| 440 |
| 441 | if (!src_y || !dst_y || !src_uv || !dst_uv || width <= 0 || height == 0) { |
| 442 | return -1; |
| 443 | } |
| 444 |
| 445 | // Negative height means invert the image. |
| 446 | if (height < 0) { |
| 447 | height = -height; |
| 448 | halfheight = (height + 1) >> 1; |
| 449 | src_y = src_y + (height - 1) * src_stride_y; |
| 450 | src_uv = src_uv + (halfheight - 1) * src_stride_uv; |
| 451 | src_stride_y = -src_stride_y; |
| 452 | src_stride_uv = -src_stride_uv; |
| 453 | } |
| 454 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 455 | CopyPlane(src_uv, src_stride_uv, dst_uv, dst_stride_uv, halfwidth * 2, |
| 456 | halfheight); |
| 457 | return 0; |
| 458 | } |
| 459 |
| 460 | // Copy NV21. Supports inverting. |
| 461 | LIBYUV_API |
| 462 | int NV21Copy(const uint8_t* src_y, |
| 463 | int src_stride_y, |
| 464 | const uint8_t* src_vu, |
| 465 | int src_stride_vu, |
| 466 | uint8_t* dst_y, |
| 467 | int dst_stride_y, |
| 468 | uint8_t* dst_vu, |
| 469 | int dst_stride_vu, |
| 470 | int width, |
| 471 | int height) { |
| 472 | return NV12Copy(src_y, src_stride_y, src_vu, src_stride_vu, dst_y, |
| 473 | dst_stride_y, dst_vu, dst_stride_vu, width, height); |
| 474 | } |
| 475 |
| 476 | // Support function for NV12 etc UV channels. |
| 477 | // Width and height are plane sizes (typically half pixel width). |
| 478 | LIBYUV_API |
| 479 | void SplitUVPlane(const uint8_t* src_uv, |
| 480 | int src_stride_uv, |
| 481 | uint8_t* dst_u, |
| 482 | int dst_stride_u, |
| 483 | uint8_t* dst_v, |
| 484 | int dst_stride_v, |
| 485 | int width, |
| 486 | int height) { |
| 487 | int y; |
| 488 | void (*SplitUVRow)(const uint8_t* src_uv, uint8_t* dst_u, uint8_t* dst_v, |
| 489 | int width) = SplitUVRow_C; |
| 490 | if (width <= 0 || height == 0) { |
| 491 | return; |
| 492 | } |
| 493 | // Negative height means invert the image. |
| 494 | if (height < 0) { |
| 495 | height = -height; |
| 496 | dst_u = dst_u + (height - 1) * dst_stride_u; |
| 497 | dst_v = dst_v + (height - 1) * dst_stride_v; |
| 498 | dst_stride_u = -dst_stride_u; |
| 499 | dst_stride_v = -dst_stride_v; |
| 500 | } |
| 501 | // Coalesce rows. |
| 502 | if (src_stride_uv == width * 2 && dst_stride_u == width && |
| 503 | dst_stride_v == width) { |
| 504 | width *= height; |
| 505 | height = 1; |
| 506 | src_stride_uv = dst_stride_u = dst_stride_v = 0; |
| 507 | } |
| 508 | #if defined(HAS_SPLITUVROW_SSE2) |
| 509 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 510 | SplitUVRow = SplitUVRow_Any_SSE2; |
| 511 | if (IS_ALIGNED(width, 16)) { |
| 512 | SplitUVRow = SplitUVRow_SSE2; |
| 513 | } |
| 514 | } |
| 515 | #endif |
| 516 | #if defined(HAS_SPLITUVROW_AVX2) |
| 517 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 518 | SplitUVRow = SplitUVRow_Any_AVX2; |
| 519 | if (IS_ALIGNED(width, 32)) { |
| 520 | SplitUVRow = SplitUVRow_AVX2; |
| 521 | } |
| 522 | } |
| 523 | #endif |
| 524 | #if defined(HAS_SPLITUVROW_NEON) |
| 525 | if (TestCpuFlag(kCpuHasNEON)) { |
| 526 | SplitUVRow = SplitUVRow_Any_NEON; |
| 527 | if (IS_ALIGNED(width, 16)) { |
| 528 | SplitUVRow = SplitUVRow_NEON; |
| 529 | } |
| 530 | } |
| 531 | #endif |
| 532 | #if defined(HAS_SPLITUVROW_MSA) |
| 533 | if (TestCpuFlag(kCpuHasMSA)) { |
| 534 | SplitUVRow = SplitUVRow_Any_MSA; |
| 535 | if (IS_ALIGNED(width, 32)) { |
| 536 | SplitUVRow = SplitUVRow_MSA; |
| 537 | } |
| 538 | } |
| 539 | #endif |
| 540 | #if defined(HAS_SPLITUVROW_LSX) |
| 541 | if (TestCpuFlag(kCpuHasLSX)) { |
| 542 | SplitUVRow = SplitUVRow_Any_LSX; |
| 543 | if (IS_ALIGNED(width, 32)) { |
| 544 | SplitUVRow = SplitUVRow_LSX; |
| 545 | } |
| 546 | } |
| 547 | #endif |
| 548 |
| 549 | for (y = 0; y < height; ++y) { |
| 550 | // Copy a row of UV. |
| 551 | SplitUVRow(src_uv, dst_u, dst_v, width); |
| 552 | dst_u += dst_stride_u; |
| 553 | dst_v += dst_stride_v; |
| 554 | src_uv += src_stride_uv; |
| 555 | } |
| 556 | } |
| 557 |
| 558 | LIBYUV_API |
| 559 | void MergeUVPlane(const uint8_t* src_u, |
| 560 | int src_stride_u, |
| 561 | const uint8_t* src_v, |
| 562 | int src_stride_v, |
| 563 | uint8_t* dst_uv, |
| 564 | int dst_stride_uv, |
| 565 | int width, |
| 566 | int height) { |
| 567 | int y; |
| 568 | void (*MergeUVRow)(const uint8_t* src_u, const uint8_t* src_v, |
| 569 | uint8_t* dst_uv, int width) = MergeUVRow_C; |
| 570 | if (width <= 0 || height == 0) { |
| 571 | return; |
| 572 | } |
| 573 | // Negative height means invert the image. |
| 574 | if (height < 0) { |
| 575 | height = -height; |
| 576 | dst_uv = dst_uv + (height - 1) * dst_stride_uv; |
| 577 | dst_stride_uv = -dst_stride_uv; |
| 578 | } |
| 579 | // Coalesce rows. |
| 580 | if (src_stride_u == width && src_stride_v == width && |
| 581 | dst_stride_uv == width * 2) { |
| 582 | width *= height; |
| 583 | height = 1; |
| 584 | src_stride_u = src_stride_v = dst_stride_uv = 0; |
| 585 | } |
| 586 | #if defined(HAS_MERGEUVROW_SSE2) |
| 587 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 588 | MergeUVRow = MergeUVRow_Any_SSE2; |
| 589 | if (IS_ALIGNED(width, 16)) { |
| 590 | MergeUVRow = MergeUVRow_SSE2; |
| 591 | } |
| 592 | } |
| 593 | #endif |
| 594 | #if defined(HAS_MERGEUVROW_AVX2) |
| 595 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 596 | MergeUVRow = MergeUVRow_Any_AVX2; |
| 597 | if (IS_ALIGNED(width, 32)) { |
| 598 | MergeUVRow = MergeUVRow_AVX2; |
| 599 | } |
| 600 | } |
| 601 | #endif |
| 602 | #if defined(HAS_MERGEUVROW_NEON) |
| 603 | if (TestCpuFlag(kCpuHasNEON)) { |
| 604 | MergeUVRow = MergeUVRow_Any_NEON; |
| 605 | if (IS_ALIGNED(width, 16)) { |
| 606 | MergeUVRow = MergeUVRow_NEON; |
| 607 | } |
| 608 | } |
| 609 | #endif |
| 610 | #if defined(HAS_MERGEUVROW_MSA) |
| 611 | if (TestCpuFlag(kCpuHasMSA)) { |
| 612 | MergeUVRow = MergeUVRow_Any_MSA; |
| 613 | if (IS_ALIGNED(width, 16)) { |
| 614 | MergeUVRow = MergeUVRow_MSA; |
| 615 | } |
| 616 | } |
| 617 | #endif |
| 618 | #if defined(HAS_MERGEUVROW_LSX) |
| 619 | if (TestCpuFlag(kCpuHasLSX)) { |
| 620 | MergeUVRow = MergeUVRow_Any_LSX; |
| 621 | if (IS_ALIGNED(width, 16)) { |
| 622 | MergeUVRow = MergeUVRow_LSX; |
| 623 | } |
| 624 | } |
| 625 | #endif |
| 626 |
| 627 | for (y = 0; y < height; ++y) { |
| 628 | // Merge a row of U and V into a row of UV. |
| 629 | MergeUVRow(src_u, src_v, dst_uv, width); |
| 630 | src_u += src_stride_u; |
| 631 | src_v += src_stride_v; |
| 632 | dst_uv += dst_stride_uv; |
| 633 | } |
| 634 | } |
| 635 |
| 636 | // Support function for P010 etc UV channels. |
| 637 | // Width and height are plane sizes (typically half pixel width). |
| 638 | LIBYUV_API |
| 639 | void SplitUVPlane_16(const uint16_t* src_uv, |
| 640 | int src_stride_uv, |
| 641 | uint16_t* dst_u, |
| 642 | int dst_stride_u, |
| 643 | uint16_t* dst_v, |
| 644 | int dst_stride_v, |
| 645 | int width, |
| 646 | int height, |
| 647 | int depth) { |
| 648 | int y; |
| 649 | void (*SplitUVRow_16)(const uint16_t* src_uv, uint16_t* dst_u, |
| 650 | uint16_t* dst_v, int depth, int width) = |
| 651 | SplitUVRow_16_C; |
| 652 | if (width <= 0 || height == 0) { |
| 653 | return; |
| 654 | } |
| 655 | // Negative height means invert the image. |
| 656 | if (height < 0) { |
| 657 | height = -height; |
| 658 | dst_u = dst_u + (height - 1) * dst_stride_u; |
| 659 | dst_v = dst_v + (height - 1) * dst_stride_v; |
| 660 | dst_stride_u = -dst_stride_u; |
| 661 | dst_stride_v = -dst_stride_v; |
| 662 | } |
| 663 | // Coalesce rows. |
| 664 | if (src_stride_uv == width * 2 && dst_stride_u == width && |
| 665 | dst_stride_v == width) { |
| 666 | width *= height; |
| 667 | height = 1; |
| 668 | src_stride_uv = dst_stride_u = dst_stride_v = 0; |
| 669 | } |
| 670 | #if defined(HAS_SPLITUVROW_16_AVX2) |
| 671 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 672 | SplitUVRow_16 = SplitUVRow_16_Any_AVX2; |
| 673 | if (IS_ALIGNED(width, 16)) { |
| 674 | SplitUVRow_16 = SplitUVRow_16_AVX2; |
| 675 | } |
| 676 | } |
| 677 | #endif |
| 678 | #if defined(HAS_SPLITUVROW_16_NEON) |
| 679 | if (TestCpuFlag(kCpuHasNEON)) { |
| 680 | SplitUVRow_16 = SplitUVRow_16_Any_NEON; |
| 681 | if (IS_ALIGNED(width, 8)) { |
| 682 | SplitUVRow_16 = SplitUVRow_16_NEON; |
| 683 | } |
| 684 | } |
| 685 | #endif |
| 686 |
| 687 | for (y = 0; y < height; ++y) { |
| 688 | // Copy a row of UV. |
| 689 | SplitUVRow_16(src_uv, dst_u, dst_v, depth, width); |
| 690 | dst_u += dst_stride_u; |
| 691 | dst_v += dst_stride_v; |
| 692 | src_uv += src_stride_uv; |
| 693 | } |
| 694 | } |
| 695 |
| 696 | LIBYUV_API |
| 697 | void MergeUVPlane_16(const uint16_t* src_u, |
| 698 | int src_stride_u, |
| 699 | const uint16_t* src_v, |
| 700 | int src_stride_v, |
| 701 | uint16_t* dst_uv, |
| 702 | int dst_stride_uv, |
| 703 | int width, |
| 704 | int height, |
| 705 | int depth) { |
| 706 | int y; |
| 707 | void (*MergeUVRow_16)(const uint16_t* src_u, const uint16_t* src_v, |
| 708 | uint16_t* dst_uv, int depth, int width) = |
| 709 | MergeUVRow_16_C; |
| 710 | assert(depth >= 8); |
| 711 | assert(depth <= 16); |
| 712 | if (width <= 0 || height == 0) { |
| 713 | return; |
| 714 | } |
| 715 | // Negative height means invert the image. |
| 716 | if (height < 0) { |
| 717 | height = -height; |
| 718 | dst_uv = dst_uv + (height - 1) * dst_stride_uv; |
| 719 | dst_stride_uv = -dst_stride_uv; |
| 720 | } |
| 721 | // Coalesce rows. |
| 722 | if (src_stride_u == width && src_stride_v == width && |
| 723 | dst_stride_uv == width * 2) { |
| 724 | width *= height; |
| 725 | height = 1; |
| 726 | src_stride_u = src_stride_v = dst_stride_uv = 0; |
| 727 | } |
| 728 | #if defined(HAS_MERGEUVROW_16_AVX2) |
| 729 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 730 | MergeUVRow_16 = MergeUVRow_16_Any_AVX2; |
| 731 | if (IS_ALIGNED(width, 16)) { |
| 732 | MergeUVRow_16 = MergeUVRow_16_AVX2; |
| 733 | } |
| 734 | } |
| 735 | #endif |
| 736 | #if defined(HAS_MERGEUVROW_16_NEON) |
| 737 | if (TestCpuFlag(kCpuHasNEON)) { |
| 738 | MergeUVRow_16 = MergeUVRow_16_Any_NEON; |
| 739 | if (IS_ALIGNED(width, 8)) { |
| 740 | MergeUVRow_16 = MergeUVRow_16_NEON; |
| 741 | } |
| 742 | } |
| 743 | #endif |
| 744 |
| 745 | for (y = 0; y < height; ++y) { |
| 746 | // Merge a row of U and V into a row of UV. |
| 747 | MergeUVRow_16(src_u, src_v, dst_uv, depth, width); |
| 748 | src_u += src_stride_u; |
| 749 | src_v += src_stride_v; |
| 750 | dst_uv += dst_stride_uv; |
| 751 | } |
| 752 | } |
| 753 |
| 754 | // Convert plane from lsb to msb |
| 755 | LIBYUV_API |
| 756 | void ConvertToMSBPlane_16(const uint16_t* src_y, |
| 757 | int src_stride_y, |
| 758 | uint16_t* dst_y, |
| 759 | int dst_stride_y, |
| 760 | int width, |
| 761 | int height, |
| 762 | int depth) { |
| 763 | int y; |
| 764 | int scale = 1 << (16 - depth); |
| 765 | void (*MultiplyRow_16)(const uint16_t* src_y, uint16_t* dst_y, int scale, |
| 766 | int width) = MultiplyRow_16_C; |
| 767 | if (width <= 0 || height == 0) { |
| 768 | return; |
| 769 | } |
| 770 | // Negative height means invert the image. |
| 771 | if (height < 0) { |
| 772 | height = -height; |
| 773 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 774 | dst_stride_y = -dst_stride_y; |
| 775 | } |
| 776 | // Coalesce rows. |
| 777 | if (src_stride_y == width && dst_stride_y == width) { |
| 778 | width *= height; |
| 779 | height = 1; |
| 780 | src_stride_y = dst_stride_y = 0; |
| 781 | } |
| 782 |
| 783 | #if defined(HAS_MULTIPLYROW_16_AVX2) |
| 784 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 785 | MultiplyRow_16 = MultiplyRow_16_Any_AVX2; |
| 786 | if (IS_ALIGNED(width, 32)) { |
| 787 | MultiplyRow_16 = MultiplyRow_16_AVX2; |
| 788 | } |
| 789 | } |
| 790 | #endif |
| 791 | #if defined(HAS_MULTIPLYROW_16_NEON) |
| 792 | if (TestCpuFlag(kCpuHasNEON)) { |
| 793 | MultiplyRow_16 = MultiplyRow_16_Any_NEON; |
| 794 | if (IS_ALIGNED(width, 16)) { |
| 795 | MultiplyRow_16 = MultiplyRow_16_NEON; |
| 796 | } |
| 797 | } |
| 798 | #endif |
| 799 |
| 800 | for (y = 0; y < height; ++y) { |
| 801 | MultiplyRow_16(src_y, dst_y, scale, width); |
| 802 | src_y += src_stride_y; |
| 803 | dst_y += dst_stride_y; |
| 804 | } |
| 805 | } |
| 806 |
| 807 | // Convert plane from msb to lsb |
| 808 | LIBYUV_API |
| 809 | void ConvertToLSBPlane_16(const uint16_t* src_y, |
| 810 | int src_stride_y, |
| 811 | uint16_t* dst_y, |
| 812 | int dst_stride_y, |
| 813 | int width, |
| 814 | int height, |
| 815 | int depth) { |
| 816 | int y; |
| 817 | int scale = 1 << depth; |
| 818 | void (*DivideRow)(const uint16_t* src_y, uint16_t* dst_y, int scale, |
| 819 | int width) = DivideRow_16_C; |
| 820 | if (width <= 0 || height == 0) { |
| 821 | return; |
| 822 | } |
| 823 | // Negative height means invert the image. |
| 824 | if (height < 0) { |
| 825 | height = -height; |
| 826 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 827 | dst_stride_y = -dst_stride_y; |
| 828 | } |
| 829 | // Coalesce rows. |
| 830 | if (src_stride_y == width && dst_stride_y == width) { |
| 831 | width *= height; |
| 832 | height = 1; |
| 833 | src_stride_y = dst_stride_y = 0; |
| 834 | } |
| 835 |
| 836 | #if defined(HAS_DIVIDEROW_16_AVX2) |
| 837 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 838 | DivideRow = DivideRow_16_Any_AVX2; |
| 839 | if (IS_ALIGNED(width, 32)) { |
| 840 | DivideRow = DivideRow_16_AVX2; |
| 841 | } |
| 842 | } |
| 843 | #endif |
| 844 | #if defined(HAS_DIVIDEROW_16_NEON) |
| 845 | if (TestCpuFlag(kCpuHasNEON)) { |
| 846 | DivideRow = DivideRow_16_Any_NEON; |
| 847 | if (IS_ALIGNED(width, 16)) { |
| 848 | DivideRow = DivideRow_16_NEON; |
| 849 | } |
| 850 | } |
| 851 | #endif |
| 852 |
| 853 | for (y = 0; y < height; ++y) { |
| 854 | DivideRow(src_y, dst_y, scale, width); |
| 855 | src_y += src_stride_y; |
| 856 | dst_y += dst_stride_y; |
| 857 | } |
| 858 | } |
| 859 |
| 860 | // Swap U and V channels in interleaved UV plane. |
| 861 | LIBYUV_API |
| 862 | void SwapUVPlane(const uint8_t* src_uv, |
| 863 | int src_stride_uv, |
| 864 | uint8_t* dst_vu, |
| 865 | int dst_stride_vu, |
| 866 | int width, |
| 867 | int height) { |
| 868 | int y; |
| 869 | void (*SwapUVRow)(const uint8_t* src_uv, uint8_t* dst_vu, int width) = |
| 870 | SwapUVRow_C; |
| 871 | if (width <= 0 || height == 0) { |
| 872 | return; |
| 873 | } |
| 874 | // Negative height means invert the image. |
| 875 | if (height < 0) { |
| 876 | height = -height; |
| 877 | src_uv = src_uv + (height - 1) * src_stride_uv; |
| 878 | src_stride_uv = -src_stride_uv; |
| 879 | } |
| 880 | // Coalesce rows. |
| 881 | if (src_stride_uv == width * 2 && dst_stride_vu == width * 2) { |
| 882 | width *= height; |
| 883 | height = 1; |
| 884 | src_stride_uv = dst_stride_vu = 0; |
| 885 | } |
| 886 |
| 887 | #if defined(HAS_SWAPUVROW_SSSE3) |
| 888 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 889 | SwapUVRow = SwapUVRow_Any_SSSE3; |
| 890 | if (IS_ALIGNED(width, 16)) { |
| 891 | SwapUVRow = SwapUVRow_SSSE3; |
| 892 | } |
| 893 | } |
| 894 | #endif |
| 895 | #if defined(HAS_SWAPUVROW_AVX2) |
| 896 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 897 | SwapUVRow = SwapUVRow_Any_AVX2; |
| 898 | if (IS_ALIGNED(width, 32)) { |
| 899 | SwapUVRow = SwapUVRow_AVX2; |
| 900 | } |
| 901 | } |
| 902 | #endif |
| 903 | #if defined(HAS_SWAPUVROW_NEON) |
| 904 | if (TestCpuFlag(kCpuHasNEON)) { |
| 905 | SwapUVRow = SwapUVRow_Any_NEON; |
| 906 | if (IS_ALIGNED(width, 16)) { |
| 907 | SwapUVRow = SwapUVRow_NEON; |
| 908 | } |
| 909 | } |
| 910 | #endif |
| 911 |
| 912 | for (y = 0; y < height; ++y) { |
| 913 | SwapUVRow(src_uv, dst_vu, width); |
| 914 | src_uv += src_stride_uv; |
| 915 | dst_vu += dst_stride_vu; |
| 916 | } |
| 917 | } |
| 918 |
| 919 | // Convert NV21 to NV12. |
| 920 | LIBYUV_API |
| 921 | int NV21ToNV12(const uint8_t* src_y, |
| 922 | int src_stride_y, |
| 923 | const uint8_t* src_vu, |
| 924 | int src_stride_vu, |
| 925 | uint8_t* dst_y, |
| 926 | int dst_stride_y, |
| 927 | uint8_t* dst_uv, |
| 928 | int dst_stride_uv, |
| 929 | int width, |
| 930 | int height) { |
| 931 | int halfwidth = (width + 1) >> 1; |
| 932 | int halfheight = (height + 1) >> 1; |
| 933 |
| 934 | if (!src_vu || !dst_uv || width <= 0 || height == 0) { |
| 935 | return -1; |
| 936 | } |
| 937 |
| 938 | if (dst_y) { |
| 939 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 940 | } |
| 941 |
| 942 | // Negative height means invert the image. |
| 943 | if (height < 0) { |
| 944 | height = -height; |
| 945 | halfheight = (height + 1) >> 1; |
| 946 | src_vu = src_vu + (halfheight - 1) * src_stride_vu; |
| 947 | src_stride_vu = -src_stride_vu; |
| 948 | } |
| 949 |
| 950 | SwapUVPlane(src_vu, src_stride_vu, dst_uv, dst_stride_uv, halfwidth, |
| 951 | halfheight); |
| 952 | return 0; |
| 953 | } |
| 954 |
| 955 | // Test if tile_height is a power of 2 (16 or 32) |
| 956 | #define IS_POWEROFTWO(x) (!((x) & ((x)-1))) |
| 957 |
| 958 | // Detile a plane of data |
| 959 | // tile width is 16 and assumed. |
| 960 | // tile_height is 16 or 32 for MM21. |
| 961 | // src_stride_y is bytes per row of source ignoring tiling. e.g. 640 |
| 962 | // TODO: More detile row functions. |
| 963 | LIBYUV_API |
| 964 | int DetilePlane(const uint8_t* src_y, |
| 965 | int src_stride_y, |
| 966 | uint8_t* dst_y, |
| 967 | int dst_stride_y, |
| 968 | int width, |
| 969 | int height, |
| 970 | int tile_height) { |
| 971 | const ptrdiff_t src_tile_stride = 16 * tile_height; |
| 972 | int y; |
| 973 | void (*DetileRow)(const uint8_t* src, ptrdiff_t src_tile_stride, uint8_t* dst, |
| 974 | int width) = DetileRow_C; |
| 975 | if (!src_y || !dst_y || width <= 0 || height == 0 || |
| 976 | !IS_POWEROFTWO(tile_height)) { |
| 977 | return -1; |
| 978 | } |
| 979 |
| 980 | // Negative height means invert the image. |
| 981 | if (height < 0) { |
| 982 | height = -height; |
| 983 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 984 | dst_stride_y = -dst_stride_y; |
| 985 | } |
| 986 |
| 987 | #if defined(HAS_DETILEROW_SSE2) |
| 988 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 989 | DetileRow = DetileRow_Any_SSE2; |
| 990 | if (IS_ALIGNED(width, 16)) { |
| 991 | DetileRow = DetileRow_SSE2; |
| 992 | } |
| 993 | } |
| 994 | #endif |
| 995 | #if defined(HAS_DETILEROW_NEON) |
| 996 | if (TestCpuFlag(kCpuHasNEON)) { |
| 997 | DetileRow = DetileRow_Any_NEON; |
| 998 | if (IS_ALIGNED(width, 16)) { |
| 999 | DetileRow = DetileRow_NEON; |
| 1000 | } |
| 1001 | } |
| 1002 | #endif |
| 1003 |
| 1004 | // Detile plane |
| 1005 | for (y = 0; y < height; ++y) { |
| 1006 | DetileRow(src_y, src_tile_stride, dst_y, width); |
| 1007 | dst_y += dst_stride_y; |
| 1008 | src_y += 16; |
| 1009 | // Advance to next row of tiles. |
| 1010 | if ((y & (tile_height - 1)) == (tile_height - 1)) { |
| 1011 | src_y = src_y - src_tile_stride + src_stride_y * tile_height; |
| 1012 | } |
| 1013 | } |
| 1014 | return 0; |
| 1015 | } |
| 1016 |
| 1017 | // Convert a plane of 16 bit tiles of 16 x H to linear. |
| 1018 | // tile width is 16 and assumed. |
| 1019 | // tile_height is 16 or 32 for MT2T. |
| 1020 | LIBYUV_API |
| 1021 | int DetilePlane_16(const uint16_t* src_y, |
| 1022 | int src_stride_y, |
| 1023 | uint16_t* dst_y, |
| 1024 | int dst_stride_y, |
| 1025 | int width, |
| 1026 | int height, |
| 1027 | int tile_height) { |
| 1028 | const ptrdiff_t src_tile_stride = 16 * tile_height; |
| 1029 | int y; |
| 1030 | void (*DetileRow_16)(const uint16_t* src, ptrdiff_t src_tile_stride, |
| 1031 | uint16_t* dst, int width) = DetileRow_16_C; |
| 1032 | if (!src_y || !dst_y || width <= 0 || height == 0 || |
| 1033 | !IS_POWEROFTWO(tile_height)) { |
| 1034 | return -1; |
| 1035 | } |
| 1036 |
| 1037 | // Negative height means invert the image. |
| 1038 | if (height < 0) { |
| 1039 | height = -height; |
| 1040 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 1041 | dst_stride_y = -dst_stride_y; |
| 1042 | } |
| 1043 |
| 1044 | #if defined(HAS_DETILEROW_16_SSE2) |
| 1045 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1046 | DetileRow_16 = DetileRow_16_Any_SSE2; |
| 1047 | if (IS_ALIGNED(width, 16)) { |
| 1048 | DetileRow_16 = DetileRow_16_SSE2; |
| 1049 | } |
| 1050 | } |
| 1051 | #endif |
| 1052 | #if defined(HAS_DETILEROW_16_AVX) |
| 1053 | if (TestCpuFlag(kCpuHasAVX)) { |
| 1054 | DetileRow_16 = DetileRow_16_Any_AVX; |
| 1055 | if (IS_ALIGNED(width, 16)) { |
| 1056 | DetileRow_16 = DetileRow_16_AVX; |
| 1057 | } |
| 1058 | } |
| 1059 | #endif |
| 1060 | #if defined(HAS_DETILEROW_16_NEON) |
| 1061 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1062 | DetileRow_16 = DetileRow_16_Any_NEON; |
| 1063 | if (IS_ALIGNED(width, 16)) { |
| 1064 | DetileRow_16 = DetileRow_16_NEON; |
| 1065 | } |
| 1066 | } |
| 1067 | #endif |
| 1068 |
| 1069 | // Detile plane |
| 1070 | for (y = 0; y < height; ++y) { |
| 1071 | DetileRow_16(src_y, src_tile_stride, dst_y, width); |
| 1072 | dst_y += dst_stride_y; |
| 1073 | src_y += 16; |
| 1074 | // Advance to next row of tiles. |
| 1075 | if ((y & (tile_height - 1)) == (tile_height - 1)) { |
| 1076 | src_y = src_y - src_tile_stride + src_stride_y * tile_height; |
| 1077 | } |
| 1078 | } |
| 1079 | return 0; |
| 1080 | } |
| 1081 |
| 1082 | LIBYUV_API |
| 1083 | void DetileSplitUVPlane(const uint8_t* src_uv, |
| 1084 | int src_stride_uv, |
| 1085 | uint8_t* dst_u, |
| 1086 | int dst_stride_u, |
| 1087 | uint8_t* dst_v, |
| 1088 | int dst_stride_v, |
| 1089 | int width, |
| 1090 | int height, |
| 1091 | int tile_height) { |
| 1092 | const ptrdiff_t src_tile_stride = 16 * tile_height; |
| 1093 | int y; |
| 1094 | void (*DetileSplitUVRow)(const uint8_t* src, ptrdiff_t src_tile_stride, |
| 1095 | uint8_t* dst_u, uint8_t* dst_v, int width) = |
| 1096 | DetileSplitUVRow_C; |
| 1097 | assert(src_stride_uv >= 0); |
| 1098 | assert(tile_height > 0); |
| 1099 | assert(src_stride_uv > 0); |
| 1100 |
| 1101 | if (width <= 0 || height == 0) { |
| 1102 | return; |
| 1103 | } |
| 1104 | // Negative height means invert the image. |
| 1105 | if (height < 0) { |
| 1106 | height = -height; |
| 1107 | dst_u = dst_u + (height - 1) * dst_stride_u; |
| 1108 | dst_stride_u = -dst_stride_u; |
| 1109 | dst_v = dst_v + (height - 1) * dst_stride_v; |
| 1110 | dst_stride_v = -dst_stride_v; |
| 1111 | } |
| 1112 |
| 1113 | #if defined(HAS_DETILESPLITUVROW_SSSE3) |
| 1114 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1115 | DetileSplitUVRow = DetileSplitUVRow_Any_SSSE3; |
| 1116 | if (IS_ALIGNED(width, 16)) { |
| 1117 | DetileSplitUVRow = DetileSplitUVRow_SSSE3; |
| 1118 | } |
| 1119 | } |
| 1120 | #endif |
| 1121 | #if defined(HAS_DETILESPLITUVROW_NEON) |
| 1122 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1123 | DetileSplitUVRow = DetileSplitUVRow_Any_NEON; |
| 1124 | if (IS_ALIGNED(width, 16)) { |
| 1125 | DetileSplitUVRow = DetileSplitUVRow_NEON; |
| 1126 | } |
| 1127 | } |
| 1128 | #endif |
| 1129 |
| 1130 | // Detile plane |
| 1131 | for (y = 0; y < height; ++y) { |
| 1132 | DetileSplitUVRow(src_uv, src_tile_stride, dst_u, dst_v, width); |
| 1133 | dst_u += dst_stride_u; |
| 1134 | dst_v += dst_stride_v; |
| 1135 | src_uv += 16; |
| 1136 | // Advance to next row of tiles. |
| 1137 | if ((y & (tile_height - 1)) == (tile_height - 1)) { |
| 1138 | src_uv = src_uv - src_tile_stride + src_stride_uv * tile_height; |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 |
| 1143 | LIBYUV_API |
| 1144 | void DetileToYUY2(const uint8_t* src_y, |
| 1145 | int src_stride_y, |
| 1146 | const uint8_t* src_uv, |
| 1147 | int src_stride_uv, |
| 1148 | uint8_t* dst_yuy2, |
| 1149 | int dst_stride_yuy2, |
| 1150 | int width, |
| 1151 | int height, |
| 1152 | int tile_height) { |
| 1153 | const ptrdiff_t src_y_tile_stride = 16 * tile_height; |
| 1154 | const ptrdiff_t src_uv_tile_stride = src_y_tile_stride / 2; |
| 1155 | int y; |
| 1156 | void (*DetileToYUY2)(const uint8_t* src_y, ptrdiff_t src_y_tile_stride, |
| 1157 | const uint8_t* src_uv, ptrdiff_t src_uv_tile_stride, |
| 1158 | uint8_t* dst_yuy2, int width) = DetileToYUY2_C; |
| 1159 | assert(src_stride_y >= 0); |
| 1160 | assert(src_stride_y > 0); |
| 1161 | assert(src_stride_uv >= 0); |
| 1162 | assert(src_stride_uv > 0); |
| 1163 | assert(tile_height > 0); |
| 1164 |
| 1165 | if (width <= 0 || height == 0 || tile_height <= 0) { |
| 1166 | return; |
| 1167 | } |
| 1168 | // Negative height means invert the image. |
| 1169 | if (height < 0) { |
| 1170 | height = -height; |
| 1171 | dst_yuy2 = dst_yuy2 + (height - 1) * dst_stride_yuy2; |
| 1172 | dst_stride_yuy2 = -dst_stride_yuy2; |
| 1173 | } |
| 1174 |
| 1175 | #if defined(HAS_DETILETOYUY2_NEON) |
| 1176 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1177 | DetileToYUY2 = DetileToYUY2_Any_NEON; |
| 1178 | if (IS_ALIGNED(width, 16)) { |
| 1179 | DetileToYUY2 = DetileToYUY2_NEON; |
| 1180 | } |
| 1181 | } |
| 1182 | #endif |
| 1183 |
| 1184 | #if defined(HAS_DETILETOYUY2_SSE2) |
| 1185 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1186 | DetileToYUY2 = DetileToYUY2_Any_SSE2; |
| 1187 | if (IS_ALIGNED(width, 16)) { |
| 1188 | DetileToYUY2 = DetileToYUY2_SSE2; |
| 1189 | } |
| 1190 | } |
| 1191 | #endif |
| 1192 |
| 1193 | // Detile plane |
| 1194 | for (y = 0; y < height; ++y) { |
| 1195 | DetileToYUY2(src_y, src_y_tile_stride, src_uv, src_uv_tile_stride, dst_yuy2, |
| 1196 | width); |
| 1197 | dst_yuy2 += dst_stride_yuy2; |
| 1198 | src_y += 16; |
| 1199 |
| 1200 | if (y & 0x1) |
| 1201 | src_uv += 16; |
| 1202 |
| 1203 | // Advance to next row of tiles. |
| 1204 | if ((y & (tile_height - 1)) == (tile_height - 1)) { |
| 1205 | src_y = src_y - src_y_tile_stride + src_stride_y * tile_height; |
| 1206 | src_uv = src_uv - src_uv_tile_stride + src_stride_uv * (tile_height / 2); |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 |
| 1211 | // Support function for NV12 etc RGB channels. |
| 1212 | // Width and height are plane sizes (typically half pixel width). |
| 1213 | LIBYUV_API |
| 1214 | void SplitRGBPlane(const uint8_t* src_rgb, |
| 1215 | int src_stride_rgb, |
| 1216 | uint8_t* dst_r, |
| 1217 | int dst_stride_r, |
| 1218 | uint8_t* dst_g, |
| 1219 | int dst_stride_g, |
| 1220 | uint8_t* dst_b, |
| 1221 | int dst_stride_b, |
| 1222 | int width, |
| 1223 | int height) { |
| 1224 | int y; |
| 1225 | void (*SplitRGBRow)(const uint8_t* src_rgb, uint8_t* dst_r, uint8_t* dst_g, |
| 1226 | uint8_t* dst_b, int width) = SplitRGBRow_C; |
| 1227 | if (width <= 0 || height == 0) { |
| 1228 | return; |
| 1229 | } |
| 1230 | // Negative height means invert the image. |
| 1231 | if (height < 0) { |
| 1232 | height = -height; |
| 1233 | dst_r = dst_r + (height - 1) * dst_stride_r; |
| 1234 | dst_g = dst_g + (height - 1) * dst_stride_g; |
| 1235 | dst_b = dst_b + (height - 1) * dst_stride_b; |
| 1236 | dst_stride_r = -dst_stride_r; |
| 1237 | dst_stride_g = -dst_stride_g; |
| 1238 | dst_stride_b = -dst_stride_b; |
| 1239 | } |
| 1240 | // Coalesce rows. |
| 1241 | if (src_stride_rgb == width * 3 && dst_stride_r == width && |
| 1242 | dst_stride_g == width && dst_stride_b == width) { |
| 1243 | width *= height; |
| 1244 | height = 1; |
| 1245 | src_stride_rgb = dst_stride_r = dst_stride_g = dst_stride_b = 0; |
| 1246 | } |
| 1247 | #if defined(HAS_SPLITRGBROW_SSSE3) |
| 1248 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1249 | SplitRGBRow = SplitRGBRow_Any_SSSE3; |
| 1250 | if (IS_ALIGNED(width, 16)) { |
| 1251 | SplitRGBRow = SplitRGBRow_SSSE3; |
| 1252 | } |
| 1253 | } |
| 1254 | #endif |
| 1255 | #if defined(HAS_SPLITRGBROW_NEON) |
| 1256 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1257 | SplitRGBRow = SplitRGBRow_Any_NEON; |
| 1258 | if (IS_ALIGNED(width, 16)) { |
| 1259 | SplitRGBRow = SplitRGBRow_NEON; |
| 1260 | } |
| 1261 | } |
| 1262 | #endif |
| 1263 |
| 1264 | for (y = 0; y < height; ++y) { |
| 1265 | // Copy a row of RGB. |
| 1266 | SplitRGBRow(src_rgb, dst_r, dst_g, dst_b, width); |
| 1267 | dst_r += dst_stride_r; |
| 1268 | dst_g += dst_stride_g; |
| 1269 | dst_b += dst_stride_b; |
| 1270 | src_rgb += src_stride_rgb; |
| 1271 | } |
| 1272 | } |
| 1273 |
| 1274 | LIBYUV_API |
| 1275 | void MergeRGBPlane(const uint8_t* src_r, |
| 1276 | int src_stride_r, |
| 1277 | const uint8_t* src_g, |
| 1278 | int src_stride_g, |
| 1279 | const uint8_t* src_b, |
| 1280 | int src_stride_b, |
| 1281 | uint8_t* dst_rgb, |
| 1282 | int dst_stride_rgb, |
| 1283 | int width, |
| 1284 | int height) { |
| 1285 | int y; |
| 1286 | void (*MergeRGBRow)(const uint8_t* src_r, const uint8_t* src_g, |
| 1287 | const uint8_t* src_b, uint8_t* dst_rgb, int width) = |
| 1288 | MergeRGBRow_C; |
| 1289 | if (width <= 0 || height == 0) { |
| 1290 | return; |
| 1291 | } |
| 1292 | // Coalesce rows. |
| 1293 | // Negative height means invert the image. |
| 1294 | if (height < 0) { |
| 1295 | height = -height; |
| 1296 | dst_rgb = dst_rgb + (height - 1) * dst_stride_rgb; |
| 1297 | dst_stride_rgb = -dst_stride_rgb; |
| 1298 | } |
| 1299 | // Coalesce rows. |
| 1300 | if (src_stride_r == width && src_stride_g == width && src_stride_b == width && |
| 1301 | dst_stride_rgb == width * 3) { |
| 1302 | width *= height; |
| 1303 | height = 1; |
| 1304 | src_stride_r = src_stride_g = src_stride_b = dst_stride_rgb = 0; |
| 1305 | } |
| 1306 | #if defined(HAS_MERGERGBROW_SSSE3) |
| 1307 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1308 | MergeRGBRow = MergeRGBRow_Any_SSSE3; |
| 1309 | if (IS_ALIGNED(width, 16)) { |
| 1310 | MergeRGBRow = MergeRGBRow_SSSE3; |
| 1311 | } |
| 1312 | } |
| 1313 | #endif |
| 1314 | #if defined(HAS_MERGERGBROW_NEON) |
| 1315 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1316 | MergeRGBRow = MergeRGBRow_Any_NEON; |
| 1317 | if (IS_ALIGNED(width, 16)) { |
| 1318 | MergeRGBRow = MergeRGBRow_NEON; |
| 1319 | } |
| 1320 | } |
| 1321 | #endif |
| 1322 |
| 1323 | for (y = 0; y < height; ++y) { |
| 1324 | // Merge a row of U and V into a row of RGB. |
| 1325 | MergeRGBRow(src_r, src_g, src_b, dst_rgb, width); |
| 1326 | src_r += src_stride_r; |
| 1327 | src_g += src_stride_g; |
| 1328 | src_b += src_stride_b; |
| 1329 | dst_rgb += dst_stride_rgb; |
| 1330 | } |
| 1331 | } |
| 1332 |
| 1333 | LIBYUV_NOINLINE |
| 1334 | static void SplitARGBPlaneAlpha(const uint8_t* src_argb, |
| 1335 | int src_stride_argb, |
| 1336 | uint8_t* dst_r, |
| 1337 | int dst_stride_r, |
| 1338 | uint8_t* dst_g, |
| 1339 | int dst_stride_g, |
| 1340 | uint8_t* dst_b, |
| 1341 | int dst_stride_b, |
| 1342 | uint8_t* dst_a, |
| 1343 | int dst_stride_a, |
| 1344 | int width, |
| 1345 | int height) { |
| 1346 | int y; |
| 1347 | void (*SplitARGBRow)(const uint8_t* src_rgb, uint8_t* dst_r, uint8_t* dst_g, |
| 1348 | uint8_t* dst_b, uint8_t* dst_a, int width) = |
| 1349 | SplitARGBRow_C; |
| 1350 |
| 1351 | assert(height > 0); |
| 1352 |
| 1353 | if (src_stride_argb == width * 4 && dst_stride_r == width && |
| 1354 | dst_stride_g == width && dst_stride_b == width && dst_stride_a == width) { |
| 1355 | width *= height; |
| 1356 | height = 1; |
| 1357 | src_stride_argb = dst_stride_r = dst_stride_g = dst_stride_b = |
| 1358 | dst_stride_a = 0; |
| 1359 | } |
| 1360 |
| 1361 | #if defined(HAS_SPLITARGBROW_SSE2) |
| 1362 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1363 | SplitARGBRow = SplitARGBRow_Any_SSE2; |
| 1364 | if (IS_ALIGNED(width, 8)) { |
| 1365 | SplitARGBRow = SplitARGBRow_SSE2; |
| 1366 | } |
| 1367 | } |
| 1368 | #endif |
| 1369 | #if defined(HAS_SPLITARGBROW_SSSE3) |
| 1370 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1371 | SplitARGBRow = SplitARGBRow_Any_SSSE3; |
| 1372 | if (IS_ALIGNED(width, 8)) { |
| 1373 | SplitARGBRow = SplitARGBRow_SSSE3; |
| 1374 | } |
| 1375 | } |
| 1376 | #endif |
| 1377 | #if defined(HAS_SPLITARGBROW_AVX2) |
| 1378 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1379 | SplitARGBRow = SplitARGBRow_Any_AVX2; |
| 1380 | if (IS_ALIGNED(width, 16)) { |
| 1381 | SplitARGBRow = SplitARGBRow_AVX2; |
| 1382 | } |
| 1383 | } |
| 1384 | #endif |
| 1385 | #if defined(HAS_SPLITARGBROW_NEON) |
| 1386 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1387 | SplitARGBRow = SplitARGBRow_Any_NEON; |
| 1388 | if (IS_ALIGNED(width, 16)) { |
| 1389 | SplitARGBRow = SplitARGBRow_NEON; |
| 1390 | } |
| 1391 | } |
| 1392 | #endif |
| 1393 |
| 1394 | for (y = 0; y < height; ++y) { |
| 1395 | SplitARGBRow(src_argb, dst_r, dst_g, dst_b, dst_a, width); |
| 1396 | dst_r += dst_stride_r; |
| 1397 | dst_g += dst_stride_g; |
| 1398 | dst_b += dst_stride_b; |
| 1399 | dst_a += dst_stride_a; |
| 1400 | src_argb += src_stride_argb; |
| 1401 | } |
| 1402 | } |
| 1403 |
| 1404 | LIBYUV_NOINLINE |
| 1405 | static void SplitARGBPlaneOpaque(const uint8_t* src_argb, |
| 1406 | int src_stride_argb, |
| 1407 | uint8_t* dst_r, |
| 1408 | int dst_stride_r, |
| 1409 | uint8_t* dst_g, |
| 1410 | int dst_stride_g, |
| 1411 | uint8_t* dst_b, |
| 1412 | int dst_stride_b, |
| 1413 | int width, |
| 1414 | int height) { |
| 1415 | int y; |
| 1416 | void (*SplitXRGBRow)(const uint8_t* src_rgb, uint8_t* dst_r, uint8_t* dst_g, |
| 1417 | uint8_t* dst_b, int width) = SplitXRGBRow_C; |
| 1418 | assert(height > 0); |
| 1419 |
| 1420 | if (src_stride_argb == width * 4 && dst_stride_r == width && |
| 1421 | dst_stride_g == width && dst_stride_b == width) { |
| 1422 | width *= height; |
| 1423 | height = 1; |
| 1424 | src_stride_argb = dst_stride_r = dst_stride_g = dst_stride_b = 0; |
| 1425 | } |
| 1426 |
| 1427 | #if defined(HAS_SPLITXRGBROW_SSE2) |
| 1428 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1429 | SplitXRGBRow = SplitXRGBRow_Any_SSE2; |
| 1430 | if (IS_ALIGNED(width, 8)) { |
| 1431 | SplitXRGBRow = SplitXRGBRow_SSE2; |
| 1432 | } |
| 1433 | } |
| 1434 | #endif |
| 1435 | #if defined(HAS_SPLITXRGBROW_SSSE3) |
| 1436 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1437 | SplitXRGBRow = SplitXRGBRow_Any_SSSE3; |
| 1438 | if (IS_ALIGNED(width, 8)) { |
| 1439 | SplitXRGBRow = SplitXRGBRow_SSSE3; |
| 1440 | } |
| 1441 | } |
| 1442 | #endif |
| 1443 | #if defined(HAS_SPLITXRGBROW_AVX2) |
| 1444 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1445 | SplitXRGBRow = SplitXRGBRow_Any_AVX2; |
| 1446 | if (IS_ALIGNED(width, 16)) { |
| 1447 | SplitXRGBRow = SplitXRGBRow_AVX2; |
| 1448 | } |
| 1449 | } |
| 1450 | #endif |
| 1451 | #if defined(HAS_SPLITXRGBROW_NEON) |
| 1452 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1453 | SplitXRGBRow = SplitXRGBRow_Any_NEON; |
| 1454 | if (IS_ALIGNED(width, 16)) { |
| 1455 | SplitXRGBRow = SplitXRGBRow_NEON; |
| 1456 | } |
| 1457 | } |
| 1458 | #endif |
| 1459 |
| 1460 | for (y = 0; y < height; ++y) { |
| 1461 | SplitXRGBRow(src_argb, dst_r, dst_g, dst_b, width); |
| 1462 | dst_r += dst_stride_r; |
| 1463 | dst_g += dst_stride_g; |
| 1464 | dst_b += dst_stride_b; |
| 1465 | src_argb += src_stride_argb; |
| 1466 | } |
| 1467 | } |
| 1468 |
| 1469 | LIBYUV_API |
| 1470 | void SplitARGBPlane(const uint8_t* src_argb, |
| 1471 | int src_stride_argb, |
| 1472 | uint8_t* dst_r, |
| 1473 | int dst_stride_r, |
| 1474 | uint8_t* dst_g, |
| 1475 | int dst_stride_g, |
| 1476 | uint8_t* dst_b, |
| 1477 | int dst_stride_b, |
| 1478 | uint8_t* dst_a, |
| 1479 | int dst_stride_a, |
| 1480 | int width, |
| 1481 | int height) { |
| 1482 | // Negative height means invert the image. |
| 1483 | if (height < 0) { |
| 1484 | height = -height; |
| 1485 | dst_r = dst_r + (height - 1) * dst_stride_r; |
| 1486 | dst_g = dst_g + (height - 1) * dst_stride_g; |
| 1487 | dst_b = dst_b + (height - 1) * dst_stride_b; |
| 1488 | dst_a = dst_a + (height - 1) * dst_stride_a; |
| 1489 | dst_stride_r = -dst_stride_r; |
| 1490 | dst_stride_g = -dst_stride_g; |
| 1491 | dst_stride_b = -dst_stride_b; |
| 1492 | dst_stride_a = -dst_stride_a; |
| 1493 | } |
| 1494 |
| 1495 | if (dst_a == NULL) { |
| 1496 | SplitARGBPlaneOpaque(src_argb, src_stride_argb, dst_r, dst_stride_r, dst_g, |
| 1497 | dst_stride_g, dst_b, dst_stride_b, width, height); |
| 1498 | } else { |
| 1499 | SplitARGBPlaneAlpha(src_argb, src_stride_argb, dst_r, dst_stride_r, dst_g, |
| 1500 | dst_stride_g, dst_b, dst_stride_b, dst_a, dst_stride_a, |
| 1501 | width, height); |
| 1502 | } |
| 1503 | } |
| 1504 |
| 1505 | LIBYUV_NOINLINE |
| 1506 | static void MergeARGBPlaneAlpha(const uint8_t* src_r, |
| 1507 | int src_stride_r, |
| 1508 | const uint8_t* src_g, |
| 1509 | int src_stride_g, |
| 1510 | const uint8_t* src_b, |
| 1511 | int src_stride_b, |
| 1512 | const uint8_t* src_a, |
| 1513 | int src_stride_a, |
| 1514 | uint8_t* dst_argb, |
| 1515 | int dst_stride_argb, |
| 1516 | int width, |
| 1517 | int height) { |
| 1518 | int y; |
| 1519 | void (*MergeARGBRow)(const uint8_t* src_r, const uint8_t* src_g, |
| 1520 | const uint8_t* src_b, const uint8_t* src_a, |
| 1521 | uint8_t* dst_argb, int width) = MergeARGBRow_C; |
| 1522 |
| 1523 | assert(height > 0); |
| 1524 |
| 1525 | if (src_stride_r == width && src_stride_g == width && src_stride_b == width && |
| 1526 | src_stride_a == width && dst_stride_argb == width * 4) { |
| 1527 | width *= height; |
| 1528 | height = 1; |
| 1529 | src_stride_r = src_stride_g = src_stride_b = src_stride_a = |
| 1530 | dst_stride_argb = 0; |
| 1531 | } |
| 1532 | #if defined(HAS_MERGEARGBROW_SSE2) |
| 1533 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1534 | MergeARGBRow = MergeARGBRow_Any_SSE2; |
| 1535 | if (IS_ALIGNED(width, 8)) { |
| 1536 | MergeARGBRow = MergeARGBRow_SSE2; |
| 1537 | } |
| 1538 | } |
| 1539 | #endif |
| 1540 | #if defined(HAS_MERGEARGBROW_AVX2) |
| 1541 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1542 | MergeARGBRow = MergeARGBRow_Any_AVX2; |
| 1543 | if (IS_ALIGNED(width, 16)) { |
| 1544 | MergeARGBRow = MergeARGBRow_AVX2; |
| 1545 | } |
| 1546 | } |
| 1547 | #endif |
| 1548 | #if defined(HAS_MERGEARGBROW_NEON) |
| 1549 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1550 | MergeARGBRow = MergeARGBRow_Any_NEON; |
| 1551 | if (IS_ALIGNED(width, 16)) { |
| 1552 | MergeARGBRow = MergeARGBRow_NEON; |
| 1553 | } |
| 1554 | } |
| 1555 | #endif |
| 1556 |
| 1557 | for (y = 0; y < height; ++y) { |
| 1558 | MergeARGBRow(src_r, src_g, src_b, src_a, dst_argb, width); |
| 1559 | src_r += src_stride_r; |
| 1560 | src_g += src_stride_g; |
| 1561 | src_b += src_stride_b; |
| 1562 | src_a += src_stride_a; |
| 1563 | dst_argb += dst_stride_argb; |
| 1564 | } |
| 1565 | } |
| 1566 |
| 1567 | LIBYUV_NOINLINE |
| 1568 | static void MergeARGBPlaneOpaque(const uint8_t* src_r, |
| 1569 | int src_stride_r, |
| 1570 | const uint8_t* src_g, |
| 1571 | int src_stride_g, |
| 1572 | const uint8_t* src_b, |
| 1573 | int src_stride_b, |
| 1574 | uint8_t* dst_argb, |
| 1575 | int dst_stride_argb, |
| 1576 | int width, |
| 1577 | int height) { |
| 1578 | int y; |
| 1579 | void (*MergeXRGBRow)(const uint8_t* src_r, const uint8_t* src_g, |
| 1580 | const uint8_t* src_b, uint8_t* dst_argb, int width) = |
| 1581 | MergeXRGBRow_C; |
| 1582 |
| 1583 | assert(height > 0); |
| 1584 |
| 1585 | if (src_stride_r == width && src_stride_g == width && src_stride_b == width && |
| 1586 | dst_stride_argb == width * 4) { |
| 1587 | width *= height; |
| 1588 | height = 1; |
| 1589 | src_stride_r = src_stride_g = src_stride_b = dst_stride_argb = 0; |
| 1590 | } |
| 1591 | #if defined(HAS_MERGEXRGBROW_SSE2) |
| 1592 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1593 | MergeXRGBRow = MergeXRGBRow_Any_SSE2; |
| 1594 | if (IS_ALIGNED(width, 8)) { |
| 1595 | MergeXRGBRow = MergeXRGBRow_SSE2; |
| 1596 | } |
| 1597 | } |
| 1598 | #endif |
| 1599 | #if defined(HAS_MERGEXRGBROW_AVX2) |
| 1600 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1601 | MergeXRGBRow = MergeXRGBRow_Any_AVX2; |
| 1602 | if (IS_ALIGNED(width, 16)) { |
| 1603 | MergeXRGBRow = MergeXRGBRow_AVX2; |
| 1604 | } |
| 1605 | } |
| 1606 | #endif |
| 1607 | #if defined(HAS_MERGEXRGBROW_NEON) |
| 1608 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1609 | MergeXRGBRow = MergeXRGBRow_Any_NEON; |
| 1610 | if (IS_ALIGNED(width, 16)) { |
| 1611 | MergeXRGBRow = MergeXRGBRow_NEON; |
| 1612 | } |
| 1613 | } |
| 1614 | #endif |
| 1615 |
| 1616 | for (y = 0; y < height; ++y) { |
| 1617 | MergeXRGBRow(src_r, src_g, src_b, dst_argb, width); |
| 1618 | src_r += src_stride_r; |
| 1619 | src_g += src_stride_g; |
| 1620 | src_b += src_stride_b; |
| 1621 | dst_argb += dst_stride_argb; |
| 1622 | } |
| 1623 | } |
| 1624 |
| 1625 | LIBYUV_API |
| 1626 | void MergeARGBPlane(const uint8_t* src_r, |
| 1627 | int src_stride_r, |
| 1628 | const uint8_t* src_g, |
| 1629 | int src_stride_g, |
| 1630 | const uint8_t* src_b, |
| 1631 | int src_stride_b, |
| 1632 | const uint8_t* src_a, |
| 1633 | int src_stride_a, |
| 1634 | uint8_t* dst_argb, |
| 1635 | int dst_stride_argb, |
| 1636 | int width, |
| 1637 | int height) { |
| 1638 | // Negative height means invert the image. |
| 1639 | if (height < 0) { |
| 1640 | height = -height; |
| 1641 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 1642 | dst_stride_argb = -dst_stride_argb; |
| 1643 | } |
| 1644 |
| 1645 | if (src_a == NULL) { |
| 1646 | MergeARGBPlaneOpaque(src_r, src_stride_r, src_g, src_stride_g, src_b, |
| 1647 | src_stride_b, dst_argb, dst_stride_argb, width, |
| 1648 | height); |
| 1649 | } else { |
| 1650 | MergeARGBPlaneAlpha(src_r, src_stride_r, src_g, src_stride_g, src_b, |
| 1651 | src_stride_b, src_a, src_stride_a, dst_argb, |
| 1652 | dst_stride_argb, width, height); |
| 1653 | } |
| 1654 | } |
| 1655 |
| 1656 | // TODO(yuan): Support 2 bit alpha channel. |
| 1657 | LIBYUV_API |
| 1658 | void MergeXR30Plane(const uint16_t* src_r, |
| 1659 | int src_stride_r, |
| 1660 | const uint16_t* src_g, |
| 1661 | int src_stride_g, |
| 1662 | const uint16_t* src_b, |
| 1663 | int src_stride_b, |
| 1664 | uint8_t* dst_ar30, |
| 1665 | int dst_stride_ar30, |
| 1666 | int width, |
| 1667 | int height, |
| 1668 | int depth) { |
| 1669 | int y; |
| 1670 | void (*MergeXR30Row)(const uint16_t* src_r, const uint16_t* src_g, |
| 1671 | const uint16_t* src_b, uint8_t* dst_ar30, int depth, |
| 1672 | int width) = MergeXR30Row_C; |
| 1673 |
| 1674 | // Negative height means invert the image. |
| 1675 | if (height < 0) { |
| 1676 | height = -height; |
| 1677 | dst_ar30 = dst_ar30 + (height - 1) * dst_stride_ar30; |
| 1678 | dst_stride_ar30 = -dst_stride_ar30; |
| 1679 | } |
| 1680 | // Coalesce rows. |
| 1681 | if (src_stride_r == width && src_stride_g == width && src_stride_b == width && |
| 1682 | dst_stride_ar30 == width * 4) { |
| 1683 | width *= height; |
| 1684 | height = 1; |
| 1685 | src_stride_r = src_stride_g = src_stride_b = dst_stride_ar30 = 0; |
| 1686 | } |
| 1687 | #if defined(HAS_MERGEXR30ROW_AVX2) |
| 1688 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1689 | MergeXR30Row = MergeXR30Row_Any_AVX2; |
| 1690 | if (IS_ALIGNED(width, 16)) { |
| 1691 | MergeXR30Row = MergeXR30Row_AVX2; |
| 1692 | } |
| 1693 | } |
| 1694 | #endif |
| 1695 | #if defined(HAS_MERGEXR30ROW_NEON) |
| 1696 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1697 | if (depth == 10) { |
| 1698 | MergeXR30Row = MergeXR30Row_10_Any_NEON; |
| 1699 | if (IS_ALIGNED(width, 8)) { |
| 1700 | MergeXR30Row = MergeXR30Row_10_NEON; |
| 1701 | } |
| 1702 | } else { |
| 1703 | MergeXR30Row = MergeXR30Row_Any_NEON; |
| 1704 | if (IS_ALIGNED(width, 8)) { |
| 1705 | MergeXR30Row = MergeXR30Row_NEON; |
| 1706 | } |
| 1707 | } |
| 1708 | } |
| 1709 | #endif |
| 1710 |
| 1711 | for (y = 0; y < height; ++y) { |
| 1712 | MergeXR30Row(src_r, src_g, src_b, dst_ar30, depth, width); |
| 1713 | src_r += src_stride_r; |
| 1714 | src_g += src_stride_g; |
| 1715 | src_b += src_stride_b; |
| 1716 | dst_ar30 += dst_stride_ar30; |
| 1717 | } |
| 1718 | } |
| 1719 |
| 1720 | LIBYUV_NOINLINE |
| 1721 | static void MergeAR64PlaneAlpha(const uint16_t* src_r, |
| 1722 | int src_stride_r, |
| 1723 | const uint16_t* src_g, |
| 1724 | int src_stride_g, |
| 1725 | const uint16_t* src_b, |
| 1726 | int src_stride_b, |
| 1727 | const uint16_t* src_a, |
| 1728 | int src_stride_a, |
| 1729 | uint16_t* dst_ar64, |
| 1730 | int dst_stride_ar64, |
| 1731 | int width, |
| 1732 | int height, |
| 1733 | int depth) { |
| 1734 | int y; |
| 1735 | void (*MergeAR64Row)(const uint16_t* src_r, const uint16_t* src_g, |
| 1736 | const uint16_t* src_b, const uint16_t* src_a, |
| 1737 | uint16_t* dst_argb, int depth, int width) = |
| 1738 | MergeAR64Row_C; |
| 1739 |
| 1740 | if (src_stride_r == width && src_stride_g == width && src_stride_b == width && |
| 1741 | src_stride_a == width && dst_stride_ar64 == width * 4) { |
| 1742 | width *= height; |
| 1743 | height = 1; |
| 1744 | src_stride_r = src_stride_g = src_stride_b = src_stride_a = |
| 1745 | dst_stride_ar64 = 0; |
| 1746 | } |
| 1747 | #if defined(HAS_MERGEAR64ROW_AVX2) |
| 1748 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1749 | MergeAR64Row = MergeAR64Row_Any_AVX2; |
| 1750 | if (IS_ALIGNED(width, 16)) { |
| 1751 | MergeAR64Row = MergeAR64Row_AVX2; |
| 1752 | } |
| 1753 | } |
| 1754 | #endif |
| 1755 | #if defined(HAS_MERGEAR64ROW_NEON) |
| 1756 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1757 | MergeAR64Row = MergeAR64Row_Any_NEON; |
| 1758 | if (IS_ALIGNED(width, 8)) { |
| 1759 | MergeAR64Row = MergeAR64Row_NEON; |
| 1760 | } |
| 1761 | } |
| 1762 | #endif |
| 1763 |
| 1764 | for (y = 0; y < height; ++y) { |
| 1765 | MergeAR64Row(src_r, src_g, src_b, src_a, dst_ar64, depth, width); |
| 1766 | src_r += src_stride_r; |
| 1767 | src_g += src_stride_g; |
| 1768 | src_b += src_stride_b; |
| 1769 | src_a += src_stride_a; |
| 1770 | dst_ar64 += dst_stride_ar64; |
| 1771 | } |
| 1772 | } |
| 1773 |
| 1774 | LIBYUV_NOINLINE |
| 1775 | static void MergeAR64PlaneOpaque(const uint16_t* src_r, |
| 1776 | int src_stride_r, |
| 1777 | const uint16_t* src_g, |
| 1778 | int src_stride_g, |
| 1779 | const uint16_t* src_b, |
| 1780 | int src_stride_b, |
| 1781 | uint16_t* dst_ar64, |
| 1782 | int dst_stride_ar64, |
| 1783 | int width, |
| 1784 | int height, |
| 1785 | int depth) { |
| 1786 | int y; |
| 1787 | void (*MergeXR64Row)(const uint16_t* src_r, const uint16_t* src_g, |
| 1788 | const uint16_t* src_b, uint16_t* dst_argb, int depth, |
| 1789 | int width) = MergeXR64Row_C; |
| 1790 |
| 1791 | // Coalesce rows. |
| 1792 | if (src_stride_r == width && src_stride_g == width && src_stride_b == width && |
| 1793 | dst_stride_ar64 == width * 4) { |
| 1794 | width *= height; |
| 1795 | height = 1; |
| 1796 | src_stride_r = src_stride_g = src_stride_b = dst_stride_ar64 = 0; |
| 1797 | } |
| 1798 | #if defined(HAS_MERGEXR64ROW_AVX2) |
| 1799 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1800 | MergeXR64Row = MergeXR64Row_Any_AVX2; |
| 1801 | if (IS_ALIGNED(width, 16)) { |
| 1802 | MergeXR64Row = MergeXR64Row_AVX2; |
| 1803 | } |
| 1804 | } |
| 1805 | #endif |
| 1806 | #if defined(HAS_MERGEXR64ROW_NEON) |
| 1807 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1808 | MergeXR64Row = MergeXR64Row_Any_NEON; |
| 1809 | if (IS_ALIGNED(width, 8)) { |
| 1810 | MergeXR64Row = MergeXR64Row_NEON; |
| 1811 | } |
| 1812 | } |
| 1813 | #endif |
| 1814 |
| 1815 | for (y = 0; y < height; ++y) { |
| 1816 | MergeXR64Row(src_r, src_g, src_b, dst_ar64, depth, width); |
| 1817 | src_r += src_stride_r; |
| 1818 | src_g += src_stride_g; |
| 1819 | src_b += src_stride_b; |
| 1820 | dst_ar64 += dst_stride_ar64; |
| 1821 | } |
| 1822 | } |
| 1823 |
| 1824 | LIBYUV_API |
| 1825 | void MergeAR64Plane(const uint16_t* src_r, |
| 1826 | int src_stride_r, |
| 1827 | const uint16_t* src_g, |
| 1828 | int src_stride_g, |
| 1829 | const uint16_t* src_b, |
| 1830 | int src_stride_b, |
| 1831 | const uint16_t* src_a, |
| 1832 | int src_stride_a, |
| 1833 | uint16_t* dst_ar64, |
| 1834 | int dst_stride_ar64, |
| 1835 | int width, |
| 1836 | int height, |
| 1837 | int depth) { |
| 1838 | // Negative height means invert the image. |
| 1839 | if (height < 0) { |
| 1840 | height = -height; |
| 1841 | dst_ar64 = dst_ar64 + (height - 1) * dst_stride_ar64; |
| 1842 | dst_stride_ar64 = -dst_stride_ar64; |
| 1843 | } |
| 1844 |
| 1845 | if (src_a == NULL) { |
| 1846 | MergeAR64PlaneOpaque(src_r, src_stride_r, src_g, src_stride_g, src_b, |
| 1847 | src_stride_b, dst_ar64, dst_stride_ar64, width, height, |
| 1848 | depth); |
| 1849 | } else { |
| 1850 | MergeAR64PlaneAlpha(src_r, src_stride_r, src_g, src_stride_g, src_b, |
| 1851 | src_stride_b, src_a, src_stride_a, dst_ar64, |
| 1852 | dst_stride_ar64, width, height, depth); |
| 1853 | } |
| 1854 | } |
| 1855 |
| 1856 | LIBYUV_NOINLINE |
| 1857 | static void MergeARGB16To8PlaneAlpha(const uint16_t* src_r, |
| 1858 | int src_stride_r, |
| 1859 | const uint16_t* src_g, |
| 1860 | int src_stride_g, |
| 1861 | const uint16_t* src_b, |
| 1862 | int src_stride_b, |
| 1863 | const uint16_t* src_a, |
| 1864 | int src_stride_a, |
| 1865 | uint8_t* dst_argb, |
| 1866 | int dst_stride_argb, |
| 1867 | int width, |
| 1868 | int height, |
| 1869 | int depth) { |
| 1870 | int y; |
| 1871 | void (*MergeARGB16To8Row)(const uint16_t* src_r, const uint16_t* src_g, |
| 1872 | const uint16_t* src_b, const uint16_t* src_a, |
| 1873 | uint8_t* dst_argb, int depth, int width) = |
| 1874 | MergeARGB16To8Row_C; |
| 1875 |
| 1876 | if (src_stride_r == width && src_stride_g == width && src_stride_b == width && |
| 1877 | src_stride_a == width && dst_stride_argb == width * 4) { |
| 1878 | width *= height; |
| 1879 | height = 1; |
| 1880 | src_stride_r = src_stride_g = src_stride_b = src_stride_a = |
| 1881 | dst_stride_argb = 0; |
| 1882 | } |
| 1883 | #if defined(HAS_MERGEARGB16TO8ROW_AVX2) |
| 1884 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1885 | MergeARGB16To8Row = MergeARGB16To8Row_Any_AVX2; |
| 1886 | if (IS_ALIGNED(width, 16)) { |
| 1887 | MergeARGB16To8Row = MergeARGB16To8Row_AVX2; |
| 1888 | } |
| 1889 | } |
| 1890 | #endif |
| 1891 | #if defined(HAS_MERGEARGB16TO8ROW_NEON) |
| 1892 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1893 | MergeARGB16To8Row = MergeARGB16To8Row_Any_NEON; |
| 1894 | if (IS_ALIGNED(width, 8)) { |
| 1895 | MergeARGB16To8Row = MergeARGB16To8Row_NEON; |
| 1896 | } |
| 1897 | } |
| 1898 | #endif |
| 1899 |
| 1900 | for (y = 0; y < height; ++y) { |
| 1901 | MergeARGB16To8Row(src_r, src_g, src_b, src_a, dst_argb, depth, width); |
| 1902 | src_r += src_stride_r; |
| 1903 | src_g += src_stride_g; |
| 1904 | src_b += src_stride_b; |
| 1905 | src_a += src_stride_a; |
| 1906 | dst_argb += dst_stride_argb; |
| 1907 | } |
| 1908 | } |
| 1909 |
| 1910 | LIBYUV_NOINLINE |
| 1911 | static void MergeARGB16To8PlaneOpaque(const uint16_t* src_r, |
| 1912 | int src_stride_r, |
| 1913 | const uint16_t* src_g, |
| 1914 | int src_stride_g, |
| 1915 | const uint16_t* src_b, |
| 1916 | int src_stride_b, |
| 1917 | uint8_t* dst_argb, |
| 1918 | int dst_stride_argb, |
| 1919 | int width, |
| 1920 | int height, |
| 1921 | int depth) { |
| 1922 | int y; |
| 1923 | void (*MergeXRGB16To8Row)(const uint16_t* src_r, const uint16_t* src_g, |
| 1924 | const uint16_t* src_b, uint8_t* dst_argb, int depth, |
| 1925 | int width) = MergeXRGB16To8Row_C; |
| 1926 |
| 1927 | // Coalesce rows. |
| 1928 | if (src_stride_r == width && src_stride_g == width && src_stride_b == width && |
| 1929 | dst_stride_argb == width * 4) { |
| 1930 | width *= height; |
| 1931 | height = 1; |
| 1932 | src_stride_r = src_stride_g = src_stride_b = dst_stride_argb = 0; |
| 1933 | } |
| 1934 | #if defined(HAS_MERGEXRGB16TO8ROW_AVX2) |
| 1935 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1936 | MergeXRGB16To8Row = MergeXRGB16To8Row_Any_AVX2; |
| 1937 | if (IS_ALIGNED(width, 16)) { |
| 1938 | MergeXRGB16To8Row = MergeXRGB16To8Row_AVX2; |
| 1939 | } |
| 1940 | } |
| 1941 | #endif |
| 1942 | #if defined(HAS_MERGEXRGB16TO8ROW_NEON) |
| 1943 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1944 | MergeXRGB16To8Row = MergeXRGB16To8Row_Any_NEON; |
| 1945 | if (IS_ALIGNED(width, 8)) { |
| 1946 | MergeXRGB16To8Row = MergeXRGB16To8Row_NEON; |
| 1947 | } |
| 1948 | } |
| 1949 | #endif |
| 1950 |
| 1951 | for (y = 0; y < height; ++y) { |
| 1952 | MergeXRGB16To8Row(src_r, src_g, src_b, dst_argb, depth, width); |
| 1953 | src_r += src_stride_r; |
| 1954 | src_g += src_stride_g; |
| 1955 | src_b += src_stride_b; |
| 1956 | dst_argb += dst_stride_argb; |
| 1957 | } |
| 1958 | } |
| 1959 |
| 1960 | LIBYUV_API |
| 1961 | void MergeARGB16To8Plane(const uint16_t* src_r, |
| 1962 | int src_stride_r, |
| 1963 | const uint16_t* src_g, |
| 1964 | int src_stride_g, |
| 1965 | const uint16_t* src_b, |
| 1966 | int src_stride_b, |
| 1967 | const uint16_t* src_a, |
| 1968 | int src_stride_a, |
| 1969 | uint8_t* dst_argb, |
| 1970 | int dst_stride_argb, |
| 1971 | int width, |
| 1972 | int height, |
| 1973 | int depth) { |
| 1974 | // Negative height means invert the image. |
| 1975 | if (height < 0) { |
| 1976 | height = -height; |
| 1977 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 1978 | dst_stride_argb = -dst_stride_argb; |
| 1979 | } |
| 1980 |
| 1981 | if (src_a == NULL) { |
| 1982 | MergeARGB16To8PlaneOpaque(src_r, src_stride_r, src_g, src_stride_g, src_b, |
| 1983 | src_stride_b, dst_argb, dst_stride_argb, width, |
| 1984 | height, depth); |
| 1985 | } else { |
| 1986 | MergeARGB16To8PlaneAlpha(src_r, src_stride_r, src_g, src_stride_g, src_b, |
| 1987 | src_stride_b, src_a, src_stride_a, dst_argb, |
| 1988 | dst_stride_argb, width, height, depth); |
| 1989 | } |
| 1990 | } |
| 1991 |
| 1992 | // Convert YUY2 to I422. |
| 1993 | LIBYUV_API |
| 1994 | int YUY2ToI422(const uint8_t* src_yuy2, |
| 1995 | int src_stride_yuy2, |
| 1996 | uint8_t* dst_y, |
| 1997 | int dst_stride_y, |
| 1998 | uint8_t* dst_u, |
| 1999 | int dst_stride_u, |
| 2000 | uint8_t* dst_v, |
| 2001 | int dst_stride_v, |
| 2002 | int width, |
| 2003 | int height) { |
| 2004 | int y; |
| 2005 | void (*YUY2ToUV422Row)(const uint8_t* src_yuy2, uint8_t* dst_u, |
| 2006 | uint8_t* dst_v, int width) = YUY2ToUV422Row_C; |
| 2007 | void (*YUY2ToYRow)(const uint8_t* src_yuy2, uint8_t* dst_y, int width) = |
| 2008 | YUY2ToYRow_C; |
| 2009 | if (!src_yuy2 || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 2010 | return -1; |
| 2011 | } |
| 2012 | // Negative height means invert the image. |
| 2013 | if (height < 0) { |
| 2014 | height = -height; |
| 2015 | src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2; |
| 2016 | src_stride_yuy2 = -src_stride_yuy2; |
| 2017 | } |
| 2018 | // Coalesce rows. |
| 2019 | if (src_stride_yuy2 == width * 2 && dst_stride_y == width && |
| 2020 | dst_stride_u * 2 == width && dst_stride_v * 2 == width && |
| 2021 | width * height <= 32768) { |
| 2022 | width *= height; |
| 2023 | height = 1; |
| 2024 | src_stride_yuy2 = dst_stride_y = dst_stride_u = dst_stride_v = 0; |
| 2025 | } |
| 2026 | #if defined(HAS_YUY2TOYROW_SSE2) |
| 2027 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2028 | YUY2ToUV422Row = YUY2ToUV422Row_Any_SSE2; |
| 2029 | YUY2ToYRow = YUY2ToYRow_Any_SSE2; |
| 2030 | if (IS_ALIGNED(width, 16)) { |
| 2031 | YUY2ToUV422Row = YUY2ToUV422Row_SSE2; |
| 2032 | YUY2ToYRow = YUY2ToYRow_SSE2; |
| 2033 | } |
| 2034 | } |
| 2035 | #endif |
| 2036 | #if defined(HAS_YUY2TOYROW_AVX2) |
| 2037 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2038 | YUY2ToUV422Row = YUY2ToUV422Row_Any_AVX2; |
| 2039 | YUY2ToYRow = YUY2ToYRow_Any_AVX2; |
| 2040 | if (IS_ALIGNED(width, 32)) { |
| 2041 | YUY2ToUV422Row = YUY2ToUV422Row_AVX2; |
| 2042 | YUY2ToYRow = YUY2ToYRow_AVX2; |
| 2043 | } |
| 2044 | } |
| 2045 | #endif |
| 2046 | #if defined(HAS_YUY2TOYROW_NEON) |
| 2047 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2048 | YUY2ToYRow = YUY2ToYRow_Any_NEON; |
| 2049 | YUY2ToUV422Row = YUY2ToUV422Row_Any_NEON; |
| 2050 | if (IS_ALIGNED(width, 16)) { |
| 2051 | YUY2ToYRow = YUY2ToYRow_NEON; |
| 2052 | YUY2ToUV422Row = YUY2ToUV422Row_NEON; |
| 2053 | } |
| 2054 | } |
| 2055 | #endif |
| 2056 | #if defined(HAS_YUY2TOYROW_MSA) && defined(HAS_YUY2TOUV422ROW_MSA) |
| 2057 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2058 | YUY2ToYRow = YUY2ToYRow_Any_MSA; |
| 2059 | YUY2ToUV422Row = YUY2ToUV422Row_Any_MSA; |
| 2060 | if (IS_ALIGNED(width, 32)) { |
| 2061 | YUY2ToYRow = YUY2ToYRow_MSA; |
| 2062 | YUY2ToUV422Row = YUY2ToUV422Row_MSA; |
| 2063 | } |
| 2064 | } |
| 2065 | #endif |
| 2066 | #if defined(HAS_YUY2TOYROW_LASX) && defined(HAS_YUY2TOUV422ROW_LASX) |
| 2067 | if (TestCpuFlag(kCpuHasLASX)) { |
| 2068 | YUY2ToYRow = YUY2ToYRow_Any_LASX; |
| 2069 | YUY2ToUV422Row = YUY2ToUV422Row_Any_LASX; |
| 2070 | if (IS_ALIGNED(width, 32)) { |
| 2071 | YUY2ToYRow = YUY2ToYRow_LASX; |
| 2072 | YUY2ToUV422Row = YUY2ToUV422Row_LASX; |
| 2073 | } |
| 2074 | } |
| 2075 | #endif |
| 2076 |
| 2077 | for (y = 0; y < height; ++y) { |
| 2078 | YUY2ToUV422Row(src_yuy2, dst_u, dst_v, width); |
| 2079 | YUY2ToYRow(src_yuy2, dst_y, width); |
| 2080 | src_yuy2 += src_stride_yuy2; |
| 2081 | dst_y += dst_stride_y; |
| 2082 | dst_u += dst_stride_u; |
| 2083 | dst_v += dst_stride_v; |
| 2084 | } |
| 2085 | return 0; |
| 2086 | } |
| 2087 |
| 2088 | // Convert UYVY to I422. |
| 2089 | LIBYUV_API |
| 2090 | int UYVYToI422(const uint8_t* src_uyvy, |
| 2091 | int src_stride_uyvy, |
| 2092 | uint8_t* dst_y, |
| 2093 | int dst_stride_y, |
| 2094 | uint8_t* dst_u, |
| 2095 | int dst_stride_u, |
| 2096 | uint8_t* dst_v, |
| 2097 | int dst_stride_v, |
| 2098 | int width, |
| 2099 | int height) { |
| 2100 | int y; |
| 2101 | void (*UYVYToUV422Row)(const uint8_t* src_uyvy, uint8_t* dst_u, |
| 2102 | uint8_t* dst_v, int width) = UYVYToUV422Row_C; |
| 2103 | void (*UYVYToYRow)(const uint8_t* src_uyvy, uint8_t* dst_y, int width) = |
| 2104 | UYVYToYRow_C; |
| 2105 | if (!src_uyvy || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 2106 | return -1; |
| 2107 | } |
| 2108 | // Negative height means invert the image. |
| 2109 | if (height < 0) { |
| 2110 | height = -height; |
| 2111 | src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy; |
| 2112 | src_stride_uyvy = -src_stride_uyvy; |
| 2113 | } |
| 2114 | // Coalesce rows. |
| 2115 | if (src_stride_uyvy == width * 2 && dst_stride_y == width && |
| 2116 | dst_stride_u * 2 == width && dst_stride_v * 2 == width && |
| 2117 | width * height <= 32768) { |
| 2118 | width *= height; |
| 2119 | height = 1; |
| 2120 | src_stride_uyvy = dst_stride_y = dst_stride_u = dst_stride_v = 0; |
| 2121 | } |
| 2122 | #if defined(HAS_UYVYTOYROW_SSE2) |
| 2123 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2124 | UYVYToUV422Row = UYVYToUV422Row_Any_SSE2; |
| 2125 | UYVYToYRow = UYVYToYRow_Any_SSE2; |
| 2126 | if (IS_ALIGNED(width, 16)) { |
| 2127 | UYVYToUV422Row = UYVYToUV422Row_SSE2; |
| 2128 | UYVYToYRow = UYVYToYRow_SSE2; |
| 2129 | } |
| 2130 | } |
| 2131 | #endif |
| 2132 | #if defined(HAS_UYVYTOYROW_AVX2) |
| 2133 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2134 | UYVYToUV422Row = UYVYToUV422Row_Any_AVX2; |
| 2135 | UYVYToYRow = UYVYToYRow_Any_AVX2; |
| 2136 | if (IS_ALIGNED(width, 32)) { |
| 2137 | UYVYToUV422Row = UYVYToUV422Row_AVX2; |
| 2138 | UYVYToYRow = UYVYToYRow_AVX2; |
| 2139 | } |
| 2140 | } |
| 2141 | #endif |
| 2142 | #if defined(HAS_UYVYTOYROW_NEON) |
| 2143 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2144 | UYVYToYRow = UYVYToYRow_Any_NEON; |
| 2145 | UYVYToUV422Row = UYVYToUV422Row_Any_NEON; |
| 2146 | if (IS_ALIGNED(width, 16)) { |
| 2147 | UYVYToYRow = UYVYToYRow_NEON; |
| 2148 | UYVYToUV422Row = UYVYToUV422Row_NEON; |
| 2149 | } |
| 2150 | } |
| 2151 | #endif |
| 2152 | #if defined(HAS_UYVYTOYROW_MSA) && defined(HAS_UYVYTOUV422ROW_MSA) |
| 2153 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2154 | UYVYToYRow = UYVYToYRow_Any_MSA; |
| 2155 | UYVYToUV422Row = UYVYToUV422Row_Any_MSA; |
| 2156 | if (IS_ALIGNED(width, 32)) { |
| 2157 | UYVYToYRow = UYVYToYRow_MSA; |
| 2158 | UYVYToUV422Row = UYVYToUV422Row_MSA; |
| 2159 | } |
| 2160 | } |
| 2161 | #endif |
| 2162 | #if defined(HAS_UYVYTOYROW_LASX) && defined(HAS_UYVYTOUV422ROW_LASX) |
| 2163 | if (TestCpuFlag(kCpuHasLASX)) { |
| 2164 | UYVYToYRow = UYVYToYRow_Any_LASX; |
| 2165 | UYVYToUV422Row = UYVYToUV422Row_Any_LASX; |
| 2166 | if (IS_ALIGNED(width, 32)) { |
| 2167 | UYVYToYRow = UYVYToYRow_LASX; |
| 2168 | UYVYToUV422Row = UYVYToUV422Row_LASX; |
| 2169 | } |
| 2170 | } |
| 2171 | #endif |
| 2172 |
| 2173 | for (y = 0; y < height; ++y) { |
| 2174 | UYVYToUV422Row(src_uyvy, dst_u, dst_v, width); |
| 2175 | UYVYToYRow(src_uyvy, dst_y, width); |
| 2176 | src_uyvy += src_stride_uyvy; |
| 2177 | dst_y += dst_stride_y; |
| 2178 | dst_u += dst_stride_u; |
| 2179 | dst_v += dst_stride_v; |
| 2180 | } |
| 2181 | return 0; |
| 2182 | } |
| 2183 |
| 2184 | // Convert YUY2 to Y. |
| 2185 | LIBYUV_API |
| 2186 | int YUY2ToY(const uint8_t* src_yuy2, |
| 2187 | int src_stride_yuy2, |
| 2188 | uint8_t* dst_y, |
| 2189 | int dst_stride_y, |
| 2190 | int width, |
| 2191 | int height) { |
| 2192 | int y; |
| 2193 | void (*YUY2ToYRow)(const uint8_t* src_yuy2, uint8_t* dst_y, int width) = |
| 2194 | YUY2ToYRow_C; |
| 2195 | if (!src_yuy2 || !dst_y || width <= 0 || height == 0) { |
| 2196 | return -1; |
| 2197 | } |
| 2198 | // Negative height means invert the image. |
| 2199 | if (height < 0) { |
| 2200 | height = -height; |
| 2201 | src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2; |
| 2202 | src_stride_yuy2 = -src_stride_yuy2; |
| 2203 | } |
| 2204 | // Coalesce rows. |
| 2205 | if (src_stride_yuy2 == width * 2 && dst_stride_y == width) { |
| 2206 | width *= height; |
| 2207 | height = 1; |
| 2208 | src_stride_yuy2 = dst_stride_y = 0; |
| 2209 | } |
| 2210 | #if defined(HAS_YUY2TOYROW_SSE2) |
| 2211 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2212 | YUY2ToYRow = YUY2ToYRow_Any_SSE2; |
| 2213 | if (IS_ALIGNED(width, 16)) { |
| 2214 | YUY2ToYRow = YUY2ToYRow_SSE2; |
| 2215 | } |
| 2216 | } |
| 2217 | #endif |
| 2218 | #if defined(HAS_YUY2TOYROW_AVX2) |
| 2219 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2220 | YUY2ToYRow = YUY2ToYRow_Any_AVX2; |
| 2221 | if (IS_ALIGNED(width, 32)) { |
| 2222 | YUY2ToYRow = YUY2ToYRow_AVX2; |
| 2223 | } |
| 2224 | } |
| 2225 | #endif |
| 2226 | #if defined(HAS_YUY2TOYROW_NEON) |
| 2227 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2228 | YUY2ToYRow = YUY2ToYRow_Any_NEON; |
| 2229 | if (IS_ALIGNED(width, 16)) { |
| 2230 | YUY2ToYRow = YUY2ToYRow_NEON; |
| 2231 | } |
| 2232 | } |
| 2233 | #endif |
| 2234 | #if defined(HAS_YUY2TOYROW_MSA) |
| 2235 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2236 | YUY2ToYRow = YUY2ToYRow_Any_MSA; |
| 2237 | if (IS_ALIGNED(width, 32)) { |
| 2238 | YUY2ToYRow = YUY2ToYRow_MSA; |
| 2239 | } |
| 2240 | } |
| 2241 | #endif |
| 2242 |
| 2243 | for (y = 0; y < height; ++y) { |
| 2244 | YUY2ToYRow(src_yuy2, dst_y, width); |
| 2245 | src_yuy2 += src_stride_yuy2; |
| 2246 | dst_y += dst_stride_y; |
| 2247 | } |
| 2248 | return 0; |
| 2249 | } |
| 2250 |
| 2251 | // Convert UYVY to Y. |
| 2252 | LIBYUV_API |
| 2253 | int UYVYToY(const uint8_t* src_uyvy, |
| 2254 | int src_stride_uyvy, |
| 2255 | uint8_t* dst_y, |
| 2256 | int dst_stride_y, |
| 2257 | int width, |
| 2258 | int height) { |
| 2259 | int y; |
| 2260 | void (*UYVYToYRow)(const uint8_t* src_uyvy, uint8_t* dst_y, int width) = |
| 2261 | UYVYToYRow_C; |
| 2262 | if (!src_uyvy || !dst_y || width <= 0 || height == 0) { |
| 2263 | return -1; |
| 2264 | } |
| 2265 | // Negative height means invert the image. |
| 2266 | if (height < 0) { |
| 2267 | height = -height; |
| 2268 | src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy; |
| 2269 | src_stride_uyvy = -src_stride_uyvy; |
| 2270 | } |
| 2271 | // Coalesce rows. |
| 2272 | if (src_stride_uyvy == width * 2 && dst_stride_y == width) { |
| 2273 | width *= height; |
| 2274 | height = 1; |
| 2275 | src_stride_uyvy = dst_stride_y = 0; |
| 2276 | } |
| 2277 | #if defined(HAS_UYVYTOYROW_SSE2) |
| 2278 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2279 | UYVYToYRow = UYVYToYRow_Any_SSE2; |
| 2280 | if (IS_ALIGNED(width, 16)) { |
| 2281 | UYVYToYRow = UYVYToYRow_SSE2; |
| 2282 | } |
| 2283 | } |
| 2284 | #endif |
| 2285 | #if defined(HAS_UYVYTOYROW_AVX2) |
| 2286 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2287 | UYVYToYRow = UYVYToYRow_Any_AVX2; |
| 2288 | if (IS_ALIGNED(width, 32)) { |
| 2289 | UYVYToYRow = UYVYToYRow_AVX2; |
| 2290 | } |
| 2291 | } |
| 2292 | #endif |
| 2293 | #if defined(HAS_UYVYTOYROW_NEON) |
| 2294 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2295 | UYVYToYRow = UYVYToYRow_Any_NEON; |
| 2296 | if (IS_ALIGNED(width, 16)) { |
| 2297 | UYVYToYRow = UYVYToYRow_NEON; |
| 2298 | } |
| 2299 | } |
| 2300 | #endif |
| 2301 | #if defined(HAS_UYVYTOYROW_MSA) |
| 2302 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2303 | UYVYToYRow = UYVYToYRow_Any_MSA; |
| 2304 | if (IS_ALIGNED(width, 32)) { |
| 2305 | UYVYToYRow = UYVYToYRow_MSA; |
| 2306 | } |
| 2307 | } |
| 2308 | #endif |
| 2309 |
| 2310 | for (y = 0; y < height; ++y) { |
| 2311 | UYVYToYRow(src_uyvy, dst_y, width); |
| 2312 | src_uyvy += src_stride_uyvy; |
| 2313 | dst_y += dst_stride_y; |
| 2314 | } |
| 2315 | return 0; |
| 2316 | } |
| 2317 |
| 2318 | // Mirror a plane of data. |
| 2319 | // See Also I400Mirror |
| 2320 | LIBYUV_API |
| 2321 | void MirrorPlane(const uint8_t* src_y, |
| 2322 | int src_stride_y, |
| 2323 | uint8_t* dst_y, |
| 2324 | int dst_stride_y, |
| 2325 | int width, |
| 2326 | int height) { |
| 2327 | int y; |
| 2328 | void (*MirrorRow)(const uint8_t* src, uint8_t* dst, int width) = MirrorRow_C; |
| 2329 | // Negative height means invert the image. |
| 2330 | if (height < 0) { |
| 2331 | height = -height; |
| 2332 | src_y = src_y + (height - 1) * src_stride_y; |
| 2333 | src_stride_y = -src_stride_y; |
| 2334 | } |
| 2335 | #if defined(HAS_MIRRORROW_NEON) |
| 2336 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2337 | MirrorRow = MirrorRow_Any_NEON; |
| 2338 | if (IS_ALIGNED(width, 32)) { |
| 2339 | MirrorRow = MirrorRow_NEON; |
| 2340 | } |
| 2341 | } |
| 2342 | #endif |
| 2343 | #if defined(HAS_MIRRORROW_SSSE3) |
| 2344 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2345 | MirrorRow = MirrorRow_Any_SSSE3; |
| 2346 | if (IS_ALIGNED(width, 16)) { |
| 2347 | MirrorRow = MirrorRow_SSSE3; |
| 2348 | } |
| 2349 | } |
| 2350 | #endif |
| 2351 | #if defined(HAS_MIRRORROW_AVX2) |
| 2352 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2353 | MirrorRow = MirrorRow_Any_AVX2; |
| 2354 | if (IS_ALIGNED(width, 32)) { |
| 2355 | MirrorRow = MirrorRow_AVX2; |
| 2356 | } |
| 2357 | } |
| 2358 | #endif |
| 2359 | #if defined(HAS_MIRRORROW_MSA) |
| 2360 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2361 | MirrorRow = MirrorRow_Any_MSA; |
| 2362 | if (IS_ALIGNED(width, 64)) { |
| 2363 | MirrorRow = MirrorRow_MSA; |
| 2364 | } |
| 2365 | } |
| 2366 | #endif |
| 2367 | #if defined(HAS_MIRRORROW_LASX) |
| 2368 | if (TestCpuFlag(kCpuHasLASX)) { |
| 2369 | MirrorRow = MirrorRow_Any_LASX; |
| 2370 | if (IS_ALIGNED(width, 64)) { |
| 2371 | MirrorRow = MirrorRow_LASX; |
| 2372 | } |
| 2373 | } |
| 2374 | #endif |
| 2375 |
| 2376 | // Mirror plane |
| 2377 | for (y = 0; y < height; ++y) { |
| 2378 | MirrorRow(src_y, dst_y, width); |
| 2379 | src_y += src_stride_y; |
| 2380 | dst_y += dst_stride_y; |
| 2381 | } |
| 2382 | } |
| 2383 |
| 2384 | // Mirror a plane of UV data. |
| 2385 | LIBYUV_API |
| 2386 | void MirrorUVPlane(const uint8_t* src_uv, |
| 2387 | int src_stride_uv, |
| 2388 | uint8_t* dst_uv, |
| 2389 | int dst_stride_uv, |
| 2390 | int width, |
| 2391 | int height) { |
| 2392 | int y; |
| 2393 | void (*MirrorUVRow)(const uint8_t* src, uint8_t* dst, int width) = |
| 2394 | MirrorUVRow_C; |
| 2395 | // Negative height means invert the image. |
| 2396 | if (height < 0) { |
| 2397 | height = -height; |
| 2398 | src_uv = src_uv + (height - 1) * src_stride_uv; |
| 2399 | src_stride_uv = -src_stride_uv; |
| 2400 | } |
| 2401 | #if defined(HAS_MIRRORUVROW_NEON) |
| 2402 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2403 | MirrorUVRow = MirrorUVRow_Any_NEON; |
| 2404 | if (IS_ALIGNED(width, 32)) { |
| 2405 | MirrorUVRow = MirrorUVRow_NEON; |
| 2406 | } |
| 2407 | } |
| 2408 | #endif |
| 2409 | #if defined(HAS_MIRRORUVROW_SSSE3) |
| 2410 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2411 | MirrorUVRow = MirrorUVRow_Any_SSSE3; |
| 2412 | if (IS_ALIGNED(width, 8)) { |
| 2413 | MirrorUVRow = MirrorUVRow_SSSE3; |
| 2414 | } |
| 2415 | } |
| 2416 | #endif |
| 2417 | #if defined(HAS_MIRRORUVROW_AVX2) |
| 2418 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2419 | MirrorUVRow = MirrorUVRow_Any_AVX2; |
| 2420 | if (IS_ALIGNED(width, 16)) { |
| 2421 | MirrorUVRow = MirrorUVRow_AVX2; |
| 2422 | } |
| 2423 | } |
| 2424 | #endif |
| 2425 | #if defined(HAS_MIRRORUVROW_MSA) |
| 2426 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2427 | MirrorUVRow = MirrorUVRow_Any_MSA; |
| 2428 | if (IS_ALIGNED(width, 8)) { |
| 2429 | MirrorUVRow = MirrorUVRow_MSA; |
| 2430 | } |
| 2431 | } |
| 2432 | #endif |
| 2433 | #if defined(HAS_MIRRORUVROW_LASX) |
| 2434 | if (TestCpuFlag(kCpuHasLASX)) { |
| 2435 | MirrorUVRow = MirrorUVRow_Any_LASX; |
| 2436 | if (IS_ALIGNED(width, 16)) { |
| 2437 | MirrorUVRow = MirrorUVRow_LASX; |
| 2438 | } |
| 2439 | } |
| 2440 | #endif |
| 2441 |
| 2442 | // MirrorUV plane |
| 2443 | for (y = 0; y < height; ++y) { |
| 2444 | MirrorUVRow(src_uv, dst_uv, width); |
| 2445 | src_uv += src_stride_uv; |
| 2446 | dst_uv += dst_stride_uv; |
| 2447 | } |
| 2448 | } |
| 2449 |
| 2450 | // Mirror I400 with optional flipping |
| 2451 | LIBYUV_API |
| 2452 | int I400Mirror(const uint8_t* src_y, |
| 2453 | int src_stride_y, |
| 2454 | uint8_t* dst_y, |
| 2455 | int dst_stride_y, |
| 2456 | int width, |
| 2457 | int height) { |
| 2458 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 2459 | return -1; |
| 2460 | } |
| 2461 | // Negative height means invert the image. |
| 2462 | if (height < 0) { |
| 2463 | height = -height; |
| 2464 | src_y = src_y + (height - 1) * src_stride_y; |
| 2465 | src_stride_y = -src_stride_y; |
| 2466 | } |
| 2467 |
| 2468 | MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 2469 | return 0; |
| 2470 | } |
| 2471 |
| 2472 | // Mirror I420 with optional flipping |
| 2473 | LIBYUV_API |
| 2474 | int I420Mirror(const uint8_t* src_y, |
| 2475 | int src_stride_y, |
| 2476 | const uint8_t* src_u, |
| 2477 | int src_stride_u, |
| 2478 | const uint8_t* src_v, |
| 2479 | int src_stride_v, |
| 2480 | uint8_t* dst_y, |
| 2481 | int dst_stride_y, |
| 2482 | uint8_t* dst_u, |
| 2483 | int dst_stride_u, |
| 2484 | uint8_t* dst_v, |
| 2485 | int dst_stride_v, |
| 2486 | int width, |
| 2487 | int height) { |
| 2488 | int halfwidth = (width + 1) >> 1; |
| 2489 | int halfheight = (height + 1) >> 1; |
| 2490 |
| 2491 | if (!src_y || !src_u || !src_v || !dst_u || !dst_v || width <= 0 || |
| 2492 | height == 0) { |
| 2493 | return -1; |
| 2494 | } |
| 2495 |
| 2496 | // Negative height means invert the image. |
| 2497 | if (height < 0) { |
| 2498 | height = -height; |
| 2499 | halfheight = (height + 1) >> 1; |
| 2500 | src_y = src_y + (height - 1) * src_stride_y; |
| 2501 | src_u = src_u + (halfheight - 1) * src_stride_u; |
| 2502 | src_v = src_v + (halfheight - 1) * src_stride_v; |
| 2503 | src_stride_y = -src_stride_y; |
| 2504 | src_stride_u = -src_stride_u; |
| 2505 | src_stride_v = -src_stride_v; |
| 2506 | } |
| 2507 |
| 2508 | if (dst_y) { |
| 2509 | MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 2510 | } |
| 2511 | MirrorPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, halfheight); |
| 2512 | MirrorPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, halfheight); |
| 2513 | return 0; |
| 2514 | } |
| 2515 |
| 2516 | // NV12 mirror. |
| 2517 | LIBYUV_API |
| 2518 | int NV12Mirror(const uint8_t* src_y, |
| 2519 | int src_stride_y, |
| 2520 | const uint8_t* src_uv, |
| 2521 | int src_stride_uv, |
| 2522 | uint8_t* dst_y, |
| 2523 | int dst_stride_y, |
| 2524 | uint8_t* dst_uv, |
| 2525 | int dst_stride_uv, |
| 2526 | int width, |
| 2527 | int height) { |
| 2528 | int halfwidth = (width + 1) >> 1; |
| 2529 | int halfheight = (height + 1) >> 1; |
| 2530 |
| 2531 | if (!src_y || !src_uv || !dst_uv || width <= 0 || height == 0) { |
| 2532 | return -1; |
| 2533 | } |
| 2534 |
| 2535 | // Negative height means invert the image. |
| 2536 | if (height < 0) { |
| 2537 | height = -height; |
| 2538 | halfheight = (height + 1) >> 1; |
| 2539 | src_y = src_y + (height - 1) * src_stride_y; |
| 2540 | src_uv = src_uv + (halfheight - 1) * src_stride_uv; |
| 2541 | src_stride_y = -src_stride_y; |
| 2542 | src_stride_uv = -src_stride_uv; |
| 2543 | } |
| 2544 |
| 2545 | if (dst_y) { |
| 2546 | MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 2547 | } |
| 2548 | MirrorUVPlane(src_uv, src_stride_uv, dst_uv, dst_stride_uv, halfwidth, |
| 2549 | halfheight); |
| 2550 | return 0; |
| 2551 | } |
| 2552 |
| 2553 | // ARGB mirror. |
| 2554 | LIBYUV_API |
| 2555 | int ARGBMirror(const uint8_t* src_argb, |
| 2556 | int src_stride_argb, |
| 2557 | uint8_t* dst_argb, |
| 2558 | int dst_stride_argb, |
| 2559 | int width, |
| 2560 | int height) { |
| 2561 | int y; |
| 2562 | void (*ARGBMirrorRow)(const uint8_t* src, uint8_t* dst, int width) = |
| 2563 | ARGBMirrorRow_C; |
| 2564 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 2565 | return -1; |
| 2566 | } |
| 2567 | // Negative height means invert the image. |
| 2568 | if (height < 0) { |
| 2569 | height = -height; |
| 2570 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2571 | src_stride_argb = -src_stride_argb; |
| 2572 | } |
| 2573 | #if defined(HAS_ARGBMIRRORROW_NEON) |
| 2574 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2575 | ARGBMirrorRow = ARGBMirrorRow_Any_NEON; |
| 2576 | if (IS_ALIGNED(width, 8)) { |
| 2577 | ARGBMirrorRow = ARGBMirrorRow_NEON; |
| 2578 | } |
| 2579 | } |
| 2580 | #endif |
| 2581 | #if defined(HAS_ARGBMIRRORROW_SSE2) |
| 2582 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2583 | ARGBMirrorRow = ARGBMirrorRow_Any_SSE2; |
| 2584 | if (IS_ALIGNED(width, 4)) { |
| 2585 | ARGBMirrorRow = ARGBMirrorRow_SSE2; |
| 2586 | } |
| 2587 | } |
| 2588 | #endif |
| 2589 | #if defined(HAS_ARGBMIRRORROW_AVX2) |
| 2590 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2591 | ARGBMirrorRow = ARGBMirrorRow_Any_AVX2; |
| 2592 | if (IS_ALIGNED(width, 8)) { |
| 2593 | ARGBMirrorRow = ARGBMirrorRow_AVX2; |
| 2594 | } |
| 2595 | } |
| 2596 | #endif |
| 2597 | #if defined(HAS_ARGBMIRRORROW_MSA) |
| 2598 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2599 | ARGBMirrorRow = ARGBMirrorRow_Any_MSA; |
| 2600 | if (IS_ALIGNED(width, 16)) { |
| 2601 | ARGBMirrorRow = ARGBMirrorRow_MSA; |
| 2602 | } |
| 2603 | } |
| 2604 | #endif |
| 2605 | #if defined(HAS_ARGBMIRRORROW_LASX) |
| 2606 | if (TestCpuFlag(kCpuHasLASX)) { |
| 2607 | ARGBMirrorRow = ARGBMirrorRow_Any_LASX; |
| 2608 | if (IS_ALIGNED(width, 16)) { |
| 2609 | ARGBMirrorRow = ARGBMirrorRow_LASX; |
| 2610 | } |
| 2611 | } |
| 2612 | #endif |
| 2613 |
| 2614 | // Mirror plane |
| 2615 | for (y = 0; y < height; ++y) { |
| 2616 | ARGBMirrorRow(src_argb, dst_argb, width); |
| 2617 | src_argb += src_stride_argb; |
| 2618 | dst_argb += dst_stride_argb; |
| 2619 | } |
| 2620 | return 0; |
| 2621 | } |
| 2622 |
| 2623 | // RGB24 mirror. |
| 2624 | LIBYUV_API |
| 2625 | int RGB24Mirror(const uint8_t* src_rgb24, |
| 2626 | int src_stride_rgb24, |
| 2627 | uint8_t* dst_rgb24, |
| 2628 | int dst_stride_rgb24, |
| 2629 | int width, |
| 2630 | int height) { |
| 2631 | int y; |
| 2632 | void (*RGB24MirrorRow)(const uint8_t* src, uint8_t* dst, int width) = |
| 2633 | RGB24MirrorRow_C; |
| 2634 | if (!src_rgb24 || !dst_rgb24 || width <= 0 || height == 0) { |
| 2635 | return -1; |
| 2636 | } |
| 2637 | // Negative height means invert the image. |
| 2638 | if (height < 0) { |
| 2639 | height = -height; |
| 2640 | src_rgb24 = src_rgb24 + (height - 1) * src_stride_rgb24; |
| 2641 | src_stride_rgb24 = -src_stride_rgb24; |
| 2642 | } |
| 2643 | #if defined(HAS_RGB24MIRRORROW_NEON) |
| 2644 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2645 | RGB24MirrorRow = RGB24MirrorRow_Any_NEON; |
| 2646 | if (IS_ALIGNED(width, 16)) { |
| 2647 | RGB24MirrorRow = RGB24MirrorRow_NEON; |
| 2648 | } |
| 2649 | } |
| 2650 | #endif |
| 2651 | #if defined(HAS_RGB24MIRRORROW_SSSE3) |
| 2652 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2653 | RGB24MirrorRow = RGB24MirrorRow_Any_SSSE3; |
| 2654 | if (IS_ALIGNED(width, 16)) { |
| 2655 | RGB24MirrorRow = RGB24MirrorRow_SSSE3; |
| 2656 | } |
| 2657 | } |
| 2658 | #endif |
| 2659 |
| 2660 | // Mirror plane |
| 2661 | for (y = 0; y < height; ++y) { |
| 2662 | RGB24MirrorRow(src_rgb24, dst_rgb24, width); |
| 2663 | src_rgb24 += src_stride_rgb24; |
| 2664 | dst_rgb24 += dst_stride_rgb24; |
| 2665 | } |
| 2666 | return 0; |
| 2667 | } |
| 2668 |
| 2669 | // Get a blender that optimized for the CPU and pixel count. |
| 2670 | // As there are 6 blenders to choose from, the caller should try to use |
| 2671 | // the same blend function for all pixels if possible. |
| 2672 | LIBYUV_API |
| 2673 | ARGBBlendRow GetARGBBlend() { |
| 2674 | void (*ARGBBlendRow)(const uint8_t* src_argb, const uint8_t* src_argb1, |
| 2675 | uint8_t* dst_argb, int width) = ARGBBlendRow_C; |
| 2676 | #if defined(HAS_ARGBBLENDROW_SSSE3) |
| 2677 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2678 | ARGBBlendRow = ARGBBlendRow_SSSE3; |
| 2679 | return ARGBBlendRow; |
| 2680 | } |
| 2681 | #endif |
| 2682 | #if defined(HAS_ARGBBLENDROW_NEON) |
| 2683 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2684 | ARGBBlendRow = ARGBBlendRow_NEON; |
| 2685 | } |
| 2686 | #endif |
| 2687 | #if defined(HAS_ARGBBLENDROW_MSA) |
| 2688 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2689 | ARGBBlendRow = ARGBBlendRow_MSA; |
| 2690 | } |
| 2691 | #endif |
| 2692 | #if defined(HAS_ARGBBLENDROW_LSX) |
| 2693 | if (TestCpuFlag(kCpuHasLSX)) { |
| 2694 | ARGBBlendRow = ARGBBlendRow_LSX; |
| 2695 | } |
| 2696 | #endif |
| 2697 | return ARGBBlendRow; |
| 2698 | } |
| 2699 |
| 2700 | // Alpha Blend 2 ARGB images and store to destination. |
| 2701 | LIBYUV_API |
| 2702 | int ARGBBlend(const uint8_t* src_argb0, |
| 2703 | int src_stride_argb0, |
| 2704 | const uint8_t* src_argb1, |
| 2705 | int src_stride_argb1, |
| 2706 | uint8_t* dst_argb, |
| 2707 | int dst_stride_argb, |
| 2708 | int width, |
| 2709 | int height) { |
| 2710 | int y; |
| 2711 | void (*ARGBBlendRow)(const uint8_t* src_argb, const uint8_t* src_argb1, |
| 2712 | uint8_t* dst_argb, int width) = GetARGBBlend(); |
| 2713 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 2714 | return -1; |
| 2715 | } |
| 2716 | // Negative height means invert the image. |
| 2717 | if (height < 0) { |
| 2718 | height = -height; |
| 2719 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 2720 | dst_stride_argb = -dst_stride_argb; |
| 2721 | } |
| 2722 | // Coalesce rows. |
| 2723 | if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && |
| 2724 | dst_stride_argb == width * 4) { |
| 2725 | width *= height; |
| 2726 | height = 1; |
| 2727 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 2728 | } |
| 2729 |
| 2730 | for (y = 0; y < height; ++y) { |
| 2731 | ARGBBlendRow(src_argb0, src_argb1, dst_argb, width); |
| 2732 | src_argb0 += src_stride_argb0; |
| 2733 | src_argb1 += src_stride_argb1; |
| 2734 | dst_argb += dst_stride_argb; |
| 2735 | } |
| 2736 | return 0; |
| 2737 | } |
| 2738 |
| 2739 | // Alpha Blend plane and store to destination. |
| 2740 | LIBYUV_API |
| 2741 | int BlendPlane(const uint8_t* src_y0, |
| 2742 | int src_stride_y0, |
| 2743 | const uint8_t* src_y1, |
| 2744 | int src_stride_y1, |
| 2745 | const uint8_t* alpha, |
| 2746 | int alpha_stride, |
| 2747 | uint8_t* dst_y, |
| 2748 | int dst_stride_y, |
| 2749 | int width, |
| 2750 | int height) { |
| 2751 | int y; |
| 2752 | void (*BlendPlaneRow)(const uint8_t* src0, const uint8_t* src1, |
| 2753 | const uint8_t* alpha, uint8_t* dst, int width) = |
| 2754 | BlendPlaneRow_C; |
| 2755 | if (!src_y0 || !src_y1 || !alpha || !dst_y || width <= 0 || height == 0) { |
| 2756 | return -1; |
| 2757 | } |
| 2758 | // Negative height means invert the image. |
| 2759 | if (height < 0) { |
| 2760 | height = -height; |
| 2761 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 2762 | dst_stride_y = -dst_stride_y; |
| 2763 | } |
| 2764 |
| 2765 | // Coalesce rows for Y plane. |
| 2766 | if (src_stride_y0 == width && src_stride_y1 == width && |
| 2767 | alpha_stride == width && dst_stride_y == width) { |
| 2768 | width *= height; |
| 2769 | height = 1; |
| 2770 | src_stride_y0 = src_stride_y1 = alpha_stride = dst_stride_y = 0; |
| 2771 | } |
| 2772 |
| 2773 | #if defined(HAS_BLENDPLANEROW_SSSE3) |
| 2774 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2775 | BlendPlaneRow = BlendPlaneRow_Any_SSSE3; |
| 2776 | if (IS_ALIGNED(width, 8)) { |
| 2777 | BlendPlaneRow = BlendPlaneRow_SSSE3; |
| 2778 | } |
| 2779 | } |
| 2780 | #endif |
| 2781 | #if defined(HAS_BLENDPLANEROW_AVX2) |
| 2782 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2783 | BlendPlaneRow = BlendPlaneRow_Any_AVX2; |
| 2784 | if (IS_ALIGNED(width, 32)) { |
| 2785 | BlendPlaneRow = BlendPlaneRow_AVX2; |
| 2786 | } |
| 2787 | } |
| 2788 | #endif |
| 2789 |
| 2790 | for (y = 0; y < height; ++y) { |
| 2791 | BlendPlaneRow(src_y0, src_y1, alpha, dst_y, width); |
| 2792 | src_y0 += src_stride_y0; |
| 2793 | src_y1 += src_stride_y1; |
| 2794 | alpha += alpha_stride; |
| 2795 | dst_y += dst_stride_y; |
| 2796 | } |
| 2797 | return 0; |
| 2798 | } |
| 2799 |
| 2800 | #define MAXTWIDTH 2048 |
| 2801 | // Alpha Blend YUV images and store to destination. |
| 2802 | LIBYUV_API |
| 2803 | int I420Blend(const uint8_t* src_y0, |
| 2804 | int src_stride_y0, |
| 2805 | const uint8_t* src_u0, |
| 2806 | int src_stride_u0, |
| 2807 | const uint8_t* src_v0, |
| 2808 | int src_stride_v0, |
| 2809 | const uint8_t* src_y1, |
| 2810 | int src_stride_y1, |
| 2811 | const uint8_t* src_u1, |
| 2812 | int src_stride_u1, |
| 2813 | const uint8_t* src_v1, |
| 2814 | int src_stride_v1, |
| 2815 | const uint8_t* alpha, |
| 2816 | int alpha_stride, |
| 2817 | uint8_t* dst_y, |
| 2818 | int dst_stride_y, |
| 2819 | uint8_t* dst_u, |
| 2820 | int dst_stride_u, |
| 2821 | uint8_t* dst_v, |
| 2822 | int dst_stride_v, |
| 2823 | int width, |
| 2824 | int height) { |
| 2825 | int y; |
| 2826 | // Half width/height for UV. |
| 2827 | int halfwidth = (width + 1) >> 1; |
| 2828 | void (*BlendPlaneRow)(const uint8_t* src0, const uint8_t* src1, |
| 2829 | const uint8_t* alpha, uint8_t* dst, int width) = |
| 2830 | BlendPlaneRow_C; |
| 2831 | void (*ScaleRowDown2)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 2832 | uint8_t* dst_ptr, int dst_width) = ScaleRowDown2Box_C; |
| 2833 |
| 2834 | if (!src_y0 || !src_u0 || !src_v0 || !src_y1 || !src_u1 || !src_v1 || |
| 2835 | !alpha || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 2836 | return -1; |
| 2837 | } |
| 2838 |
| 2839 | // Negative height means invert the image. |
| 2840 | if (height < 0) { |
| 2841 | height = -height; |
| 2842 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 2843 | dst_stride_y = -dst_stride_y; |
| 2844 | } |
| 2845 |
| 2846 | // Blend Y plane. |
| 2847 | BlendPlane(src_y0, src_stride_y0, src_y1, src_stride_y1, alpha, alpha_stride, |
| 2848 | dst_y, dst_stride_y, width, height); |
| 2849 |
| 2850 | #if defined(HAS_BLENDPLANEROW_SSSE3) |
| 2851 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2852 | BlendPlaneRow = BlendPlaneRow_Any_SSSE3; |
| 2853 | if (IS_ALIGNED(halfwidth, 8)) { |
| 2854 | BlendPlaneRow = BlendPlaneRow_SSSE3; |
| 2855 | } |
| 2856 | } |
| 2857 | #endif |
| 2858 | #if defined(HAS_BLENDPLANEROW_AVX2) |
| 2859 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2860 | BlendPlaneRow = BlendPlaneRow_Any_AVX2; |
| 2861 | if (IS_ALIGNED(halfwidth, 32)) { |
| 2862 | BlendPlaneRow = BlendPlaneRow_AVX2; |
| 2863 | } |
| 2864 | } |
| 2865 | #endif |
| 2866 | if (!IS_ALIGNED(width, 2)) { |
| 2867 | ScaleRowDown2 = ScaleRowDown2Box_Odd_C; |
| 2868 | } |
| 2869 | #if defined(HAS_SCALEROWDOWN2_NEON) |
| 2870 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2871 | ScaleRowDown2 = ScaleRowDown2Box_Odd_NEON; |
| 2872 | if (IS_ALIGNED(width, 2)) { |
| 2873 | ScaleRowDown2 = ScaleRowDown2Box_Any_NEON; |
| 2874 | if (IS_ALIGNED(halfwidth, 16)) { |
| 2875 | ScaleRowDown2 = ScaleRowDown2Box_NEON; |
| 2876 | } |
| 2877 | } |
| 2878 | } |
| 2879 | #endif |
| 2880 | #if defined(HAS_SCALEROWDOWN2_SSSE3) |
| 2881 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2882 | ScaleRowDown2 = ScaleRowDown2Box_Odd_SSSE3; |
| 2883 | if (IS_ALIGNED(width, 2)) { |
| 2884 | ScaleRowDown2 = ScaleRowDown2Box_Any_SSSE3; |
| 2885 | if (IS_ALIGNED(halfwidth, 16)) { |
| 2886 | ScaleRowDown2 = ScaleRowDown2Box_SSSE3; |
| 2887 | } |
| 2888 | } |
| 2889 | } |
| 2890 | #endif |
| 2891 | #if defined(HAS_SCALEROWDOWN2_AVX2) |
| 2892 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2893 | ScaleRowDown2 = ScaleRowDown2Box_Odd_AVX2; |
| 2894 | if (IS_ALIGNED(width, 2)) { |
| 2895 | ScaleRowDown2 = ScaleRowDown2Box_Any_AVX2; |
| 2896 | if (IS_ALIGNED(halfwidth, 32)) { |
| 2897 | ScaleRowDown2 = ScaleRowDown2Box_AVX2; |
| 2898 | } |
| 2899 | } |
| 2900 | } |
| 2901 | #endif |
| 2902 |
| 2903 | // Row buffer for intermediate alpha pixels. |
| 2904 | align_buffer_64(halfalpha, halfwidth); |
| 2905 | for (y = 0; y < height; y += 2) { |
| 2906 | // last row of odd height image use 1 row of alpha instead of 2. |
| 2907 | if (y == (height - 1)) { |
| 2908 | alpha_stride = 0; |
| 2909 | } |
| 2910 | // Subsample 2 rows of UV to half width and half height. |
| 2911 | ScaleRowDown2(alpha, alpha_stride, halfalpha, halfwidth); |
| 2912 | alpha += alpha_stride * 2; |
| 2913 | BlendPlaneRow(src_u0, src_u1, halfalpha, dst_u, halfwidth); |
| 2914 | BlendPlaneRow(src_v0, src_v1, halfalpha, dst_v, halfwidth); |
| 2915 | src_u0 += src_stride_u0; |
| 2916 | src_u1 += src_stride_u1; |
| 2917 | dst_u += dst_stride_u; |
| 2918 | src_v0 += src_stride_v0; |
| 2919 | src_v1 += src_stride_v1; |
| 2920 | dst_v += dst_stride_v; |
| 2921 | } |
| 2922 | free_aligned_buffer_64(halfalpha); |
| 2923 | return 0; |
| 2924 | } |
| 2925 |
| 2926 | // Multiply 2 ARGB images and store to destination. |
| 2927 | LIBYUV_API |
| 2928 | int ARGBMultiply(const uint8_t* src_argb0, |
| 2929 | int src_stride_argb0, |
| 2930 | const uint8_t* src_argb1, |
| 2931 | int src_stride_argb1, |
| 2932 | uint8_t* dst_argb, |
| 2933 | int dst_stride_argb, |
| 2934 | int width, |
| 2935 | int height) { |
| 2936 | int y; |
| 2937 | void (*ARGBMultiplyRow)(const uint8_t* src0, const uint8_t* src1, |
| 2938 | uint8_t* dst, int width) = ARGBMultiplyRow_C; |
| 2939 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 2940 | return -1; |
| 2941 | } |
| 2942 | // Negative height means invert the image. |
| 2943 | if (height < 0) { |
| 2944 | height = -height; |
| 2945 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 2946 | dst_stride_argb = -dst_stride_argb; |
| 2947 | } |
| 2948 | // Coalesce rows. |
| 2949 | if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && |
| 2950 | dst_stride_argb == width * 4) { |
| 2951 | width *= height; |
| 2952 | height = 1; |
| 2953 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 2954 | } |
| 2955 | #if defined(HAS_ARGBMULTIPLYROW_SSE2) |
| 2956 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2957 | ARGBMultiplyRow = ARGBMultiplyRow_Any_SSE2; |
| 2958 | if (IS_ALIGNED(width, 4)) { |
| 2959 | ARGBMultiplyRow = ARGBMultiplyRow_SSE2; |
| 2960 | } |
| 2961 | } |
| 2962 | #endif |
| 2963 | #if defined(HAS_ARGBMULTIPLYROW_AVX2) |
| 2964 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2965 | ARGBMultiplyRow = ARGBMultiplyRow_Any_AVX2; |
| 2966 | if (IS_ALIGNED(width, 8)) { |
| 2967 | ARGBMultiplyRow = ARGBMultiplyRow_AVX2; |
| 2968 | } |
| 2969 | } |
| 2970 | #endif |
| 2971 | #if defined(HAS_ARGBMULTIPLYROW_NEON) |
| 2972 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2973 | ARGBMultiplyRow = ARGBMultiplyRow_Any_NEON; |
| 2974 | if (IS_ALIGNED(width, 8)) { |
| 2975 | ARGBMultiplyRow = ARGBMultiplyRow_NEON; |
| 2976 | } |
| 2977 | } |
| 2978 | #endif |
| 2979 | #if defined(HAS_ARGBMULTIPLYROW_MSA) |
| 2980 | if (TestCpuFlag(kCpuHasMSA)) { |
| 2981 | ARGBMultiplyRow = ARGBMultiplyRow_Any_MSA; |
| 2982 | if (IS_ALIGNED(width, 4)) { |
| 2983 | ARGBMultiplyRow = ARGBMultiplyRow_MSA; |
| 2984 | } |
| 2985 | } |
| 2986 | #endif |
| 2987 | #if defined(HAS_ARGBMULTIPLYROW_LASX) |
| 2988 | if (TestCpuFlag(kCpuHasLASX)) { |
| 2989 | ARGBMultiplyRow = ARGBMultiplyRow_Any_LASX; |
| 2990 | if (IS_ALIGNED(width, 8)) { |
| 2991 | ARGBMultiplyRow = ARGBMultiplyRow_LASX; |
| 2992 | } |
| 2993 | } |
| 2994 | #endif |
| 2995 |
| 2996 | // Multiply plane |
| 2997 | for (y = 0; y < height; ++y) { |
| 2998 | ARGBMultiplyRow(src_argb0, src_argb1, dst_argb, width); |
| 2999 | src_argb0 += src_stride_argb0; |
| 3000 | src_argb1 += src_stride_argb1; |
| 3001 | dst_argb += dst_stride_argb; |
| 3002 | } |
| 3003 | return 0; |
| 3004 | } |
| 3005 |
| 3006 | // Add 2 ARGB images and store to destination. |
| 3007 | LIBYUV_API |
| 3008 | int ARGBAdd(const uint8_t* src_argb0, |
| 3009 | int src_stride_argb0, |
| 3010 | const uint8_t* src_argb1, |
| 3011 | int src_stride_argb1, |
| 3012 | uint8_t* dst_argb, |
| 3013 | int dst_stride_argb, |
| 3014 | int width, |
| 3015 | int height) { |
| 3016 | int y; |
| 3017 | void (*ARGBAddRow)(const uint8_t* src0, const uint8_t* src1, uint8_t* dst, |
| 3018 | int width) = ARGBAddRow_C; |
| 3019 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 3020 | return -1; |
| 3021 | } |
| 3022 | // Negative height means invert the image. |
| 3023 | if (height < 0) { |
| 3024 | height = -height; |
| 3025 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 3026 | dst_stride_argb = -dst_stride_argb; |
| 3027 | } |
| 3028 | // Coalesce rows. |
| 3029 | if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && |
| 3030 | dst_stride_argb == width * 4) { |
| 3031 | width *= height; |
| 3032 | height = 1; |
| 3033 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 3034 | } |
| 3035 | #if defined(HAS_ARGBADDROW_SSE2) |
| 3036 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3037 | ARGBAddRow = ARGBAddRow_SSE2; |
| 3038 | } |
| 3039 | #endif |
| 3040 | #if defined(HAS_ARGBADDROW_SSE2) |
| 3041 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3042 | ARGBAddRow = ARGBAddRow_Any_SSE2; |
| 3043 | if (IS_ALIGNED(width, 4)) { |
| 3044 | ARGBAddRow = ARGBAddRow_SSE2; |
| 3045 | } |
| 3046 | } |
| 3047 | #endif |
| 3048 | #if defined(HAS_ARGBADDROW_AVX2) |
| 3049 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3050 | ARGBAddRow = ARGBAddRow_Any_AVX2; |
| 3051 | if (IS_ALIGNED(width, 8)) { |
| 3052 | ARGBAddRow = ARGBAddRow_AVX2; |
| 3053 | } |
| 3054 | } |
| 3055 | #endif |
| 3056 | #if defined(HAS_ARGBADDROW_NEON) |
| 3057 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3058 | ARGBAddRow = ARGBAddRow_Any_NEON; |
| 3059 | if (IS_ALIGNED(width, 8)) { |
| 3060 | ARGBAddRow = ARGBAddRow_NEON; |
| 3061 | } |
| 3062 | } |
| 3063 | #endif |
| 3064 | #if defined(HAS_ARGBADDROW_MSA) |
| 3065 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3066 | ARGBAddRow = ARGBAddRow_Any_MSA; |
| 3067 | if (IS_ALIGNED(width, 8)) { |
| 3068 | ARGBAddRow = ARGBAddRow_MSA; |
| 3069 | } |
| 3070 | } |
| 3071 | #endif |
| 3072 | #if defined(HAS_ARGBADDROW_LASX) |
| 3073 | if (TestCpuFlag(kCpuHasLASX)) { |
| 3074 | ARGBAddRow = ARGBAddRow_Any_LASX; |
| 3075 | if (IS_ALIGNED(width, 8)) { |
| 3076 | ARGBAddRow = ARGBAddRow_LASX; |
| 3077 | } |
| 3078 | } |
| 3079 | #endif |
| 3080 |
| 3081 | // Add plane |
| 3082 | for (y = 0; y < height; ++y) { |
| 3083 | ARGBAddRow(src_argb0, src_argb1, dst_argb, width); |
| 3084 | src_argb0 += src_stride_argb0; |
| 3085 | src_argb1 += src_stride_argb1; |
| 3086 | dst_argb += dst_stride_argb; |
| 3087 | } |
| 3088 | return 0; |
| 3089 | } |
| 3090 |
| 3091 | // Subtract 2 ARGB images and store to destination. |
| 3092 | LIBYUV_API |
| 3093 | int ARGBSubtract(const uint8_t* src_argb0, |
| 3094 | int src_stride_argb0, |
| 3095 | const uint8_t* src_argb1, |
| 3096 | int src_stride_argb1, |
| 3097 | uint8_t* dst_argb, |
| 3098 | int dst_stride_argb, |
| 3099 | int width, |
| 3100 | int height) { |
| 3101 | int y; |
| 3102 | void (*ARGBSubtractRow)(const uint8_t* src0, const uint8_t* src1, |
| 3103 | uint8_t* dst, int width) = ARGBSubtractRow_C; |
| 3104 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 3105 | return -1; |
| 3106 | } |
| 3107 | // Negative height means invert the image. |
| 3108 | if (height < 0) { |
| 3109 | height = -height; |
| 3110 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 3111 | dst_stride_argb = -dst_stride_argb; |
| 3112 | } |
| 3113 | // Coalesce rows. |
| 3114 | if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 && |
| 3115 | dst_stride_argb == width * 4) { |
| 3116 | width *= height; |
| 3117 | height = 1; |
| 3118 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 3119 | } |
| 3120 | #if defined(HAS_ARGBSUBTRACTROW_SSE2) |
| 3121 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3122 | ARGBSubtractRow = ARGBSubtractRow_Any_SSE2; |
| 3123 | if (IS_ALIGNED(width, 4)) { |
| 3124 | ARGBSubtractRow = ARGBSubtractRow_SSE2; |
| 3125 | } |
| 3126 | } |
| 3127 | #endif |
| 3128 | #if defined(HAS_ARGBSUBTRACTROW_AVX2) |
| 3129 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3130 | ARGBSubtractRow = ARGBSubtractRow_Any_AVX2; |
| 3131 | if (IS_ALIGNED(width, 8)) { |
| 3132 | ARGBSubtractRow = ARGBSubtractRow_AVX2; |
| 3133 | } |
| 3134 | } |
| 3135 | #endif |
| 3136 | #if defined(HAS_ARGBSUBTRACTROW_NEON) |
| 3137 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3138 | ARGBSubtractRow = ARGBSubtractRow_Any_NEON; |
| 3139 | if (IS_ALIGNED(width, 8)) { |
| 3140 | ARGBSubtractRow = ARGBSubtractRow_NEON; |
| 3141 | } |
| 3142 | } |
| 3143 | #endif |
| 3144 | #if defined(HAS_ARGBSUBTRACTROW_MSA) |
| 3145 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3146 | ARGBSubtractRow = ARGBSubtractRow_Any_MSA; |
| 3147 | if (IS_ALIGNED(width, 8)) { |
| 3148 | ARGBSubtractRow = ARGBSubtractRow_MSA; |
| 3149 | } |
| 3150 | } |
| 3151 | #endif |
| 3152 | #if defined(HAS_ARGBSUBTRACTROW_LASX) |
| 3153 | if (TestCpuFlag(kCpuHasLASX)) { |
| 3154 | ARGBSubtractRow = ARGBSubtractRow_Any_LASX; |
| 3155 | if (IS_ALIGNED(width, 8)) { |
| 3156 | ARGBSubtractRow = ARGBSubtractRow_LASX; |
| 3157 | } |
| 3158 | } |
| 3159 | #endif |
| 3160 |
| 3161 | // Subtract plane |
| 3162 | for (y = 0; y < height; ++y) { |
| 3163 | ARGBSubtractRow(src_argb0, src_argb1, dst_argb, width); |
| 3164 | src_argb0 += src_stride_argb0; |
| 3165 | src_argb1 += src_stride_argb1; |
| 3166 | dst_argb += dst_stride_argb; |
| 3167 | } |
| 3168 | return 0; |
| 3169 | } |
| 3170 |
| 3171 | // Convert RAW to RGB24. |
| 3172 | LIBYUV_API |
| 3173 | int RAWToRGB24(const uint8_t* src_raw, |
| 3174 | int src_stride_raw, |
| 3175 | uint8_t* dst_rgb24, |
| 3176 | int dst_stride_rgb24, |
| 3177 | int width, |
| 3178 | int height) { |
| 3179 | int y; |
| 3180 | void (*RAWToRGB24Row)(const uint8_t* src_rgb, uint8_t* dst_rgb24, int width) = |
| 3181 | RAWToRGB24Row_C; |
| 3182 | if (!src_raw || !dst_rgb24 || width <= 0 || height == 0) { |
| 3183 | return -1; |
| 3184 | } |
| 3185 | // Negative height means invert the image. |
| 3186 | if (height < 0) { |
| 3187 | height = -height; |
| 3188 | src_raw = src_raw + (height - 1) * src_stride_raw; |
| 3189 | src_stride_raw = -src_stride_raw; |
| 3190 | } |
| 3191 | // Coalesce rows. |
| 3192 | if (src_stride_raw == width * 3 && dst_stride_rgb24 == width * 3) { |
| 3193 | width *= height; |
| 3194 | height = 1; |
| 3195 | src_stride_raw = dst_stride_rgb24 = 0; |
| 3196 | } |
| 3197 | #if defined(HAS_RAWTORGB24ROW_SSSE3) |
| 3198 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 3199 | RAWToRGB24Row = RAWToRGB24Row_Any_SSSE3; |
| 3200 | if (IS_ALIGNED(width, 8)) { |
| 3201 | RAWToRGB24Row = RAWToRGB24Row_SSSE3; |
| 3202 | } |
| 3203 | } |
| 3204 | #endif |
| 3205 | #if defined(HAS_RAWTORGB24ROW_NEON) |
| 3206 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3207 | RAWToRGB24Row = RAWToRGB24Row_Any_NEON; |
| 3208 | if (IS_ALIGNED(width, 8)) { |
| 3209 | RAWToRGB24Row = RAWToRGB24Row_NEON; |
| 3210 | } |
| 3211 | } |
| 3212 | #endif |
| 3213 | #if defined(HAS_RAWTORGB24ROW_MSA) |
| 3214 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3215 | RAWToRGB24Row = RAWToRGB24Row_Any_MSA; |
| 3216 | if (IS_ALIGNED(width, 16)) { |
| 3217 | RAWToRGB24Row = RAWToRGB24Row_MSA; |
| 3218 | } |
| 3219 | } |
| 3220 | #endif |
| 3221 | #if defined(HAS_RAWTORGB24ROW_LSX) |
| 3222 | if (TestCpuFlag(kCpuHasLSX)) { |
| 3223 | RAWToRGB24Row = RAWToRGB24Row_Any_LSX; |
| 3224 | if (IS_ALIGNED(width, 16)) { |
| 3225 | RAWToRGB24Row = RAWToRGB24Row_LSX; |
| 3226 | } |
| 3227 | } |
| 3228 | #endif |
| 3229 |
| 3230 | for (y = 0; y < height; ++y) { |
| 3231 | RAWToRGB24Row(src_raw, dst_rgb24, width); |
| 3232 | src_raw += src_stride_raw; |
| 3233 | dst_rgb24 += dst_stride_rgb24; |
| 3234 | } |
| 3235 | return 0; |
| 3236 | } |
| 3237 |
| 3238 | // TODO(fbarchard): Consider uint8_t value |
| 3239 | LIBYUV_API |
| 3240 | void SetPlane(uint8_t* dst_y, |
| 3241 | int dst_stride_y, |
| 3242 | int width, |
| 3243 | int height, |
| 3244 | uint32_t value) { |
| 3245 | int y; |
| 3246 | void (*SetRow)(uint8_t* dst, uint8_t value, int width) = SetRow_C; |
| 3247 |
| 3248 | if (width <= 0 || height == 0) { |
| 3249 | return; |
| 3250 | } |
| 3251 | if (height < 0) { |
| 3252 | height = -height; |
| 3253 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 3254 | dst_stride_y = -dst_stride_y; |
| 3255 | } |
| 3256 | // Coalesce rows. |
| 3257 | if (dst_stride_y == width) { |
| 3258 | width *= height; |
| 3259 | height = 1; |
| 3260 | dst_stride_y = 0; |
| 3261 | } |
| 3262 | #if defined(HAS_SETROW_NEON) |
| 3263 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3264 | SetRow = SetRow_Any_NEON; |
| 3265 | if (IS_ALIGNED(width, 16)) { |
| 3266 | SetRow = SetRow_NEON; |
| 3267 | } |
| 3268 | } |
| 3269 | #endif |
| 3270 | #if defined(HAS_SETROW_X86) |
| 3271 | if (TestCpuFlag(kCpuHasX86)) { |
| 3272 | SetRow = SetRow_Any_X86; |
| 3273 | if (IS_ALIGNED(width, 4)) { |
| 3274 | SetRow = SetRow_X86; |
| 3275 | } |
| 3276 | } |
| 3277 | #endif |
| 3278 | #if defined(HAS_SETROW_ERMS) |
| 3279 | if (TestCpuFlag(kCpuHasERMS)) { |
| 3280 | SetRow = SetRow_ERMS; |
| 3281 | } |
| 3282 | #endif |
| 3283 | #if defined(HAS_SETROW_MSA) |
| 3284 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 16)) { |
| 3285 | SetRow = SetRow_MSA; |
| 3286 | } |
| 3287 | #endif |
| 3288 | #if defined(HAS_SETROW_LSX) |
| 3289 | if (TestCpuFlag(kCpuHasLSX)) { |
| 3290 | SetRow = SetRow_Any_LSX; |
| 3291 | if (IS_ALIGNED(width, 16)) { |
| 3292 | SetRow = SetRow_LSX; |
| 3293 | } |
| 3294 | } |
| 3295 | #endif |
| 3296 |
| 3297 | // Set plane |
| 3298 | for (y = 0; y < height; ++y) { |
| 3299 | SetRow(dst_y, (uint8_t)value, width); |
| 3300 | dst_y += dst_stride_y; |
| 3301 | } |
| 3302 | } |
| 3303 |
| 3304 | // Draw a rectangle into I420 |
| 3305 | LIBYUV_API |
| 3306 | int I420Rect(uint8_t* dst_y, |
| 3307 | int dst_stride_y, |
| 3308 | uint8_t* dst_u, |
| 3309 | int dst_stride_u, |
| 3310 | uint8_t* dst_v, |
| 3311 | int dst_stride_v, |
| 3312 | int x, |
| 3313 | int y, |
| 3314 | int width, |
| 3315 | int height, |
| 3316 | int value_y, |
| 3317 | int value_u, |
| 3318 | int value_v) { |
| 3319 | int halfwidth = (width + 1) >> 1; |
| 3320 | int halfheight = (height + 1) >> 1; |
| 3321 | uint8_t* start_y = dst_y + y * dst_stride_y + x; |
| 3322 | uint8_t* start_u = dst_u + (y / 2) * dst_stride_u + (x / 2); |
| 3323 | uint8_t* start_v = dst_v + (y / 2) * dst_stride_v + (x / 2); |
| 3324 |
| 3325 | if (!dst_y || !dst_u || !dst_v || width <= 0 || height == 0 || x < 0 || |
| 3326 | y < 0 || value_y < 0 || value_y > 255 || value_u < 0 || value_u > 255 || |
| 3327 | value_v < 0 || value_v > 255) { |
| 3328 | return -1; |
| 3329 | } |
| 3330 |
| 3331 | SetPlane(start_y, dst_stride_y, width, height, value_y); |
| 3332 | SetPlane(start_u, dst_stride_u, halfwidth, halfheight, value_u); |
| 3333 | SetPlane(start_v, dst_stride_v, halfwidth, halfheight, value_v); |
| 3334 | return 0; |
| 3335 | } |
| 3336 |
| 3337 | // Draw a rectangle into ARGB |
| 3338 | LIBYUV_API |
| 3339 | int ARGBRect(uint8_t* dst_argb, |
| 3340 | int dst_stride_argb, |
| 3341 | int dst_x, |
| 3342 | int dst_y, |
| 3343 | int width, |
| 3344 | int height, |
| 3345 | uint32_t value) { |
| 3346 | int y; |
| 3347 | void (*ARGBSetRow)(uint8_t* dst_argb, uint32_t value, int width) = |
| 3348 | ARGBSetRow_C; |
| 3349 | if (!dst_argb || width <= 0 || height == 0 || dst_x < 0 || dst_y < 0) { |
| 3350 | return -1; |
| 3351 | } |
| 3352 | if (height < 0) { |
| 3353 | height = -height; |
| 3354 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 3355 | dst_stride_argb = -dst_stride_argb; |
| 3356 | } |
| 3357 | dst_argb += dst_y * dst_stride_argb + dst_x * 4; |
| 3358 | // Coalesce rows. |
| 3359 | if (dst_stride_argb == width * 4) { |
| 3360 | width *= height; |
| 3361 | height = 1; |
| 3362 | dst_stride_argb = 0; |
| 3363 | } |
| 3364 |
| 3365 | #if defined(HAS_ARGBSETROW_NEON) |
| 3366 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3367 | ARGBSetRow = ARGBSetRow_Any_NEON; |
| 3368 | if (IS_ALIGNED(width, 4)) { |
| 3369 | ARGBSetRow = ARGBSetRow_NEON; |
| 3370 | } |
| 3371 | } |
| 3372 | #endif |
| 3373 | #if defined(HAS_ARGBSETROW_X86) |
| 3374 | if (TestCpuFlag(kCpuHasX86)) { |
| 3375 | ARGBSetRow = ARGBSetRow_X86; |
| 3376 | } |
| 3377 | #endif |
| 3378 | #if defined(HAS_ARGBSETROW_MSA) |
| 3379 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3380 | ARGBSetRow = ARGBSetRow_Any_MSA; |
| 3381 | if (IS_ALIGNED(width, 4)) { |
| 3382 | ARGBSetRow = ARGBSetRow_MSA; |
| 3383 | } |
| 3384 | } |
| 3385 | #endif |
| 3386 | #if defined(HAS_ARGBSETROW_LSX) |
| 3387 | if (TestCpuFlag(kCpuHasLSX)) { |
| 3388 | ARGBSetRow = ARGBSetRow_Any_LSX; |
| 3389 | if (IS_ALIGNED(width, 4)) { |
| 3390 | ARGBSetRow = ARGBSetRow_LSX; |
| 3391 | } |
| 3392 | } |
| 3393 | #endif |
| 3394 |
| 3395 | // Set plane |
| 3396 | for (y = 0; y < height; ++y) { |
| 3397 | ARGBSetRow(dst_argb, value, width); |
| 3398 | dst_argb += dst_stride_argb; |
| 3399 | } |
| 3400 | return 0; |
| 3401 | } |
| 3402 |
| 3403 | // Convert unattentuated ARGB to preattenuated ARGB. |
| 3404 | // An unattenutated ARGB alpha blend uses the formula |
| 3405 | // p = a * f + (1 - a) * b |
| 3406 | // where |
| 3407 | // p is output pixel |
| 3408 | // f is foreground pixel |
| 3409 | // b is background pixel |
| 3410 | // a is alpha value from foreground pixel |
| 3411 | // An preattenutated ARGB alpha blend uses the formula |
| 3412 | // p = f + (1 - a) * b |
| 3413 | // where |
| 3414 | // f is foreground pixel premultiplied by alpha |
| 3415 |
| 3416 | LIBYUV_API |
| 3417 | int ARGBAttenuate(const uint8_t* src_argb, |
| 3418 | int src_stride_argb, |
| 3419 | uint8_t* dst_argb, |
| 3420 | int dst_stride_argb, |
| 3421 | int width, |
| 3422 | int height) { |
| 3423 | int y; |
| 3424 | void (*ARGBAttenuateRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 3425 | int width) = ARGBAttenuateRow_C; |
| 3426 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 3427 | return -1; |
| 3428 | } |
| 3429 | if (height < 0) { |
| 3430 | height = -height; |
| 3431 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 3432 | src_stride_argb = -src_stride_argb; |
| 3433 | } |
| 3434 | // Coalesce rows. |
| 3435 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 3436 | width *= height; |
| 3437 | height = 1; |
| 3438 | src_stride_argb = dst_stride_argb = 0; |
| 3439 | } |
| 3440 | #if defined(HAS_ARGBATTENUATEROW_SSSE3) |
| 3441 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 3442 | ARGBAttenuateRow = ARGBAttenuateRow_Any_SSSE3; |
| 3443 | if (IS_ALIGNED(width, 4)) { |
| 3444 | ARGBAttenuateRow = ARGBAttenuateRow_SSSE3; |
| 3445 | } |
| 3446 | } |
| 3447 | #endif |
| 3448 | #if defined(HAS_ARGBATTENUATEROW_AVX2) |
| 3449 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3450 | ARGBAttenuateRow = ARGBAttenuateRow_Any_AVX2; |
| 3451 | if (IS_ALIGNED(width, 8)) { |
| 3452 | ARGBAttenuateRow = ARGBAttenuateRow_AVX2; |
| 3453 | } |
| 3454 | } |
| 3455 | #endif |
| 3456 | #if defined(HAS_ARGBATTENUATEROW_NEON) |
| 3457 | if (TestCpuFlag(kCpuHasNEON)) { |
| 3458 | ARGBAttenuateRow = ARGBAttenuateRow_Any_NEON; |
| 3459 | if (IS_ALIGNED(width, 8)) { |
| 3460 | ARGBAttenuateRow = ARGBAttenuateRow_NEON; |
| 3461 | } |
| 3462 | } |
| 3463 | #endif |
| 3464 | #if defined(HAS_ARGBATTENUATEROW_MSA) |
| 3465 | if (TestCpuFlag(kCpuHasMSA)) { |
| 3466 | ARGBAttenuateRow = ARGBAttenuateRow_Any_MSA; |
| 3467 | if (IS_ALIGNED(width, 8)) { |
| 3468 | ARGBAttenuateRow = ARGBAttenuateRow_MSA; |
| 3469 | } |
| 3470 | } |
| 3471 | #endif |
| 3472 | #if defined(HAS_ARGBATTENUATEROW_LASX) |
| 3473 | if (TestCpuFlag(kCpuHasLASX)) { |
| 3474 | ARGBAttenuateRow = ARGBAttenuateRow_Any_LASX; |
| 3475 | if (IS_ALIGNED(width, 16)) { |
| 3476 | ARGBAttenuateRow = ARGBAttenuateRow_LASX; |
| 3477 | } |
| 3478 | } |
| 3479 | #endif |
| 3480 |
| 3481 | for (y = 0; y < height; ++y) { |
| 3482 | ARGBAttenuateRow(src_argb, dst_argb, width); |
| 3483 | src_argb += src_stride_argb; |
| 3484 | dst_argb += dst_stride_argb; |
| 3485 | } |
| 3486 | return 0; |
| 3487 | } |
| 3488 |
| 3489 | // Convert preattentuated ARGB to unattenuated ARGB. |
| 3490 | LIBYUV_API |
| 3491 | int ARGBUnattenuate(const uint8_t* src_argb, |
| 3492 | int src_stride_argb, |
| 3493 | uint8_t* dst_argb, |
| 3494 | int dst_stride_argb, |
| 3495 | int width, |
| 3496 | int height) { |
| 3497 | int y; |
| 3498 | void (*ARGBUnattenuateRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 3499 | int width) = ARGBUnattenuateRow_C; |
| 3500 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 3501 | return -1; |
| 3502 | } |
| 3503 | if (height < 0) { |
| 3504 | height = -height; |
| 3505 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 3506 | src_stride_argb = -src_stride_argb; |
| 3507 | } |
| 3508 | // Coalesce rows. |
| 3509 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 3510 | width *= height; |
| 3511 | height = 1; |
| 3512 | src_stride_argb = dst_stride_argb = 0; |
| 3513 | } |
| 3514 | #if defined(HAS_ARGBUNATTENUATEROW_SSE2) |
| 3515 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3516 | ARGBUnattenuateRow = ARGBUnattenuateRow_Any_SSE2; |
| 3517 | if (IS_ALIGNED(width, 4)) { |
| 3518 | ARGBUnattenuateRow = ARGBUnattenuateRow_SSE2; |
| 3519 | } |
| 3520 | } |
| 3521 | #endif |
| 3522 | #if defined(HAS_ARGBUNATTENUATEROW_AVX2) |
| 3523 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 3524 | ARGBUnattenuateRow = ARGBUnattenuateRow_Any_AVX2; |
| 3525 | if (IS_ALIGNED(width, 8)) { |
| 3526 | ARGBUnattenuateRow = ARGBUnattenuateRow_AVX2; |
| 3527 | } |
| 3528 | } |
| 3529 | #endif |
| 3530 | // TODO(fbarchard): Neon version. |
| 3531 |
| 3532 | for (y = 0; y < height; ++y) { |
| 3533 | ARGBUnattenuateRow(src_argb, dst_argb, width); |
| 3534 | src_argb += src_stride_argb; |
| 3535 | dst_argb += dst_stride_argb; |
| 3536 | } |
| 3537 | return 0; |
| 3538 | } |
| 3539 |
| 3540 | // Convert ARGB to Grayed ARGB. |
| 3541 | LIBYUV_API |
| 3542 | int ARGBGrayTo(const uint8_t* src_argb, |
| 3543 | int src_stride_argb, |
| 3544 | uint8_t* dst_argb, |
| 3545 | int dst_stride_argb, |
| 3546 | int width, |
| 3547 | int height) { |
| 3548 | int y; |
| 3549 | void (*ARGBGrayRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) = |
| 3550 | ARGBGrayRow_C; |
| 3551 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 3552 | return -1; |
| 3553 | } |
| 3554 | if (height < 0) { |
| 3555 | height = -height; |
| 3556 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 3557 | src_stride_argb = -src_stride_argb; |
| 3558 | } |
| 3559 | // Coalesce rows. |
| 3560 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 3561 | width *= height; |
| 3562 | height = 1; |
| 3563 | src_stride_argb = dst_stride_argb = 0; |
| 3564 | } |
| 3565 | #if defined(HAS_ARGBGRAYROW_SSSE3) |
| 3566 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 3567 | ARGBGrayRow = ARGBGrayRow_SSSE3; |
| 3568 | } |
| 3569 | #endif |
| 3570 | #if defined(HAS_ARGBGRAYROW_NEON) |
| 3571 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 3572 | ARGBGrayRow = ARGBGrayRow_NEON; |
| 3573 | } |
| 3574 | #endif |
| 3575 | #if defined(HAS_ARGBGRAYROW_MSA) |
| 3576 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 3577 | ARGBGrayRow = ARGBGrayRow_MSA; |
| 3578 | } |
| 3579 | #endif |
| 3580 | #if defined(HAS_ARGBGRAYROW_LASX) |
| 3581 | if (TestCpuFlag(kCpuHasLASX) && IS_ALIGNED(width, 16)) { |
| 3582 | ARGBGrayRow = ARGBGrayRow_LASX; |
| 3583 | } |
| 3584 | #endif |
| 3585 |
| 3586 | for (y = 0; y < height; ++y) { |
| 3587 | ARGBGrayRow(src_argb, dst_argb, width); |
| 3588 | src_argb += src_stride_argb; |
| 3589 | dst_argb += dst_stride_argb; |
| 3590 | } |
| 3591 | return 0; |
| 3592 | } |
| 3593 |
| 3594 | // Make a rectangle of ARGB gray scale. |
| 3595 | LIBYUV_API |
| 3596 | int ARGBGray(uint8_t* dst_argb, |
| 3597 | int dst_stride_argb, |
| 3598 | int dst_x, |
| 3599 | int dst_y, |
| 3600 | int width, |
| 3601 | int height) { |
| 3602 | int y; |
| 3603 | void (*ARGBGrayRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) = |
| 3604 | ARGBGrayRow_C; |
| 3605 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 3606 | if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) { |
| 3607 | return -1; |
| 3608 | } |
| 3609 | // Coalesce rows. |
| 3610 | if (dst_stride_argb == width * 4) { |
| 3611 | width *= height; |
| 3612 | height = 1; |
| 3613 | dst_stride_argb = 0; |
| 3614 | } |
| 3615 | #if defined(HAS_ARGBGRAYROW_SSSE3) |
| 3616 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 3617 | ARGBGrayRow = ARGBGrayRow_SSSE3; |
| 3618 | } |
| 3619 | #endif |
| 3620 | #if defined(HAS_ARGBGRAYROW_NEON) |
| 3621 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 3622 | ARGBGrayRow = ARGBGrayRow_NEON; |
| 3623 | } |
| 3624 | #endif |
| 3625 | #if defined(HAS_ARGBGRAYROW_MSA) |
| 3626 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 3627 | ARGBGrayRow = ARGBGrayRow_MSA; |
| 3628 | } |
| 3629 | #endif |
| 3630 | #if defined(HAS_ARGBGRAYROW_LASX) |
| 3631 | if (TestCpuFlag(kCpuHasLASX) && IS_ALIGNED(width, 16)) { |
| 3632 | ARGBGrayRow = ARGBGrayRow_LASX; |
| 3633 | } |
| 3634 | #endif |
| 3635 |
| 3636 | for (y = 0; y < height; ++y) { |
| 3637 | ARGBGrayRow(dst, dst, width); |
| 3638 | dst += dst_stride_argb; |
| 3639 | } |
| 3640 | return 0; |
| 3641 | } |
| 3642 |
| 3643 | // Make a rectangle of ARGB Sepia tone. |
| 3644 | LIBYUV_API |
| 3645 | int ARGBSepia(uint8_t* dst_argb, |
| 3646 | int dst_stride_argb, |
| 3647 | int dst_x, |
| 3648 | int dst_y, |
| 3649 | int width, |
| 3650 | int height) { |
| 3651 | int y; |
| 3652 | void (*ARGBSepiaRow)(uint8_t* dst_argb, int width) = ARGBSepiaRow_C; |
| 3653 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 3654 | if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) { |
| 3655 | return -1; |
| 3656 | } |
| 3657 | // Coalesce rows. |
| 3658 | if (dst_stride_argb == width * 4) { |
| 3659 | width *= height; |
| 3660 | height = 1; |
| 3661 | dst_stride_argb = 0; |
| 3662 | } |
| 3663 | #if defined(HAS_ARGBSEPIAROW_SSSE3) |
| 3664 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 3665 | ARGBSepiaRow = ARGBSepiaRow_SSSE3; |
| 3666 | } |
| 3667 | #endif |
| 3668 | #if defined(HAS_ARGBSEPIAROW_NEON) |
| 3669 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 3670 | ARGBSepiaRow = ARGBSepiaRow_NEON; |
| 3671 | } |
| 3672 | #endif |
| 3673 | #if defined(HAS_ARGBSEPIAROW_MSA) |
| 3674 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 3675 | ARGBSepiaRow = ARGBSepiaRow_MSA; |
| 3676 | } |
| 3677 | #endif |
| 3678 | #if defined(HAS_ARGBSEPIAROW_LASX) |
| 3679 | if (TestCpuFlag(kCpuHasLASX) && IS_ALIGNED(width, 16)) { |
| 3680 | ARGBSepiaRow = ARGBSepiaRow_LASX; |
| 3681 | } |
| 3682 | #endif |
| 3683 |
| 3684 | for (y = 0; y < height; ++y) { |
| 3685 | ARGBSepiaRow(dst, width); |
| 3686 | dst += dst_stride_argb; |
| 3687 | } |
| 3688 | return 0; |
| 3689 | } |
| 3690 |
| 3691 | // Apply a 4x4 matrix to each ARGB pixel. |
| 3692 | // Note: Normally for shading, but can be used to swizzle or invert. |
| 3693 | LIBYUV_API |
| 3694 | int ARGBColorMatrix(const uint8_t* src_argb, |
| 3695 | int src_stride_argb, |
| 3696 | uint8_t* dst_argb, |
| 3697 | int dst_stride_argb, |
| 3698 | const int8_t* matrix_argb, |
| 3699 | int width, |
| 3700 | int height) { |
| 3701 | int y; |
| 3702 | void (*ARGBColorMatrixRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 3703 | const int8_t* matrix_argb, int width) = |
| 3704 | ARGBColorMatrixRow_C; |
| 3705 | if (!src_argb || !dst_argb || !matrix_argb || width <= 0 || height == 0) { |
| 3706 | return -1; |
| 3707 | } |
| 3708 | if (height < 0) { |
| 3709 | height = -height; |
| 3710 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 3711 | src_stride_argb = -src_stride_argb; |
| 3712 | } |
| 3713 | // Coalesce rows. |
| 3714 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 3715 | width *= height; |
| 3716 | height = 1; |
| 3717 | src_stride_argb = dst_stride_argb = 0; |
| 3718 | } |
| 3719 | #if defined(HAS_ARGBCOLORMATRIXROW_SSSE3) |
| 3720 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 3721 | ARGBColorMatrixRow = ARGBColorMatrixRow_SSSE3; |
| 3722 | } |
| 3723 | #endif |
| 3724 | #if defined(HAS_ARGBCOLORMATRIXROW_NEON) |
| 3725 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 3726 | ARGBColorMatrixRow = ARGBColorMatrixRow_NEON; |
| 3727 | } |
| 3728 | #endif |
| 3729 | #if defined(HAS_ARGBCOLORMATRIXROW_MSA) |
| 3730 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 3731 | ARGBColorMatrixRow = ARGBColorMatrixRow_MSA; |
| 3732 | } |
| 3733 | #endif |
| 3734 | #if defined(HAS_ARGBCOLORMATRIXROW_LSX) |
| 3735 | if (TestCpuFlag(kCpuHasLSX) && IS_ALIGNED(width, 8)) { |
| 3736 | ARGBColorMatrixRow = ARGBColorMatrixRow_LSX; |
| 3737 | } |
| 3738 | #endif |
| 3739 | for (y = 0; y < height; ++y) { |
| 3740 | ARGBColorMatrixRow(src_argb, dst_argb, matrix_argb, width); |
| 3741 | src_argb += src_stride_argb; |
| 3742 | dst_argb += dst_stride_argb; |
| 3743 | } |
| 3744 | return 0; |
| 3745 | } |
| 3746 |
| 3747 | // Apply a 4x3 matrix to each ARGB pixel. |
| 3748 | // Deprecated. |
| 3749 | LIBYUV_API |
| 3750 | int RGBColorMatrix(uint8_t* dst_argb, |
| 3751 | int dst_stride_argb, |
| 3752 | const int8_t* matrix_rgb, |
| 3753 | int dst_x, |
| 3754 | int dst_y, |
| 3755 | int width, |
| 3756 | int height) { |
| 3757 | SIMD_ALIGNED(int8_t matrix_argb[16]); |
| 3758 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 3759 | if (!dst_argb || !matrix_rgb || width <= 0 || height <= 0 || dst_x < 0 || |
| 3760 | dst_y < 0) { |
| 3761 | return -1; |
| 3762 | } |
| 3763 |
| 3764 | // Convert 4x3 7 bit matrix to 4x4 6 bit matrix. |
| 3765 | matrix_argb[0] = matrix_rgb[0] / 2; |
| 3766 | matrix_argb[1] = matrix_rgb[1] / 2; |
| 3767 | matrix_argb[2] = matrix_rgb[2] / 2; |
| 3768 | matrix_argb[3] = matrix_rgb[3] / 2; |
| 3769 | matrix_argb[4] = matrix_rgb[4] / 2; |
| 3770 | matrix_argb[5] = matrix_rgb[5] / 2; |
| 3771 | matrix_argb[6] = matrix_rgb[6] / 2; |
| 3772 | matrix_argb[7] = matrix_rgb[7] / 2; |
| 3773 | matrix_argb[8] = matrix_rgb[8] / 2; |
| 3774 | matrix_argb[9] = matrix_rgb[9] / 2; |
| 3775 | matrix_argb[10] = matrix_rgb[10] / 2; |
| 3776 | matrix_argb[11] = matrix_rgb[11] / 2; |
| 3777 | matrix_argb[14] = matrix_argb[13] = matrix_argb[12] = 0; |
| 3778 | matrix_argb[15] = 64; // 1.0 |
| 3779 |
| 3780 | return ARGBColorMatrix((const uint8_t*)(dst), dst_stride_argb, dst, |
| 3781 | dst_stride_argb, &matrix_argb[0], width, height); |
| 3782 | } |
| 3783 |
| 3784 | // Apply a color table each ARGB pixel. |
| 3785 | // Table contains 256 ARGB values. |
| 3786 | LIBYUV_API |
| 3787 | int ARGBColorTable(uint8_t* dst_argb, |
| 3788 | int dst_stride_argb, |
| 3789 | const uint8_t* table_argb, |
| 3790 | int dst_x, |
| 3791 | int dst_y, |
| 3792 | int width, |
| 3793 | int height) { |
| 3794 | int y; |
| 3795 | void (*ARGBColorTableRow)(uint8_t* dst_argb, const uint8_t* table_argb, |
| 3796 | int width) = ARGBColorTableRow_C; |
| 3797 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 3798 | if (!dst_argb || !table_argb || width <= 0 || height <= 0 || dst_x < 0 || |
| 3799 | dst_y < 0) { |
| 3800 | return -1; |
| 3801 | } |
| 3802 | // Coalesce rows. |
| 3803 | if (dst_stride_argb == width * 4) { |
| 3804 | width *= height; |
| 3805 | height = 1; |
| 3806 | dst_stride_argb = 0; |
| 3807 | } |
| 3808 | #if defined(HAS_ARGBCOLORTABLEROW_X86) |
| 3809 | if (TestCpuFlag(kCpuHasX86)) { |
| 3810 | ARGBColorTableRow = ARGBColorTableRow_X86; |
| 3811 | } |
| 3812 | #endif |
| 3813 | for (y = 0; y < height; ++y) { |
| 3814 | ARGBColorTableRow(dst, table_argb, width); |
| 3815 | dst += dst_stride_argb; |
| 3816 | } |
| 3817 | return 0; |
| 3818 | } |
| 3819 |
| 3820 | // Apply a color table each ARGB pixel but preserve destination alpha. |
| 3821 | // Table contains 256 ARGB values. |
| 3822 | LIBYUV_API |
| 3823 | int RGBColorTable(uint8_t* dst_argb, |
| 3824 | int dst_stride_argb, |
| 3825 | const uint8_t* table_argb, |
| 3826 | int dst_x, |
| 3827 | int dst_y, |
| 3828 | int width, |
| 3829 | int height) { |
| 3830 | int y; |
| 3831 | void (*RGBColorTableRow)(uint8_t* dst_argb, const uint8_t* table_argb, |
| 3832 | int width) = RGBColorTableRow_C; |
| 3833 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 3834 | if (!dst_argb || !table_argb || width <= 0 || height <= 0 || dst_x < 0 || |
| 3835 | dst_y < 0) { |
| 3836 | return -1; |
| 3837 | } |
| 3838 | // Coalesce rows. |
| 3839 | if (dst_stride_argb == width * 4) { |
| 3840 | width *= height; |
| 3841 | height = 1; |
| 3842 | dst_stride_argb = 0; |
| 3843 | } |
| 3844 | #if defined(HAS_RGBCOLORTABLEROW_X86) |
| 3845 | if (TestCpuFlag(kCpuHasX86)) { |
| 3846 | RGBColorTableRow = RGBColorTableRow_X86; |
| 3847 | } |
| 3848 | #endif |
| 3849 | for (y = 0; y < height; ++y) { |
| 3850 | RGBColorTableRow(dst, table_argb, width); |
| 3851 | dst += dst_stride_argb; |
| 3852 | } |
| 3853 | return 0; |
| 3854 | } |
| 3855 |
| 3856 | // ARGBQuantize is used to posterize art. |
| 3857 | // e.g. rgb / qvalue * qvalue + qvalue / 2 |
| 3858 | // But the low levels implement efficiently with 3 parameters, and could be |
| 3859 | // used for other high level operations. |
| 3860 | // dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset; |
| 3861 | // where scale is 1 / interval_size as a fixed point value. |
| 3862 | // The divide is replaces with a multiply by reciprocal fixed point multiply. |
| 3863 | // Caveat - although SSE2 saturates, the C function does not and should be used |
| 3864 | // with care if doing anything but quantization. |
| 3865 | LIBYUV_API |
| 3866 | int ARGBQuantize(uint8_t* dst_argb, |
| 3867 | int dst_stride_argb, |
| 3868 | int scale, |
| 3869 | int interval_size, |
| 3870 | int interval_offset, |
| 3871 | int dst_x, |
| 3872 | int dst_y, |
| 3873 | int width, |
| 3874 | int height) { |
| 3875 | int y; |
| 3876 | void (*ARGBQuantizeRow)(uint8_t* dst_argb, int scale, int interval_size, |
| 3877 | int interval_offset, int width) = ARGBQuantizeRow_C; |
| 3878 | uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 3879 | if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0 || |
| 3880 | interval_size < 1 || interval_size > 255) { |
| 3881 | return -1; |
| 3882 | } |
| 3883 | // Coalesce rows. |
| 3884 | if (dst_stride_argb == width * 4) { |
| 3885 | width *= height; |
| 3886 | height = 1; |
| 3887 | dst_stride_argb = 0; |
| 3888 | } |
| 3889 | #if defined(HAS_ARGBQUANTIZEROW_SSE2) |
| 3890 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) { |
| 3891 | ARGBQuantizeRow = ARGBQuantizeRow_SSE2; |
| 3892 | } |
| 3893 | #endif |
| 3894 | #if defined(HAS_ARGBQUANTIZEROW_NEON) |
| 3895 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 3896 | ARGBQuantizeRow = ARGBQuantizeRow_NEON; |
| 3897 | } |
| 3898 | #endif |
| 3899 | #if defined(HAS_ARGBQUANTIZEROW_MSA) |
| 3900 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) { |
| 3901 | ARGBQuantizeRow = ARGBQuantizeRow_MSA; |
| 3902 | } |
| 3903 | #endif |
| 3904 | #if defined(HAS_ARGBQUANTIZEROW_LSX) |
| 3905 | if (TestCpuFlag(kCpuHasLSX) && IS_ALIGNED(width, 8)) { |
| 3906 | ARGBQuantizeRow = ARGBQuantizeRow_LSX; |
| 3907 | } |
| 3908 | #endif |
| 3909 | for (y = 0; y < height; ++y) { |
| 3910 | ARGBQuantizeRow(dst, scale, interval_size, interval_offset, width); |
| 3911 | dst += dst_stride_argb; |
| 3912 | } |
| 3913 | return 0; |
| 3914 | } |
| 3915 |
| 3916 | // Computes table of cumulative sum for image where the value is the sum |
| 3917 | // of all values above and to the left of the entry. Used by ARGBBlur. |
| 3918 | LIBYUV_API |
| 3919 | int ARGBComputeCumulativeSum(const uint8_t* src_argb, |
| 3920 | int src_stride_argb, |
| 3921 | int32_t* dst_cumsum, |
| 3922 | int dst_stride32_cumsum, |
| 3923 | int width, |
| 3924 | int height) { |
| 3925 | int y; |
| 3926 | void (*ComputeCumulativeSumRow)(const uint8_t* row, int32_t* cumsum, |
| 3927 | const int32_t* previous_cumsum, int width) = |
| 3928 | ComputeCumulativeSumRow_C; |
| 3929 | int32_t* previous_cumsum = dst_cumsum; |
| 3930 | if (!dst_cumsum || !src_argb || width <= 0 || height <= 0) { |
| 3931 | return -1; |
| 3932 | } |
| 3933 | #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2) |
| 3934 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3935 | ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2; |
| 3936 | } |
| 3937 | #endif |
| 3938 |
| 3939 | memset(dst_cumsum, 0, width * sizeof(dst_cumsum[0]) * 4); // 4 int per pixel. |
| 3940 | for (y = 0; y < height; ++y) { |
| 3941 | ComputeCumulativeSumRow(src_argb, dst_cumsum, previous_cumsum, width); |
| 3942 | previous_cumsum = dst_cumsum; |
| 3943 | dst_cumsum += dst_stride32_cumsum; |
| 3944 | src_argb += src_stride_argb; |
| 3945 | } |
| 3946 | return 0; |
| 3947 | } |
| 3948 |
| 3949 | // Blur ARGB image. |
| 3950 | // Caller should allocate CumulativeSum table of width * height * 16 bytes |
| 3951 | // aligned to 16 byte boundary. height can be radius * 2 + 2 to save memory |
| 3952 | // as the buffer is treated as circular. |
| 3953 | LIBYUV_API |
| 3954 | int ARGBBlur(const uint8_t* src_argb, |
| 3955 | int src_stride_argb, |
| 3956 | uint8_t* dst_argb, |
| 3957 | int dst_stride_argb, |
| 3958 | int32_t* dst_cumsum, |
| 3959 | int dst_stride32_cumsum, |
| 3960 | int width, |
| 3961 | int height, |
| 3962 | int radius) { |
| 3963 | int y; |
| 3964 | void (*ComputeCumulativeSumRow)(const uint8_t* row, int32_t* cumsum, |
| 3965 | const int32_t* previous_cumsum, int width) = |
| 3966 | ComputeCumulativeSumRow_C; |
| 3967 | void (*CumulativeSumToAverageRow)( |
| 3968 | const int32_t* topleft, const int32_t* botleft, int width, int area, |
| 3969 | uint8_t* dst, int count) = CumulativeSumToAverageRow_C; |
| 3970 | int32_t* cumsum_bot_row; |
| 3971 | int32_t* max_cumsum_bot_row; |
| 3972 | int32_t* cumsum_top_row; |
| 3973 |
| 3974 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 3975 | return -1; |
| 3976 | } |
| 3977 | if (height < 0) { |
| 3978 | height = -height; |
| 3979 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 3980 | src_stride_argb = -src_stride_argb; |
| 3981 | } |
| 3982 | if (radius > height) { |
| 3983 | radius = height; |
| 3984 | } |
| 3985 | if (radius > (width / 2 - 1)) { |
| 3986 | radius = width / 2 - 1; |
| 3987 | } |
| 3988 | if (radius <= 0 || height <= 1) { |
| 3989 | return -1; |
| 3990 | } |
| 3991 | #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2) |
| 3992 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 3993 | ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2; |
| 3994 | CumulativeSumToAverageRow = CumulativeSumToAverageRow_SSE2; |
| 3995 | } |
| 3996 | #endif |
| 3997 | // Compute enough CumulativeSum for first row to be blurred. After this |
| 3998 | // one row of CumulativeSum is updated at a time. |
| 3999 | ARGBComputeCumulativeSum(src_argb, src_stride_argb, dst_cumsum, |
| 4000 | dst_stride32_cumsum, width, radius); |
| 4001 |
| 4002 | src_argb = src_argb + radius * src_stride_argb; |
| 4003 | cumsum_bot_row = &dst_cumsum[(radius - 1) * dst_stride32_cumsum]; |
| 4004 |
| 4005 | max_cumsum_bot_row = &dst_cumsum[(radius * 2 + 2) * dst_stride32_cumsum]; |
| 4006 | cumsum_top_row = &dst_cumsum[0]; |
| 4007 |
| 4008 | for (y = 0; y < height; ++y) { |
| 4009 | int top_y = ((y - radius - 1) >= 0) ? (y - radius - 1) : 0; |
| 4010 | int bot_y = ((y + radius) < height) ? (y + radius) : (height - 1); |
| 4011 | int area = radius * (bot_y - top_y); |
| 4012 | int boxwidth = radius * 4; |
| 4013 | int x; |
| 4014 | int n; |
| 4015 |
| 4016 | // Increment cumsum_top_row pointer with circular buffer wrap around. |
| 4017 | if (top_y) { |
| 4018 | cumsum_top_row += dst_stride32_cumsum; |
| 4019 | if (cumsum_top_row >= max_cumsum_bot_row) { |
| 4020 | cumsum_top_row = dst_cumsum; |
| 4021 | } |
| 4022 | } |
| 4023 | // Increment cumsum_bot_row pointer with circular buffer wrap around and |
| 4024 | // then fill in a row of CumulativeSum. |
| 4025 | if ((y + radius) < height) { |
| 4026 | const int32_t* prev_cumsum_bot_row = cumsum_bot_row; |
| 4027 | cumsum_bot_row += dst_stride32_cumsum; |
| 4028 | if (cumsum_bot_row >= max_cumsum_bot_row) { |
| 4029 | cumsum_bot_row = dst_cumsum; |
| 4030 | } |
| 4031 | ComputeCumulativeSumRow(src_argb, cumsum_bot_row, prev_cumsum_bot_row, |
| 4032 | width); |
| 4033 | src_argb += src_stride_argb; |
| 4034 | } |
| 4035 |
| 4036 | // Left clipped. |
| 4037 | for (x = 0; x < radius + 1; ++x) { |
| 4038 | CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, boxwidth, area, |
| 4039 | &dst_argb[x * 4], 1); |
| 4040 | area += (bot_y - top_y); |
| 4041 | boxwidth += 4; |
| 4042 | } |
| 4043 |
| 4044 | // Middle unclipped. |
| 4045 | n = (width - 1) - radius - x + 1; |
| 4046 | CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, boxwidth, area, |
| 4047 | &dst_argb[x * 4], n); |
| 4048 |
| 4049 | // Right clipped. |
| 4050 | for (x += n; x <= width - 1; ++x) { |
| 4051 | area -= (bot_y - top_y); |
| 4052 | boxwidth -= 4; |
| 4053 | CumulativeSumToAverageRow(cumsum_top_row + (x - radius - 1) * 4, |
| 4054 | cumsum_bot_row + (x - radius - 1) * 4, boxwidth, |
| 4055 | area, &dst_argb[x * 4], 1); |
| 4056 | } |
| 4057 | dst_argb += dst_stride_argb; |
| 4058 | } |
| 4059 | return 0; |
| 4060 | } |
| 4061 |
| 4062 | // Multiply ARGB image by a specified ARGB value. |
| 4063 | LIBYUV_API |
| 4064 | int ARGBShade(const uint8_t* src_argb, |
| 4065 | int src_stride_argb, |
| 4066 | uint8_t* dst_argb, |
| 4067 | int dst_stride_argb, |
| 4068 | int width, |
| 4069 | int height, |
| 4070 | uint32_t value) { |
| 4071 | int y; |
| 4072 | void (*ARGBShadeRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width, |
| 4073 | uint32_t value) = ARGBShadeRow_C; |
| 4074 | if (!src_argb || !dst_argb || width <= 0 || height == 0 || value == 0u) { |
| 4075 | return -1; |
| 4076 | } |
| 4077 | if (height < 0) { |
| 4078 | height = -height; |
| 4079 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 4080 | src_stride_argb = -src_stride_argb; |
| 4081 | } |
| 4082 | // Coalesce rows. |
| 4083 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 4084 | width *= height; |
| 4085 | height = 1; |
| 4086 | src_stride_argb = dst_stride_argb = 0; |
| 4087 | } |
| 4088 | #if defined(HAS_ARGBSHADEROW_SSE2) |
| 4089 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) { |
| 4090 | ARGBShadeRow = ARGBShadeRow_SSE2; |
| 4091 | } |
| 4092 | #endif |
| 4093 | #if defined(HAS_ARGBSHADEROW_NEON) |
| 4094 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 4095 | ARGBShadeRow = ARGBShadeRow_NEON; |
| 4096 | } |
| 4097 | #endif |
| 4098 | #if defined(HAS_ARGBSHADEROW_MSA) |
| 4099 | if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 4)) { |
| 4100 | ARGBShadeRow = ARGBShadeRow_MSA; |
| 4101 | } |
| 4102 | #endif |
| 4103 | #if defined(HAS_ARGBSHADEROW_LASX) |
| 4104 | if (TestCpuFlag(kCpuHasLASX) && IS_ALIGNED(width, 8)) { |
| 4105 | ARGBShadeRow = ARGBShadeRow_LASX; |
| 4106 | } |
| 4107 | #endif |
| 4108 |
| 4109 | for (y = 0; y < height; ++y) { |
| 4110 | ARGBShadeRow(src_argb, dst_argb, width, value); |
| 4111 | src_argb += src_stride_argb; |
| 4112 | dst_argb += dst_stride_argb; |
| 4113 | } |
| 4114 | return 0; |
| 4115 | } |
| 4116 |
| 4117 | // Interpolate 2 planes by specified amount (0 to 255). |
| 4118 | LIBYUV_API |
| 4119 | int InterpolatePlane(const uint8_t* src0, |
| 4120 | int src_stride0, |
| 4121 | const uint8_t* src1, |
| 4122 | int src_stride1, |
| 4123 | uint8_t* dst, |
| 4124 | int dst_stride, |
| 4125 | int width, |
| 4126 | int height, |
| 4127 | int interpolation) { |
| 4128 | int y; |
| 4129 | void (*InterpolateRow)(uint8_t* dst_ptr, const uint8_t* src_ptr, |
| 4130 | ptrdiff_t src_stride, int dst_width, |
| 4131 | int source_y_fraction) = InterpolateRow_C; |
| 4132 | if (!src0 || !src1 || !dst || width <= 0 || height == 0) { |
| 4133 | return -1; |
| 4134 | } |
| 4135 | // Negative height means invert the image. |
| 4136 | if (height < 0) { |
| 4137 | height = -height; |
| 4138 | dst = dst + (height - 1) * dst_stride; |
| 4139 | dst_stride = -dst_stride; |
| 4140 | } |
| 4141 | // Coalesce rows. |
| 4142 | if (src_stride0 == width && src_stride1 == width && dst_stride == width) { |
| 4143 | width *= height; |
| 4144 | height = 1; |
| 4145 | src_stride0 = src_stride1 = dst_stride = 0; |
| 4146 | } |
| 4147 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 4148 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 4149 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 4150 | if (IS_ALIGNED(width, 16)) { |
| 4151 | InterpolateRow = InterpolateRow_SSSE3; |
| 4152 | } |
| 4153 | } |
| 4154 | #endif |
| 4155 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 4156 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 4157 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 4158 | if (IS_ALIGNED(width, 32)) { |
| 4159 | InterpolateRow = InterpolateRow_AVX2; |
| 4160 | } |
| 4161 | } |
| 4162 | #endif |
| 4163 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 4164 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4165 | InterpolateRow = InterpolateRow_Any_NEON; |
| 4166 | if (IS_ALIGNED(width, 16)) { |
| 4167 | InterpolateRow = InterpolateRow_NEON; |
| 4168 | } |
| 4169 | } |
| 4170 | #endif |
| 4171 | #if defined(HAS_INTERPOLATEROW_MSA) |
| 4172 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4173 | InterpolateRow = InterpolateRow_Any_MSA; |
| 4174 | if (IS_ALIGNED(width, 32)) { |
| 4175 | InterpolateRow = InterpolateRow_MSA; |
| 4176 | } |
| 4177 | } |
| 4178 | #endif |
| 4179 | #if defined(HAS_INTERPOLATEROW_LSX) |
| 4180 | if (TestCpuFlag(kCpuHasLSX)) { |
| 4181 | InterpolateRow = InterpolateRow_Any_LSX; |
| 4182 | if (IS_ALIGNED(width, 32)) { |
| 4183 | InterpolateRow = InterpolateRow_LSX; |
| 4184 | } |
| 4185 | } |
| 4186 | #endif |
| 4187 |
| 4188 | for (y = 0; y < height; ++y) { |
| 4189 | InterpolateRow(dst, src0, src1 - src0, width, interpolation); |
| 4190 | src0 += src_stride0; |
| 4191 | src1 += src_stride1; |
| 4192 | dst += dst_stride; |
| 4193 | } |
| 4194 | return 0; |
| 4195 | } |
| 4196 |
| 4197 | // Interpolate 2 planes by specified amount (0 to 255). |
| 4198 | LIBYUV_API |
| 4199 | int InterpolatePlane_16(const uint16_t* src0, |
| 4200 | int src_stride0, |
| 4201 | const uint16_t* src1, |
| 4202 | int src_stride1, |
| 4203 | uint16_t* dst, |
| 4204 | int dst_stride, |
| 4205 | int width, |
| 4206 | int height, |
| 4207 | int interpolation) { |
| 4208 | int y; |
| 4209 | void (*InterpolateRow_16)(uint16_t* dst_ptr, const uint16_t* src_ptr, |
| 4210 | ptrdiff_t src_stride, int dst_width, |
| 4211 | int source_y_fraction) = InterpolateRow_16_C; |
| 4212 | if (!src0 || !src1 || !dst || width <= 0 || height == 0) { |
| 4213 | return -1; |
| 4214 | } |
| 4215 | // Negative height means invert the image. |
| 4216 | if (height < 0) { |
| 4217 | height = -height; |
| 4218 | dst = dst + (height - 1) * dst_stride; |
| 4219 | dst_stride = -dst_stride; |
| 4220 | } |
| 4221 | // Coalesce rows. |
| 4222 | if (src_stride0 == width && src_stride1 == width && dst_stride == width) { |
| 4223 | width *= height; |
| 4224 | height = 1; |
| 4225 | src_stride0 = src_stride1 = dst_stride = 0; |
| 4226 | } |
| 4227 | #if defined(HAS_INTERPOLATEROW_16_SSSE3) |
| 4228 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 4229 | InterpolateRow_16 = InterpolateRow_16_Any_SSSE3; |
| 4230 | if (IS_ALIGNED(width, 16)) { |
| 4231 | InterpolateRow_16 = InterpolateRow_16_SSSE3; |
| 4232 | } |
| 4233 | } |
| 4234 | #endif |
| 4235 | #if defined(HAS_INTERPOLATEROW_16_AVX2) |
| 4236 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 4237 | InterpolateRow_16 = InterpolateRow_16_Any_AVX2; |
| 4238 | if (IS_ALIGNED(width, 32)) { |
| 4239 | InterpolateRow_16 = InterpolateRow_16_AVX2; |
| 4240 | } |
| 4241 | } |
| 4242 | #endif |
| 4243 | #if defined(HAS_INTERPOLATEROW_16_NEON) |
| 4244 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4245 | InterpolateRow_16 = InterpolateRow_16_Any_NEON; |
| 4246 | if (IS_ALIGNED(width, 8)) { |
| 4247 | InterpolateRow_16 = InterpolateRow_16_NEON; |
| 4248 | } |
| 4249 | } |
| 4250 | #endif |
| 4251 | #if defined(HAS_INTERPOLATEROW_16_MSA) |
| 4252 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4253 | InterpolateRow_16 = InterpolateRow_16_Any_MSA; |
| 4254 | if (IS_ALIGNED(width, 32)) { |
| 4255 | InterpolateRow_16 = InterpolateRow_16_MSA; |
| 4256 | } |
| 4257 | } |
| 4258 | #endif |
| 4259 | #if defined(HAS_INTERPOLATEROW_16_LSX) |
| 4260 | if (TestCpuFlag(kCpuHasLSX)) { |
| 4261 | InterpolateRow_16 = InterpolateRow_16_Any_LSX; |
| 4262 | if (IS_ALIGNED(width, 32)) { |
| 4263 | InterpolateRow_16 = InterpolateRow_16_LSX; |
| 4264 | } |
| 4265 | } |
| 4266 | #endif |
| 4267 |
| 4268 | for (y = 0; y < height; ++y) { |
| 4269 | InterpolateRow_16(dst, src0, src1 - src0, width, interpolation); |
| 4270 | src0 += src_stride0; |
| 4271 | src1 += src_stride1; |
| 4272 | dst += dst_stride; |
| 4273 | } |
| 4274 | return 0; |
| 4275 | } |
| 4276 |
| 4277 | // Interpolate 2 ARGB images by specified amount (0 to 255). |
| 4278 | LIBYUV_API |
| 4279 | int ARGBInterpolate(const uint8_t* src_argb0, |
| 4280 | int src_stride_argb0, |
| 4281 | const uint8_t* src_argb1, |
| 4282 | int src_stride_argb1, |
| 4283 | uint8_t* dst_argb, |
| 4284 | int dst_stride_argb, |
| 4285 | int width, |
| 4286 | int height, |
| 4287 | int interpolation) { |
| 4288 | return InterpolatePlane(src_argb0, src_stride_argb0, src_argb1, |
| 4289 | src_stride_argb1, dst_argb, dst_stride_argb, |
| 4290 | width * 4, height, interpolation); |
| 4291 | } |
| 4292 |
| 4293 | // Interpolate 2 YUV images by specified amount (0 to 255). |
| 4294 | LIBYUV_API |
| 4295 | int I420Interpolate(const uint8_t* src0_y, |
| 4296 | int src0_stride_y, |
| 4297 | const uint8_t* src0_u, |
| 4298 | int src0_stride_u, |
| 4299 | const uint8_t* src0_v, |
| 4300 | int src0_stride_v, |
| 4301 | const uint8_t* src1_y, |
| 4302 | int src1_stride_y, |
| 4303 | const uint8_t* src1_u, |
| 4304 | int src1_stride_u, |
| 4305 | const uint8_t* src1_v, |
| 4306 | int src1_stride_v, |
| 4307 | uint8_t* dst_y, |
| 4308 | int dst_stride_y, |
| 4309 | uint8_t* dst_u, |
| 4310 | int dst_stride_u, |
| 4311 | uint8_t* dst_v, |
| 4312 | int dst_stride_v, |
| 4313 | int width, |
| 4314 | int height, |
| 4315 | int interpolation) { |
| 4316 | int halfwidth = (width + 1) >> 1; |
| 4317 | int halfheight = (height + 1) >> 1; |
| 4318 |
| 4319 | if (!src0_y || !src0_u || !src0_v || !src1_y || !src1_u || !src1_v || |
| 4320 | !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 4321 | return -1; |
| 4322 | } |
| 4323 |
| 4324 | InterpolatePlane(src0_y, src0_stride_y, src1_y, src1_stride_y, dst_y, |
| 4325 | dst_stride_y, width, height, interpolation); |
| 4326 | InterpolatePlane(src0_u, src0_stride_u, src1_u, src1_stride_u, dst_u, |
| 4327 | dst_stride_u, halfwidth, halfheight, interpolation); |
| 4328 | InterpolatePlane(src0_v, src0_stride_v, src1_v, src1_stride_v, dst_v, |
| 4329 | dst_stride_v, halfwidth, halfheight, interpolation); |
| 4330 | return 0; |
| 4331 | } |
| 4332 |
| 4333 | // Shuffle ARGB channel order. e.g. BGRA to ARGB. |
| 4334 | LIBYUV_API |
| 4335 | int ARGBShuffle(const uint8_t* src_bgra, |
| 4336 | int src_stride_bgra, |
| 4337 | uint8_t* dst_argb, |
| 4338 | int dst_stride_argb, |
| 4339 | const uint8_t* shuffler, |
| 4340 | int width, |
| 4341 | int height) { |
| 4342 | int y; |
| 4343 | void (*ARGBShuffleRow)(const uint8_t* src_bgra, uint8_t* dst_argb, |
| 4344 | const uint8_t* shuffler, int width) = ARGBShuffleRow_C; |
| 4345 | if (!src_bgra || !dst_argb || width <= 0 || height == 0) { |
| 4346 | return -1; |
| 4347 | } |
| 4348 | // Negative height means invert the image. |
| 4349 | if (height < 0) { |
| 4350 | height = -height; |
| 4351 | src_bgra = src_bgra + (height - 1) * src_stride_bgra; |
| 4352 | src_stride_bgra = -src_stride_bgra; |
| 4353 | } |
| 4354 | // Coalesce rows. |
| 4355 | if (src_stride_bgra == width * 4 && dst_stride_argb == width * 4) { |
| 4356 | width *= height; |
| 4357 | height = 1; |
| 4358 | src_stride_bgra = dst_stride_argb = 0; |
| 4359 | } |
| 4360 | #if defined(HAS_ARGBSHUFFLEROW_SSSE3) |
| 4361 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 4362 | ARGBShuffleRow = ARGBShuffleRow_Any_SSSE3; |
| 4363 | if (IS_ALIGNED(width, 8)) { |
| 4364 | ARGBShuffleRow = ARGBShuffleRow_SSSE3; |
| 4365 | } |
| 4366 | } |
| 4367 | #endif |
| 4368 | #if defined(HAS_ARGBSHUFFLEROW_AVX2) |
| 4369 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 4370 | ARGBShuffleRow = ARGBShuffleRow_Any_AVX2; |
| 4371 | if (IS_ALIGNED(width, 16)) { |
| 4372 | ARGBShuffleRow = ARGBShuffleRow_AVX2; |
| 4373 | } |
| 4374 | } |
| 4375 | #endif |
| 4376 | #if defined(HAS_ARGBSHUFFLEROW_NEON) |
| 4377 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4378 | ARGBShuffleRow = ARGBShuffleRow_Any_NEON; |
| 4379 | if (IS_ALIGNED(width, 4)) { |
| 4380 | ARGBShuffleRow = ARGBShuffleRow_NEON; |
| 4381 | } |
| 4382 | } |
| 4383 | #endif |
| 4384 | #if defined(HAS_ARGBSHUFFLEROW_MSA) |
| 4385 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4386 | ARGBShuffleRow = ARGBShuffleRow_Any_MSA; |
| 4387 | if (IS_ALIGNED(width, 8)) { |
| 4388 | ARGBShuffleRow = ARGBShuffleRow_MSA; |
| 4389 | } |
| 4390 | } |
| 4391 | #endif |
| 4392 | #if defined(HAS_ARGBSHUFFLEROW_LASX) |
| 4393 | if (TestCpuFlag(kCpuHasLASX)) { |
| 4394 | ARGBShuffleRow = ARGBShuffleRow_Any_LASX; |
| 4395 | if (IS_ALIGNED(width, 16)) { |
| 4396 | ARGBShuffleRow = ARGBShuffleRow_LASX; |
| 4397 | } |
| 4398 | } |
| 4399 | #endif |
| 4400 |
| 4401 | for (y = 0; y < height; ++y) { |
| 4402 | ARGBShuffleRow(src_bgra, dst_argb, shuffler, width); |
| 4403 | src_bgra += src_stride_bgra; |
| 4404 | dst_argb += dst_stride_argb; |
| 4405 | } |
| 4406 | return 0; |
| 4407 | } |
| 4408 |
| 4409 | // Shuffle AR64 channel order. e.g. AR64 to AB64. |
| 4410 | LIBYUV_API |
| 4411 | int AR64Shuffle(const uint16_t* src_ar64, |
| 4412 | int src_stride_ar64, |
| 4413 | uint16_t* dst_ar64, |
| 4414 | int dst_stride_ar64, |
| 4415 | const uint8_t* shuffler, |
| 4416 | int width, |
| 4417 | int height) { |
| 4418 | int y; |
| 4419 | void (*AR64ShuffleRow)(const uint8_t* src_ar64, uint8_t* dst_ar64, |
| 4420 | const uint8_t* shuffler, int width) = AR64ShuffleRow_C; |
| 4421 | if (!src_ar64 || !dst_ar64 || width <= 0 || height == 0) { |
| 4422 | return -1; |
| 4423 | } |
| 4424 | // Negative height means invert the image. |
| 4425 | if (height < 0) { |
| 4426 | height = -height; |
| 4427 | src_ar64 = src_ar64 + (height - 1) * src_stride_ar64; |
| 4428 | src_stride_ar64 = -src_stride_ar64; |
| 4429 | } |
| 4430 | // Coalesce rows. |
| 4431 | if (src_stride_ar64 == width * 4 && dst_stride_ar64 == width * 4) { |
| 4432 | width *= height; |
| 4433 | height = 1; |
| 4434 | src_stride_ar64 = dst_stride_ar64 = 0; |
| 4435 | } |
| 4436 | // Assembly versions can be reused if it's implemented with shuffle. |
| 4437 | #if defined(HAS_ARGBSHUFFLEROW_SSSE3) |
| 4438 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 4439 | AR64ShuffleRow = ARGBShuffleRow_Any_SSSE3; |
| 4440 | if (IS_ALIGNED(width, 8)) { |
| 4441 | AR64ShuffleRow = ARGBShuffleRow_SSSE3; |
| 4442 | } |
| 4443 | } |
| 4444 | #endif |
| 4445 | #if defined(HAS_ARGBSHUFFLEROW_AVX2) |
| 4446 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 4447 | AR64ShuffleRow = ARGBShuffleRow_Any_AVX2; |
| 4448 | if (IS_ALIGNED(width, 16)) { |
| 4449 | AR64ShuffleRow = ARGBShuffleRow_AVX2; |
| 4450 | } |
| 4451 | } |
| 4452 | #endif |
| 4453 | #if defined(HAS_ARGBSHUFFLEROW_NEON) |
| 4454 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4455 | AR64ShuffleRow = ARGBShuffleRow_Any_NEON; |
| 4456 | if (IS_ALIGNED(width, 4)) { |
| 4457 | AR64ShuffleRow = ARGBShuffleRow_NEON; |
| 4458 | } |
| 4459 | } |
| 4460 | #endif |
| 4461 |
| 4462 | for (y = 0; y < height; ++y) { |
| 4463 | AR64ShuffleRow((uint8_t*)(src_ar64), (uint8_t*)(dst_ar64), shuffler, |
| 4464 | width * 2); |
| 4465 | src_ar64 += src_stride_ar64; |
| 4466 | dst_ar64 += dst_stride_ar64; |
| 4467 | } |
| 4468 | return 0; |
| 4469 | } |
| 4470 |
| 4471 | // Gauss blur a float plane using Gaussian 5x5 filter with |
| 4472 | // coefficients of 1, 4, 6, 4, 1. |
| 4473 | // Each destination pixel is a blur of the 5x5 |
| 4474 | // pixels from the source. |
| 4475 | // Source edges are clamped. |
| 4476 | // Edge is 2 pixels on each side, and interior is multiple of 4. |
| 4477 | LIBYUV_API |
| 4478 | int GaussPlane_F32(const float* src, |
| 4479 | int src_stride, |
| 4480 | float* dst, |
| 4481 | int dst_stride, |
| 4482 | int width, |
| 4483 | int height) { |
| 4484 | int y; |
| 4485 | void (*GaussCol_F32)(const float* src0, const float* src1, const float* src2, |
| 4486 | const float* src3, const float* src4, float* dst, |
| 4487 | int width) = GaussCol_F32_C; |
| 4488 | void (*GaussRow_F32)(const float* src, float* dst, int width) = |
| 4489 | GaussRow_F32_C; |
| 4490 | if (!src || !dst || width <= 0 || height == 0) { |
| 4491 | return -1; |
| 4492 | } |
| 4493 | // Negative height means invert the image. |
| 4494 | if (height < 0) { |
| 4495 | height = -height; |
| 4496 | src = src + (height - 1) * src_stride; |
| 4497 | src_stride = -src_stride; |
| 4498 | } |
| 4499 |
| 4500 | #if defined(HAS_GAUSSCOL_F32_NEON) |
| 4501 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 4502 | GaussCol_F32 = GaussCol_F32_NEON; |
| 4503 | } |
| 4504 | #endif |
| 4505 | #if defined(HAS_GAUSSROW_F32_NEON) |
| 4506 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 4507 | GaussRow_F32 = GaussRow_F32_NEON; |
| 4508 | } |
| 4509 | #endif |
| 4510 | { |
| 4511 | // 2 pixels on each side, but aligned out to 16 bytes. |
| 4512 | align_buffer_64(rowbuf, (4 + width + 4) * 4); |
| 4513 | memset(rowbuf, 0, 16); |
| 4514 | memset(rowbuf + (4 + width) * 4, 0, 16); |
| 4515 | float* row = (float*)(rowbuf + 16); |
| 4516 | const float* src0 = src; |
| 4517 | const float* src1 = src; |
| 4518 | const float* src2 = src; |
| 4519 | const float* src3 = src2 + ((height > 1) ? src_stride : 0); |
| 4520 | const float* src4 = src3 + ((height > 2) ? src_stride : 0); |
| 4521 |
| 4522 | for (y = 0; y < height; ++y) { |
| 4523 | GaussCol_F32(src0, src1, src2, src3, src4, row, width); |
| 4524 |
| 4525 | // Extrude edge by 2 floats |
| 4526 | row[-2] = row[-1] = row[0]; |
| 4527 | row[width + 1] = row[width] = row[width - 1]; |
| 4528 |
| 4529 | GaussRow_F32(row - 2, dst, width); |
| 4530 |
| 4531 | src0 = src1; |
| 4532 | src1 = src2; |
| 4533 | src2 = src3; |
| 4534 | src3 = src4; |
| 4535 | if ((y + 2) < (height - 1)) { |
| 4536 | src4 += src_stride; |
| 4537 | } |
| 4538 | dst += dst_stride; |
| 4539 | } |
| 4540 | free_aligned_buffer_64(rowbuf); |
| 4541 | } |
| 4542 | return 0; |
| 4543 | } |
| 4544 |
| 4545 | // Sobel ARGB effect. |
| 4546 | static int ARGBSobelize(const uint8_t* src_argb, |
| 4547 | int src_stride_argb, |
| 4548 | uint8_t* dst_argb, |
| 4549 | int dst_stride_argb, |
| 4550 | int width, |
| 4551 | int height, |
| 4552 | void (*SobelRow)(const uint8_t* src_sobelx, |
| 4553 | const uint8_t* src_sobely, |
| 4554 | uint8_t* dst, |
| 4555 | int width)) { |
| 4556 | int y; |
| 4557 | void (*ARGBToYJRow)(const uint8_t* src_argb, uint8_t* dst_g, int width) = |
| 4558 | ARGBToYJRow_C; |
| 4559 | void (*SobelYRow)(const uint8_t* src_y0, const uint8_t* src_y1, |
| 4560 | uint8_t* dst_sobely, int width) = SobelYRow_C; |
| 4561 | void (*SobelXRow)(const uint8_t* src_y0, const uint8_t* src_y1, |
| 4562 | const uint8_t* src_y2, uint8_t* dst_sobely, int width) = |
| 4563 | SobelXRow_C; |
| 4564 | const int kEdge = 16; // Extra pixels at start of row for extrude/align. |
| 4565 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 4566 | return -1; |
| 4567 | } |
| 4568 | // Negative height means invert the image. |
| 4569 | if (height < 0) { |
| 4570 | height = -height; |
| 4571 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 4572 | src_stride_argb = -src_stride_argb; |
| 4573 | } |
| 4574 |
| 4575 | #if defined(HAS_ARGBTOYJROW_SSSE3) |
| 4576 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 4577 | ARGBToYJRow = ARGBToYJRow_Any_SSSE3; |
| 4578 | if (IS_ALIGNED(width, 16)) { |
| 4579 | ARGBToYJRow = ARGBToYJRow_SSSE3; |
| 4580 | } |
| 4581 | } |
| 4582 | #endif |
| 4583 | #if defined(HAS_ARGBTOYJROW_AVX2) |
| 4584 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 4585 | ARGBToYJRow = ARGBToYJRow_Any_AVX2; |
| 4586 | if (IS_ALIGNED(width, 32)) { |
| 4587 | ARGBToYJRow = ARGBToYJRow_AVX2; |
| 4588 | } |
| 4589 | } |
| 4590 | #endif |
| 4591 | #if defined(HAS_ARGBTOYJROW_NEON) |
| 4592 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4593 | ARGBToYJRow = ARGBToYJRow_Any_NEON; |
| 4594 | if (IS_ALIGNED(width, 16)) { |
| 4595 | ARGBToYJRow = ARGBToYJRow_NEON; |
| 4596 | } |
| 4597 | } |
| 4598 | #endif |
| 4599 | #if defined(HAS_ARGBTOYJROW_MSA) |
| 4600 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4601 | ARGBToYJRow = ARGBToYJRow_Any_MSA; |
| 4602 | if (IS_ALIGNED(width, 16)) { |
| 4603 | ARGBToYJRow = ARGBToYJRow_MSA; |
| 4604 | } |
| 4605 | } |
| 4606 | #endif |
| 4607 | #if defined(HAS_ARGBTOYJROW_LSX) |
| 4608 | if (TestCpuFlag(kCpuHasLSX)) { |
| 4609 | ARGBToYJRow = ARGBToYJRow_Any_LSX; |
| 4610 | if (IS_ALIGNED(width, 16)) { |
| 4611 | ARGBToYJRow = ARGBToYJRow_LSX; |
| 4612 | } |
| 4613 | } |
| 4614 | #endif |
| 4615 | #if defined(HAS_ARGBTOYJROW_LASX) |
| 4616 | if (TestCpuFlag(kCpuHasLASX)) { |
| 4617 | ARGBToYJRow = ARGBToYJRow_Any_LASX; |
| 4618 | if (IS_ALIGNED(width, 32)) { |
| 4619 | ARGBToYJRow = ARGBToYJRow_LASX; |
| 4620 | } |
| 4621 | } |
| 4622 | #endif |
| 4623 |
| 4624 | #if defined(HAS_SOBELYROW_SSE2) |
| 4625 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 4626 | SobelYRow = SobelYRow_SSE2; |
| 4627 | } |
| 4628 | #endif |
| 4629 | #if defined(HAS_SOBELYROW_NEON) |
| 4630 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4631 | SobelYRow = SobelYRow_NEON; |
| 4632 | } |
| 4633 | #endif |
| 4634 | #if defined(HAS_SOBELYROW_MSA) |
| 4635 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4636 | SobelYRow = SobelYRow_MSA; |
| 4637 | } |
| 4638 | #endif |
| 4639 | #if defined(HAS_SOBELXROW_SSE2) |
| 4640 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 4641 | SobelXRow = SobelXRow_SSE2; |
| 4642 | } |
| 4643 | #endif |
| 4644 | #if defined(HAS_SOBELXROW_NEON) |
| 4645 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4646 | SobelXRow = SobelXRow_NEON; |
| 4647 | } |
| 4648 | #endif |
| 4649 | #if defined(HAS_SOBELXROW_MSA) |
| 4650 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4651 | SobelXRow = SobelXRow_MSA; |
| 4652 | } |
| 4653 | #endif |
| 4654 | { |
| 4655 | // 3 rows with edges before/after. |
| 4656 | const int row_size = (width + kEdge + 31) & ~31; |
| 4657 | align_buffer_64(rows, row_size * 2 + (kEdge + row_size * 3 + kEdge)); |
| 4658 | uint8_t* row_sobelx = rows; |
| 4659 | uint8_t* row_sobely = rows + row_size; |
| 4660 | uint8_t* row_y = rows + row_size * 2; |
| 4661 |
| 4662 | // Convert first row. |
| 4663 | uint8_t* row_y0 = row_y + kEdge; |
| 4664 | uint8_t* row_y1 = row_y0 + row_size; |
| 4665 | uint8_t* row_y2 = row_y1 + row_size; |
| 4666 | ARGBToYJRow(src_argb, row_y0, width); |
| 4667 | row_y0[-1] = row_y0[0]; |
| 4668 | memset(row_y0 + width, row_y0[width - 1], 16); // Extrude 16 for valgrind. |
| 4669 | ARGBToYJRow(src_argb, row_y1, width); |
| 4670 | row_y1[-1] = row_y1[0]; |
| 4671 | memset(row_y1 + width, row_y1[width - 1], 16); |
| 4672 | memset(row_y2 + width, 0, 16); |
| 4673 |
| 4674 | for (y = 0; y < height; ++y) { |
| 4675 | // Convert next row of ARGB to G. |
| 4676 | if (y < (height - 1)) { |
| 4677 | src_argb += src_stride_argb; |
| 4678 | } |
| 4679 | ARGBToYJRow(src_argb, row_y2, width); |
| 4680 | row_y2[-1] = row_y2[0]; |
| 4681 | row_y2[width] = row_y2[width - 1]; |
| 4682 |
| 4683 | SobelXRow(row_y0 - 1, row_y1 - 1, row_y2 - 1, row_sobelx, width); |
| 4684 | SobelYRow(row_y0 - 1, row_y2 - 1, row_sobely, width); |
| 4685 | SobelRow(row_sobelx, row_sobely, dst_argb, width); |
| 4686 |
| 4687 | // Cycle thru circular queue of 3 row_y buffers. |
| 4688 | { |
| 4689 | uint8_t* row_yt = row_y0; |
| 4690 | row_y0 = row_y1; |
| 4691 | row_y1 = row_y2; |
| 4692 | row_y2 = row_yt; |
| 4693 | } |
| 4694 |
| 4695 | dst_argb += dst_stride_argb; |
| 4696 | } |
| 4697 | free_aligned_buffer_64(rows); |
| 4698 | } |
| 4699 | return 0; |
| 4700 | } |
| 4701 |
| 4702 | // Sobel ARGB effect. |
| 4703 | LIBYUV_API |
| 4704 | int ARGBSobel(const uint8_t* src_argb, |
| 4705 | int src_stride_argb, |
| 4706 | uint8_t* dst_argb, |
| 4707 | int dst_stride_argb, |
| 4708 | int width, |
| 4709 | int height) { |
| 4710 | void (*SobelRow)(const uint8_t* src_sobelx, const uint8_t* src_sobely, |
| 4711 | uint8_t* dst_argb, int width) = SobelRow_C; |
| 4712 | #if defined(HAS_SOBELROW_SSE2) |
| 4713 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 4714 | SobelRow = SobelRow_Any_SSE2; |
| 4715 | if (IS_ALIGNED(width, 16)) { |
| 4716 | SobelRow = SobelRow_SSE2; |
| 4717 | } |
| 4718 | } |
| 4719 | #endif |
| 4720 | #if defined(HAS_SOBELROW_NEON) |
| 4721 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4722 | SobelRow = SobelRow_Any_NEON; |
| 4723 | if (IS_ALIGNED(width, 8)) { |
| 4724 | SobelRow = SobelRow_NEON; |
| 4725 | } |
| 4726 | } |
| 4727 | #endif |
| 4728 | #if defined(HAS_SOBELROW_MSA) |
| 4729 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4730 | SobelRow = SobelRow_Any_MSA; |
| 4731 | if (IS_ALIGNED(width, 16)) { |
| 4732 | SobelRow = SobelRow_MSA; |
| 4733 | } |
| 4734 | } |
| 4735 | #endif |
| 4736 | #if defined(HAS_SOBELROW_LSX) |
| 4737 | if (TestCpuFlag(kCpuHasLSX)) { |
| 4738 | SobelRow = SobelRow_Any_LSX; |
| 4739 | if (IS_ALIGNED(width, 16)) { |
| 4740 | SobelRow = SobelRow_LSX; |
| 4741 | } |
| 4742 | } |
| 4743 | #endif |
| 4744 | return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb, |
| 4745 | width, height, SobelRow); |
| 4746 | } |
| 4747 |
| 4748 | // Sobel ARGB effect with planar output. |
| 4749 | LIBYUV_API |
| 4750 | int ARGBSobelToPlane(const uint8_t* src_argb, |
| 4751 | int src_stride_argb, |
| 4752 | uint8_t* dst_y, |
| 4753 | int dst_stride_y, |
| 4754 | int width, |
| 4755 | int height) { |
| 4756 | void (*SobelToPlaneRow)(const uint8_t* src_sobelx, const uint8_t* src_sobely, |
| 4757 | uint8_t* dst_, int width) = SobelToPlaneRow_C; |
| 4758 | #if defined(HAS_SOBELTOPLANEROW_SSE2) |
| 4759 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 4760 | SobelToPlaneRow = SobelToPlaneRow_Any_SSE2; |
| 4761 | if (IS_ALIGNED(width, 16)) { |
| 4762 | SobelToPlaneRow = SobelToPlaneRow_SSE2; |
| 4763 | } |
| 4764 | } |
| 4765 | #endif |
| 4766 | #if defined(HAS_SOBELTOPLANEROW_NEON) |
| 4767 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4768 | SobelToPlaneRow = SobelToPlaneRow_Any_NEON; |
| 4769 | if (IS_ALIGNED(width, 16)) { |
| 4770 | SobelToPlaneRow = SobelToPlaneRow_NEON; |
| 4771 | } |
| 4772 | } |
| 4773 | #endif |
| 4774 | #if defined(HAS_SOBELTOPLANEROW_MSA) |
| 4775 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4776 | SobelToPlaneRow = SobelToPlaneRow_Any_MSA; |
| 4777 | if (IS_ALIGNED(width, 32)) { |
| 4778 | SobelToPlaneRow = SobelToPlaneRow_MSA; |
| 4779 | } |
| 4780 | } |
| 4781 | #endif |
| 4782 | #if defined(HAS_SOBELTOPLANEROW_LSX) |
| 4783 | if (TestCpuFlag(kCpuHasLSX)) { |
| 4784 | SobelToPlaneRow = SobelToPlaneRow_Any_LSX; |
| 4785 | if (IS_ALIGNED(width, 32)) { |
| 4786 | SobelToPlaneRow = SobelToPlaneRow_LSX; |
| 4787 | } |
| 4788 | } |
| 4789 | #endif |
| 4790 | return ARGBSobelize(src_argb, src_stride_argb, dst_y, dst_stride_y, width, |
| 4791 | height, SobelToPlaneRow); |
| 4792 | } |
| 4793 |
| 4794 | // SobelXY ARGB effect. |
| 4795 | // Similar to Sobel, but also stores Sobel X in R and Sobel Y in B. G = Sobel. |
| 4796 | LIBYUV_API |
| 4797 | int ARGBSobelXY(const uint8_t* src_argb, |
| 4798 | int src_stride_argb, |
| 4799 | uint8_t* dst_argb, |
| 4800 | int dst_stride_argb, |
| 4801 | int width, |
| 4802 | int height) { |
| 4803 | void (*SobelXYRow)(const uint8_t* src_sobelx, const uint8_t* src_sobely, |
| 4804 | uint8_t* dst_argb, int width) = SobelXYRow_C; |
| 4805 | #if defined(HAS_SOBELXYROW_SSE2) |
| 4806 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 4807 | SobelXYRow = SobelXYRow_Any_SSE2; |
| 4808 | if (IS_ALIGNED(width, 16)) { |
| 4809 | SobelXYRow = SobelXYRow_SSE2; |
| 4810 | } |
| 4811 | } |
| 4812 | #endif |
| 4813 | #if defined(HAS_SOBELXYROW_NEON) |
| 4814 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4815 | SobelXYRow = SobelXYRow_Any_NEON; |
| 4816 | if (IS_ALIGNED(width, 8)) { |
| 4817 | SobelXYRow = SobelXYRow_NEON; |
| 4818 | } |
| 4819 | } |
| 4820 | #endif |
| 4821 | #if defined(HAS_SOBELXYROW_MSA) |
| 4822 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4823 | SobelXYRow = SobelXYRow_Any_MSA; |
| 4824 | if (IS_ALIGNED(width, 16)) { |
| 4825 | SobelXYRow = SobelXYRow_MSA; |
| 4826 | } |
| 4827 | } |
| 4828 | #endif |
| 4829 | #if defined(HAS_SOBELXYROW_LSX) |
| 4830 | if (TestCpuFlag(kCpuHasLSX)) { |
| 4831 | SobelXYRow = SobelXYRow_Any_LSX; |
| 4832 | if (IS_ALIGNED(width, 16)) { |
| 4833 | SobelXYRow = SobelXYRow_LSX; |
| 4834 | } |
| 4835 | } |
| 4836 | #endif |
| 4837 | return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb, |
| 4838 | width, height, SobelXYRow); |
| 4839 | } |
| 4840 |
| 4841 | // Apply a 4x4 polynomial to each ARGB pixel. |
| 4842 | LIBYUV_API |
| 4843 | int ARGBPolynomial(const uint8_t* src_argb, |
| 4844 | int src_stride_argb, |
| 4845 | uint8_t* dst_argb, |
| 4846 | int dst_stride_argb, |
| 4847 | const float* poly, |
| 4848 | int width, |
| 4849 | int height) { |
| 4850 | int y; |
| 4851 | void (*ARGBPolynomialRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 4852 | const float* poly, int width) = ARGBPolynomialRow_C; |
| 4853 | if (!src_argb || !dst_argb || !poly || width <= 0 || height == 0) { |
| 4854 | return -1; |
| 4855 | } |
| 4856 | // Negative height means invert the image. |
| 4857 | if (height < 0) { |
| 4858 | height = -height; |
| 4859 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 4860 | src_stride_argb = -src_stride_argb; |
| 4861 | } |
| 4862 | // Coalesce rows. |
| 4863 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 4864 | width *= height; |
| 4865 | height = 1; |
| 4866 | src_stride_argb = dst_stride_argb = 0; |
| 4867 | } |
| 4868 | #if defined(HAS_ARGBPOLYNOMIALROW_SSE2) |
| 4869 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 2)) { |
| 4870 | ARGBPolynomialRow = ARGBPolynomialRow_SSE2; |
| 4871 | } |
| 4872 | #endif |
| 4873 | #if defined(HAS_ARGBPOLYNOMIALROW_AVX2) |
| 4874 | if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasFMA3) && |
| 4875 | IS_ALIGNED(width, 2)) { |
| 4876 | ARGBPolynomialRow = ARGBPolynomialRow_AVX2; |
| 4877 | } |
| 4878 | #endif |
| 4879 |
| 4880 | for (y = 0; y < height; ++y) { |
| 4881 | ARGBPolynomialRow(src_argb, dst_argb, poly, width); |
| 4882 | src_argb += src_stride_argb; |
| 4883 | dst_argb += dst_stride_argb; |
| 4884 | } |
| 4885 | return 0; |
| 4886 | } |
| 4887 |
| 4888 | // Convert plane of 16 bit shorts to half floats. |
| 4889 | // Source values are multiplied by scale before storing as half float. |
| 4890 | LIBYUV_API |
| 4891 | int HalfFloatPlane(const uint16_t* src_y, |
| 4892 | int src_stride_y, |
| 4893 | uint16_t* dst_y, |
| 4894 | int dst_stride_y, |
| 4895 | float scale, |
| 4896 | int width, |
| 4897 | int height) { |
| 4898 | int y; |
| 4899 | void (*HalfFloatRow)(const uint16_t* src, uint16_t* dst, float scale, |
| 4900 | int width) = HalfFloatRow_C; |
| 4901 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 4902 | return -1; |
| 4903 | } |
| 4904 | src_stride_y >>= 1; |
| 4905 | dst_stride_y >>= 1; |
| 4906 | // Negative height means invert the image. |
| 4907 | if (height < 0) { |
| 4908 | height = -height; |
| 4909 | src_y = src_y + (height - 1) * src_stride_y; |
| 4910 | src_stride_y = -src_stride_y; |
| 4911 | } |
| 4912 | // Coalesce rows. |
| 4913 | if (src_stride_y == width && dst_stride_y == width) { |
| 4914 | width *= height; |
| 4915 | height = 1; |
| 4916 | src_stride_y = dst_stride_y = 0; |
| 4917 | } |
| 4918 | #if defined(HAS_HALFFLOATROW_SSE2) |
| 4919 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 4920 | HalfFloatRow = HalfFloatRow_Any_SSE2; |
| 4921 | if (IS_ALIGNED(width, 8)) { |
| 4922 | HalfFloatRow = HalfFloatRow_SSE2; |
| 4923 | } |
| 4924 | } |
| 4925 | #endif |
| 4926 | #if defined(HAS_HALFFLOATROW_AVX2) |
| 4927 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 4928 | HalfFloatRow = HalfFloatRow_Any_AVX2; |
| 4929 | if (IS_ALIGNED(width, 16)) { |
| 4930 | HalfFloatRow = HalfFloatRow_AVX2; |
| 4931 | } |
| 4932 | } |
| 4933 | #endif |
| 4934 | #if defined(HAS_HALFFLOATROW_F16C) |
| 4935 | if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasF16C)) { |
| 4936 | HalfFloatRow = |
| 4937 | (scale == 1.0f) ? HalfFloat1Row_Any_F16C : HalfFloatRow_Any_F16C; |
| 4938 | if (IS_ALIGNED(width, 16)) { |
| 4939 | HalfFloatRow = (scale == 1.0f) ? HalfFloat1Row_F16C : HalfFloatRow_F16C; |
| 4940 | } |
| 4941 | } |
| 4942 | #endif |
| 4943 | #if defined(HAS_HALFFLOATROW_NEON) |
| 4944 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4945 | HalfFloatRow = |
| 4946 | (scale == 1.0f) ? HalfFloat1Row_Any_NEON : HalfFloatRow_Any_NEON; |
| 4947 | if (IS_ALIGNED(width, 8)) { |
| 4948 | HalfFloatRow = (scale == 1.0f) ? HalfFloat1Row_NEON : HalfFloatRow_NEON; |
| 4949 | } |
| 4950 | } |
| 4951 | #endif |
| 4952 | #if defined(HAS_HALFFLOATROW_MSA) |
| 4953 | if (TestCpuFlag(kCpuHasMSA)) { |
| 4954 | HalfFloatRow = HalfFloatRow_Any_MSA; |
| 4955 | if (IS_ALIGNED(width, 32)) { |
| 4956 | HalfFloatRow = HalfFloatRow_MSA; |
| 4957 | } |
| 4958 | } |
| 4959 | #endif |
| 4960 | #if defined(HAS_HALFFLOATROW_LSX) |
| 4961 | if (TestCpuFlag(kCpuHasLSX)) { |
| 4962 | HalfFloatRow = HalfFloatRow_Any_LSX; |
| 4963 | if (IS_ALIGNED(width, 32)) { |
| 4964 | HalfFloatRow = HalfFloatRow_LSX; |
| 4965 | } |
| 4966 | } |
| 4967 | #endif |
| 4968 |
| 4969 | for (y = 0; y < height; ++y) { |
| 4970 | HalfFloatRow(src_y, dst_y, scale, width); |
| 4971 | src_y += src_stride_y; |
| 4972 | dst_y += dst_stride_y; |
| 4973 | } |
| 4974 | return 0; |
| 4975 | } |
| 4976 |
| 4977 | // Convert a buffer of bytes to floats, scale the values and store as floats. |
| 4978 | LIBYUV_API |
| 4979 | int ByteToFloat(const uint8_t* src_y, float* dst_y, float scale, int width) { |
| 4980 | void (*ByteToFloatRow)(const uint8_t* src, float* dst, float scale, |
| 4981 | int width) = ByteToFloatRow_C; |
| 4982 | if (!src_y || !dst_y || width <= 0) { |
| 4983 | return -1; |
| 4984 | } |
| 4985 | #if defined(HAS_BYTETOFLOATROW_NEON) |
| 4986 | if (TestCpuFlag(kCpuHasNEON)) { |
| 4987 | ByteToFloatRow = ByteToFloatRow_Any_NEON; |
| 4988 | if (IS_ALIGNED(width, 8)) { |
| 4989 | ByteToFloatRow = ByteToFloatRow_NEON; |
| 4990 | } |
| 4991 | } |
| 4992 | #endif |
| 4993 |
| 4994 | ByteToFloatRow(src_y, dst_y, scale, width); |
| 4995 | return 0; |
| 4996 | } |
| 4997 |
| 4998 | // Apply a lumacolortable to each ARGB pixel. |
| 4999 | LIBYUV_API |
| 5000 | int ARGBLumaColorTable(const uint8_t* src_argb, |
| 5001 | int src_stride_argb, |
| 5002 | uint8_t* dst_argb, |
| 5003 | int dst_stride_argb, |
| 5004 | const uint8_t* luma, |
| 5005 | int width, |
| 5006 | int height) { |
| 5007 | int y; |
| 5008 | void (*ARGBLumaColorTableRow)( |
| 5009 | const uint8_t* src_argb, uint8_t* dst_argb, int width, |
| 5010 | const uint8_t* luma, const uint32_t lumacoeff) = ARGBLumaColorTableRow_C; |
| 5011 | if (!src_argb || !dst_argb || !luma || width <= 0 || height == 0) { |
| 5012 | return -1; |
| 5013 | } |
| 5014 | // Negative height means invert the image. |
| 5015 | if (height < 0) { |
| 5016 | height = -height; |
| 5017 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 5018 | src_stride_argb = -src_stride_argb; |
| 5019 | } |
| 5020 | // Coalesce rows. |
| 5021 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 5022 | width *= height; |
| 5023 | height = 1; |
| 5024 | src_stride_argb = dst_stride_argb = 0; |
| 5025 | } |
| 5026 | #if defined(HAS_ARGBLUMACOLORTABLEROW_SSSE3) |
| 5027 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4)) { |
| 5028 | ARGBLumaColorTableRow = ARGBLumaColorTableRow_SSSE3; |
| 5029 | } |
| 5030 | #endif |
| 5031 |
| 5032 | for (y = 0; y < height; ++y) { |
| 5033 | ARGBLumaColorTableRow(src_argb, dst_argb, width, luma, 0x00264b0f); |
| 5034 | src_argb += src_stride_argb; |
| 5035 | dst_argb += dst_stride_argb; |
| 5036 | } |
| 5037 | return 0; |
| 5038 | } |
| 5039 |
| 5040 | // Copy Alpha from one ARGB image to another. |
| 5041 | LIBYUV_API |
| 5042 | int ARGBCopyAlpha(const uint8_t* src_argb, |
| 5043 | int src_stride_argb, |
| 5044 | uint8_t* dst_argb, |
| 5045 | int dst_stride_argb, |
| 5046 | int width, |
| 5047 | int height) { |
| 5048 | int y; |
| 5049 | void (*ARGBCopyAlphaRow)(const uint8_t* src_argb, uint8_t* dst_argb, |
| 5050 | int width) = ARGBCopyAlphaRow_C; |
| 5051 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 5052 | return -1; |
| 5053 | } |
| 5054 | // Negative height means invert the image. |
| 5055 | if (height < 0) { |
| 5056 | height = -height; |
| 5057 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 5058 | src_stride_argb = -src_stride_argb; |
| 5059 | } |
| 5060 | // Coalesce rows. |
| 5061 | if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) { |
| 5062 | width *= height; |
| 5063 | height = 1; |
| 5064 | src_stride_argb = dst_stride_argb = 0; |
| 5065 | } |
| 5066 | #if defined(HAS_ARGBCOPYALPHAROW_SSE2) |
| 5067 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 5068 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_SSE2; |
| 5069 | if (IS_ALIGNED(width, 8)) { |
| 5070 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_SSE2; |
| 5071 | } |
| 5072 | } |
| 5073 | #endif |
| 5074 | #if defined(HAS_ARGBCOPYALPHAROW_AVX2) |
| 5075 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 5076 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_AVX2; |
| 5077 | if (IS_ALIGNED(width, 16)) { |
| 5078 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_AVX2; |
| 5079 | } |
| 5080 | } |
| 5081 | #endif |
| 5082 |
| 5083 | for (y = 0; y < height; ++y) { |
| 5084 | ARGBCopyAlphaRow(src_argb, dst_argb, width); |
| 5085 | src_argb += src_stride_argb; |
| 5086 | dst_argb += dst_stride_argb; |
| 5087 | } |
| 5088 | return 0; |
| 5089 | } |
| 5090 |
| 5091 | // Extract just the alpha channel from ARGB. |
| 5092 | LIBYUV_API |
| 5093 | int ARGBExtractAlpha(const uint8_t* src_argb, |
| 5094 | int src_stride_argb, |
| 5095 | uint8_t* dst_a, |
| 5096 | int dst_stride_a, |
| 5097 | int width, |
| 5098 | int height) { |
| 5099 | if (!src_argb || !dst_a || width <= 0 || height == 0) { |
| 5100 | return -1; |
| 5101 | } |
| 5102 | // Negative height means invert the image. |
| 5103 | if (height < 0) { |
| 5104 | height = -height; |
| 5105 | src_argb += (height - 1) * src_stride_argb; |
| 5106 | src_stride_argb = -src_stride_argb; |
| 5107 | } |
| 5108 | // Coalesce rows. |
| 5109 | if (src_stride_argb == width * 4 && dst_stride_a == width) { |
| 5110 | width *= height; |
| 5111 | height = 1; |
| 5112 | src_stride_argb = dst_stride_a = 0; |
| 5113 | } |
| 5114 | void (*ARGBExtractAlphaRow)(const uint8_t* src_argb, uint8_t* dst_a, |
| 5115 | int width) = ARGBExtractAlphaRow_C; |
| 5116 | #if defined(HAS_ARGBEXTRACTALPHAROW_SSE2) |
| 5117 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 5118 | ARGBExtractAlphaRow = IS_ALIGNED(width, 8) ? ARGBExtractAlphaRow_SSE2 |
| 5119 | : ARGBExtractAlphaRow_Any_SSE2; |
| 5120 | } |
| 5121 | #endif |
| 5122 | #if defined(HAS_ARGBEXTRACTALPHAROW_AVX2) |
| 5123 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 5124 | ARGBExtractAlphaRow = IS_ALIGNED(width, 32) ? ARGBExtractAlphaRow_AVX2 |
| 5125 | : ARGBExtractAlphaRow_Any_AVX2; |
| 5126 | } |
| 5127 | #endif |
| 5128 | #if defined(HAS_ARGBEXTRACTALPHAROW_NEON) |
| 5129 | if (TestCpuFlag(kCpuHasNEON)) { |
| 5130 | ARGBExtractAlphaRow = IS_ALIGNED(width, 16) ? ARGBExtractAlphaRow_NEON |
| 5131 | : ARGBExtractAlphaRow_Any_NEON; |
| 5132 | } |
| 5133 | #endif |
| 5134 | #if defined(HAS_ARGBEXTRACTALPHAROW_MSA) |
| 5135 | if (TestCpuFlag(kCpuHasMSA)) { |
| 5136 | ARGBExtractAlphaRow = IS_ALIGNED(width, 16) ? ARGBExtractAlphaRow_MSA |
| 5137 | : ARGBExtractAlphaRow_Any_MSA; |
| 5138 | } |
| 5139 | #endif |
| 5140 | #if defined(HAS_ARGBEXTRACTALPHAROW_LSX) |
| 5141 | if (TestCpuFlag(kCpuHasLSX)) { |
| 5142 | ARGBExtractAlphaRow = IS_ALIGNED(width, 16) ? ARGBExtractAlphaRow_LSX |
| 5143 | : ARGBExtractAlphaRow_Any_LSX; |
| 5144 | } |
| 5145 | #endif |
| 5146 |
| 5147 | for (int y = 0; y < height; ++y) { |
| 5148 | ARGBExtractAlphaRow(src_argb, dst_a, width); |
| 5149 | src_argb += src_stride_argb; |
| 5150 | dst_a += dst_stride_a; |
| 5151 | } |
| 5152 | return 0; |
| 5153 | } |
| 5154 |
| 5155 | // Copy a planar Y channel to the alpha channel of a destination ARGB image. |
| 5156 | LIBYUV_API |
| 5157 | int ARGBCopyYToAlpha(const uint8_t* src_y, |
| 5158 | int src_stride_y, |
| 5159 | uint8_t* dst_argb, |
| 5160 | int dst_stride_argb, |
| 5161 | int width, |
| 5162 | int height) { |
| 5163 | int y; |
| 5164 | void (*ARGBCopyYToAlphaRow)(const uint8_t* src_y, uint8_t* dst_argb, |
| 5165 | int width) = ARGBCopyYToAlphaRow_C; |
| 5166 | if (!src_y || !dst_argb || width <= 0 || height == 0) { |
| 5167 | return -1; |
| 5168 | } |
| 5169 | // Negative height means invert the image. |
| 5170 | if (height < 0) { |
| 5171 | height = -height; |
| 5172 | src_y = src_y + (height - 1) * src_stride_y; |
| 5173 | src_stride_y = -src_stride_y; |
| 5174 | } |
| 5175 | // Coalesce rows. |
| 5176 | if (src_stride_y == width && dst_stride_argb == width * 4) { |
| 5177 | width *= height; |
| 5178 | height = 1; |
| 5179 | src_stride_y = dst_stride_argb = 0; |
| 5180 | } |
| 5181 | #if defined(HAS_ARGBCOPYYTOALPHAROW_SSE2) |
| 5182 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 5183 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_SSE2; |
| 5184 | if (IS_ALIGNED(width, 8)) { |
| 5185 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_SSE2; |
| 5186 | } |
| 5187 | } |
| 5188 | #endif |
| 5189 | #if defined(HAS_ARGBCOPYYTOALPHAROW_AVX2) |
| 5190 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 5191 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_AVX2; |
| 5192 | if (IS_ALIGNED(width, 16)) { |
| 5193 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_AVX2; |
| 5194 | } |
| 5195 | } |
| 5196 | #endif |
| 5197 |
| 5198 | for (y = 0; y < height; ++y) { |
| 5199 | ARGBCopyYToAlphaRow(src_y, dst_argb, width); |
| 5200 | src_y += src_stride_y; |
| 5201 | dst_argb += dst_stride_argb; |
| 5202 | } |
| 5203 | return 0; |
| 5204 | } |
| 5205 |
| 5206 | LIBYUV_API |
| 5207 | int YUY2ToNV12(const uint8_t* src_yuy2, |
| 5208 | int src_stride_yuy2, |
| 5209 | uint8_t* dst_y, |
| 5210 | int dst_stride_y, |
| 5211 | uint8_t* dst_uv, |
| 5212 | int dst_stride_uv, |
| 5213 | int width, |
| 5214 | int height) { |
| 5215 | int y; |
| 5216 | void (*YUY2ToYRow)(const uint8_t* src_yuy2, uint8_t* dst_y, int width) = |
| 5217 | YUY2ToYRow_C; |
| 5218 | void (*YUY2ToNVUVRow)(const uint8_t* src_yuy2, int stride_yuy2, |
| 5219 | uint8_t* dst_uv, int width) = YUY2ToNVUVRow_C; |
| 5220 | if (!src_yuy2 || !dst_y || !dst_uv || width <= 0 || height == 0) { |
| 5221 | return -1; |
| 5222 | } |
| 5223 |
| 5224 | // Negative height means invert the image. |
| 5225 | if (height < 0) { |
| 5226 | height = -height; |
| 5227 | src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2; |
| 5228 | src_stride_yuy2 = -src_stride_yuy2; |
| 5229 | } |
| 5230 | #if defined(HAS_YUY2TOYROW_SSE2) |
| 5231 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 5232 | YUY2ToYRow = YUY2ToYRow_Any_SSE2; |
| 5233 | if (IS_ALIGNED(width, 16)) { |
| 5234 | YUY2ToYRow = YUY2ToYRow_SSE2; |
| 5235 | } |
| 5236 | } |
| 5237 | #endif |
| 5238 | #if defined(HAS_YUY2TOYROW_AVX2) |
| 5239 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 5240 | YUY2ToYRow = YUY2ToYRow_Any_AVX2; |
| 5241 | if (IS_ALIGNED(width, 32)) { |
| 5242 | YUY2ToYRow = YUY2ToYRow_AVX2; |
| 5243 | } |
| 5244 | } |
| 5245 | #endif |
| 5246 | #if defined(HAS_YUY2TOYROW_NEON) |
| 5247 | if (TestCpuFlag(kCpuHasNEON)) { |
| 5248 | YUY2ToYRow = YUY2ToYRow_Any_NEON; |
| 5249 | if (IS_ALIGNED(width, 16)) { |
| 5250 | YUY2ToYRow = YUY2ToYRow_NEON; |
| 5251 | } |
| 5252 | } |
| 5253 | #endif |
| 5254 | #if defined(HAS_YUY2TOYROW_MSA) && defined(HAS_YUY2TOUV422ROW_MSA) |
| 5255 | if (TestCpuFlag(kCpuHasMSA)) { |
| 5256 | YUY2ToYRow = YUY2ToYRow_Any_MSA; |
| 5257 | if (IS_ALIGNED(width, 32)) { |
| 5258 | YUY2ToYRow = YUY2ToYRow_MSA; |
| 5259 | } |
| 5260 | } |
| 5261 | #endif |
| 5262 | #if defined(HAS_YUY2TOYROW_LASX) && defined(HAS_YUY2TOUV422ROW_LASX) |
| 5263 | if (TestCpuFlag(kCpuHasLASX)) { |
| 5264 | YUY2ToYRow = YUY2ToYRow_Any_LASX; |
| 5265 | if (IS_ALIGNED(width, 32)) { |
| 5266 | YUY2ToYRow = YUY2ToYRow_LASX; |
| 5267 | } |
| 5268 | } |
| 5269 | #endif |
| 5270 |
| 5271 | #if defined(HAS_YUY2TONVUVROW_SSE2) |
| 5272 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 5273 | YUY2ToNVUVRow = YUY2ToNVUVRow_Any_SSE2; |
| 5274 | if (IS_ALIGNED(width, 16)) { |
| 5275 | YUY2ToNVUVRow = YUY2ToNVUVRow_SSE2; |
| 5276 | } |
| 5277 | } |
| 5278 | #endif |
| 5279 | #if defined(HAS_YUY2TONVUVROW_AVX2) |
| 5280 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 5281 | YUY2ToNVUVRow = YUY2ToNVUVRow_Any_AVX2; |
| 5282 | if (IS_ALIGNED(width, 32)) { |
| 5283 | YUY2ToNVUVRow = YUY2ToNVUVRow_AVX2; |
| 5284 | } |
| 5285 | } |
| 5286 | #endif |
| 5287 | #if defined(HAS_YUY2TONVUVROW_NEON) |
| 5288 | if (TestCpuFlag(kCpuHasNEON)) { |
| 5289 | YUY2ToNVUVRow = YUY2ToNVUVRow_Any_NEON; |
| 5290 | if (IS_ALIGNED(width, 16)) { |
| 5291 | YUY2ToNVUVRow = YUY2ToNVUVRow_NEON; |
| 5292 | } |
| 5293 | } |
| 5294 | #endif |
| 5295 |
| 5296 | for (y = 0; y < height - 1; y += 2) { |
| 5297 | YUY2ToYRow(src_yuy2, dst_y, width); |
| 5298 | YUY2ToYRow(src_yuy2 + src_stride_yuy2, dst_y + dst_stride_y, width); |
| 5299 | YUY2ToNVUVRow(src_yuy2, src_stride_yuy2, dst_uv, width); |
| 5300 | src_yuy2 += src_stride_yuy2 * 2; |
| 5301 | dst_y += dst_stride_y * 2; |
| 5302 | dst_uv += dst_stride_uv; |
| 5303 | } |
| 5304 | if (height & 1) { |
| 5305 | YUY2ToYRow(src_yuy2, dst_y, width); |
| 5306 | YUY2ToNVUVRow(src_yuy2, 0, dst_uv, width); |
| 5307 | } |
| 5308 | return 0; |
| 5309 | } |
| 5310 |
| 5311 | LIBYUV_API |
| 5312 | int UYVYToNV12(const uint8_t* src_uyvy, |
| 5313 | int src_stride_uyvy, |
| 5314 | uint8_t* dst_y, |
| 5315 | int dst_stride_y, |
| 5316 | uint8_t* dst_uv, |
| 5317 | int dst_stride_uv, |
| 5318 | int width, |
| 5319 | int height) { |
| 5320 | int y; |
| 5321 | int halfwidth = (width + 1) >> 1; |
| 5322 | void (*SplitUVRow)(const uint8_t* src_uv, uint8_t* dst_u, uint8_t* dst_v, |
| 5323 | int width) = SplitUVRow_C; |
| 5324 | void (*InterpolateRow)(uint8_t* dst_ptr, const uint8_t* src_ptr, |
| 5325 | ptrdiff_t src_stride, int dst_width, |
| 5326 | int source_y_fraction) = InterpolateRow_C; |
| 5327 |
| 5328 | if (!src_uyvy || !dst_y || !dst_uv || width <= 0 || height == 0) { |
| 5329 | return -1; |
| 5330 | } |
| 5331 |
| 5332 | // Negative height means invert the image. |
| 5333 | if (height < 0) { |
| 5334 | height = -height; |
| 5335 | src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy; |
| 5336 | src_stride_uyvy = -src_stride_uyvy; |
| 5337 | } |
| 5338 | #if defined(HAS_SPLITUVROW_SSE2) |
| 5339 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 5340 | SplitUVRow = SplitUVRow_Any_SSE2; |
| 5341 | if (IS_ALIGNED(width, 16)) { |
| 5342 | SplitUVRow = SplitUVRow_SSE2; |
| 5343 | } |
| 5344 | } |
| 5345 | #endif |
| 5346 | #if defined(HAS_SPLITUVROW_AVX2) |
| 5347 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 5348 | SplitUVRow = SplitUVRow_Any_AVX2; |
| 5349 | if (IS_ALIGNED(width, 32)) { |
| 5350 | SplitUVRow = SplitUVRow_AVX2; |
| 5351 | } |
| 5352 | } |
| 5353 | #endif |
| 5354 | #if defined(HAS_SPLITUVROW_NEON) |
| 5355 | if (TestCpuFlag(kCpuHasNEON)) { |
| 5356 | SplitUVRow = SplitUVRow_Any_NEON; |
| 5357 | if (IS_ALIGNED(width, 16)) { |
| 5358 | SplitUVRow = SplitUVRow_NEON; |
| 5359 | } |
| 5360 | } |
| 5361 | #endif |
| 5362 | #if defined(HAS_SPLITUVROW_MSA) |
| 5363 | if (TestCpuFlag(kCpuHasMSA)) { |
| 5364 | SplitUVRow = SplitUVRow_Any_MSA; |
| 5365 | if (IS_ALIGNED(width, 32)) { |
| 5366 | SplitUVRow = SplitUVRow_MSA; |
| 5367 | } |
| 5368 | } |
| 5369 | #endif |
| 5370 | #if defined(HAS_SPLITUVROW_LSX) |
| 5371 | if (TestCpuFlag(kCpuHasLSX)) { |
| 5372 | SplitUVRow = SplitUVRow_Any_LSX; |
| 5373 | if (IS_ALIGNED(width, 32)) { |
| 5374 | SplitUVRow = SplitUVRow_LSX; |
| 5375 | } |
| 5376 | } |
| 5377 | #endif |
| 5378 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 5379 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 5380 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 5381 | if (IS_ALIGNED(width, 16)) { |
| 5382 | InterpolateRow = InterpolateRow_SSSE3; |
| 5383 | } |
| 5384 | } |
| 5385 | #endif |
| 5386 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 5387 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 5388 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 5389 | if (IS_ALIGNED(width, 32)) { |
| 5390 | InterpolateRow = InterpolateRow_AVX2; |
| 5391 | } |
| 5392 | } |
| 5393 | #endif |
| 5394 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 5395 | if (TestCpuFlag(kCpuHasNEON)) { |
| 5396 | InterpolateRow = InterpolateRow_Any_NEON; |
| 5397 | if (IS_ALIGNED(width, 16)) { |
| 5398 | InterpolateRow = InterpolateRow_NEON; |
| 5399 | } |
| 5400 | } |
| 5401 | #endif |
| 5402 | #if defined(HAS_INTERPOLATEROW_MSA) |
| 5403 | if (TestCpuFlag(kCpuHasMSA)) { |
| 5404 | InterpolateRow = InterpolateRow_Any_MSA; |
| 5405 | if (IS_ALIGNED(width, 32)) { |
| 5406 | InterpolateRow = InterpolateRow_MSA; |
| 5407 | } |
| 5408 | } |
| 5409 | #endif |
| 5410 | #if defined(HAS_INTERPOLATEROW_LSX) |
| 5411 | if (TestCpuFlag(kCpuHasLSX)) { |
| 5412 | InterpolateRow = InterpolateRow_Any_LSX; |
| 5413 | if (IS_ALIGNED(width, 32)) { |
| 5414 | InterpolateRow = InterpolateRow_LSX; |
| 5415 | } |
| 5416 | } |
| 5417 | #endif |
| 5418 |
| 5419 | { |
| 5420 | int awidth = halfwidth * 2; |
| 5421 | // row of y and 2 rows of uv |
| 5422 | align_buffer_64(rows, awidth * 3); |
| 5423 |
| 5424 | for (y = 0; y < height - 1; y += 2) { |
| 5425 | // Split Y from UV. |
| 5426 | SplitUVRow(src_uyvy, rows + awidth, rows, awidth); |
| 5427 | memcpy(dst_y, rows, width); |
| 5428 | SplitUVRow(src_uyvy + src_stride_uyvy, rows + awidth * 2, rows, awidth); |
| 5429 | memcpy(dst_y + dst_stride_y, rows, width); |
| 5430 | InterpolateRow(dst_uv, rows + awidth, awidth, awidth, 128); |
| 5431 | src_uyvy += src_stride_uyvy * 2; |
| 5432 | dst_y += dst_stride_y * 2; |
| 5433 | dst_uv += dst_stride_uv; |
| 5434 | } |
| 5435 | if (height & 1) { |
| 5436 | // Split Y from UV. |
| 5437 | SplitUVRow(src_uyvy, dst_uv, rows, awidth); |
| 5438 | memcpy(dst_y, rows, width); |
| 5439 | } |
| 5440 | free_aligned_buffer_64(rows); |
| 5441 | } |
| 5442 | return 0; |
| 5443 | } |
| 5444 |
| 5445 | // width and height are src size allowing odd size handling. |
| 5446 | LIBYUV_API |
| 5447 | void HalfMergeUVPlane(const uint8_t* src_u, |
| 5448 | int src_stride_u, |
| 5449 | const uint8_t* src_v, |
| 5450 | int src_stride_v, |
| 5451 | uint8_t* dst_uv, |
| 5452 | int dst_stride_uv, |
| 5453 | int width, |
| 5454 | int height) { |
| 5455 | int y; |
| 5456 | void (*HalfMergeUVRow)(const uint8_t* src_u, int src_stride_u, |
| 5457 | const uint8_t* src_v, int src_stride_v, |
| 5458 | uint8_t* dst_uv, int width) = HalfMergeUVRow_C; |
| 5459 |
| 5460 | // Negative height means invert the image. |
| 5461 | if (height < 0) { |
| 5462 | height = -height; |
| 5463 | src_u = src_u + (height - 1) * src_stride_u; |
| 5464 | src_v = src_v + (height - 1) * src_stride_v; |
| 5465 | src_stride_u = -src_stride_u; |
| 5466 | src_stride_v = -src_stride_v; |
| 5467 | } |
| 5468 | #if defined(HAS_HALFMERGEUVROW_NEON) |
| 5469 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16)) { |
| 5470 | HalfMergeUVRow = HalfMergeUVRow_NEON; |
| 5471 | } |
| 5472 | #endif |
| 5473 | #if defined(HAS_HALFMERGEUVROW_SSSE3) |
| 5474 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 16)) { |
| 5475 | HalfMergeUVRow = HalfMergeUVRow_SSSE3; |
| 5476 | } |
| 5477 | #endif |
| 5478 | #if defined(HAS_HALFMERGEUVROW_AVX2) |
| 5479 | if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 32)) { |
| 5480 | HalfMergeUVRow = HalfMergeUVRow_AVX2; |
| 5481 | } |
| 5482 | #endif |
| 5483 | for (y = 0; y < height - 1; y += 2) { |
| 5484 | // Merge a row of U and V into a row of UV. |
| 5485 | HalfMergeUVRow(src_u, src_stride_u, src_v, src_stride_v, dst_uv, width); |
| 5486 | src_u += src_stride_u * 2; |
| 5487 | src_v += src_stride_v * 2; |
| 5488 | dst_uv += dst_stride_uv; |
| 5489 | } |
| 5490 | if (height & 1) { |
| 5491 | HalfMergeUVRow(src_u, 0, src_v, 0, dst_uv, width); |
| 5492 | } |
| 5493 | } |
| 5494 |
| 5495 | #ifdef __cplusplus |
| 5496 | } // extern "C" |
| 5497 | } // namespace libyuv |
| 5498 | #endif |
| 5499 |