| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "libyuv/planar_functions.h" |
| 12 |
| 13 | #include <string.h> // for memset() |
| 14 |
| 15 | #include "libyuv/cpu_id.h" |
| 16 | #ifdef HAVE_JPEG |
| 17 | #include "libyuv/mjpeg_decoder.h" |
| 18 | #endif |
| 19 | #include "libyuv/row.h" |
| 20 | #include "libyuv/scale_row.h" // for ScaleRowDown2 |
| 21 |
| 22 | #ifdef __cplusplus |
| 23 | namespace libyuv { |
| 24 | extern "C" { |
| 25 | #endif |
| 26 |
| 27 | // Copy a plane of data |
| 28 | LIBYUV_API |
| 29 | void CopyPlane(const uint8* src_y, int src_stride_y, |
| 30 | uint8* dst_y, int dst_stride_y, |
| 31 | int width, int height) { |
| 32 | int y; |
| 33 | void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C; |
| 34 | // Coalesce rows. |
| 35 | if (src_stride_y == width && |
| 36 | dst_stride_y == width) { |
| 37 | width *= height; |
| 38 | height = 1; |
| 39 | src_stride_y = dst_stride_y = 0; |
| 40 | } |
| 41 | // Nothing to do. |
| 42 | if (src_y == dst_y && src_stride_y == dst_stride_y) { |
| 43 | return; |
| 44 | } |
| 45 | #if defined(HAS_COPYROW_SSE2) |
| 46 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 47 | CopyRow = IS_ALIGNED(width, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2; |
| 48 | } |
| 49 | #endif |
| 50 | #if defined(HAS_COPYROW_AVX) |
| 51 | if (TestCpuFlag(kCpuHasAVX)) { |
| 52 | CopyRow = IS_ALIGNED(width, 64) ? CopyRow_AVX : CopyRow_Any_AVX; |
| 53 | } |
| 54 | #endif |
| 55 | #if defined(HAS_COPYROW_ERMS) |
| 56 | if (TestCpuFlag(kCpuHasERMS)) { |
| 57 | CopyRow = CopyRow_ERMS; |
| 58 | } |
| 59 | #endif |
| 60 | #if defined(HAS_COPYROW_NEON) |
| 61 | if (TestCpuFlag(kCpuHasNEON)) { |
| 62 | CopyRow = IS_ALIGNED(width, 32) ? CopyRow_NEON : CopyRow_Any_NEON; |
| 63 | } |
| 64 | #endif |
| 65 | #if defined(HAS_COPYROW_MIPS) |
| 66 | if (TestCpuFlag(kCpuHasMIPS)) { |
| 67 | CopyRow = CopyRow_MIPS; |
| 68 | } |
| 69 | #endif |
| 70 |
| 71 | // Copy plane |
| 72 | for (y = 0; y < height; ++y) { |
| 73 | CopyRow(src_y, dst_y, width); |
| 74 | src_y += src_stride_y; |
| 75 | dst_y += dst_stride_y; |
| 76 | } |
| 77 | } |
| 78 |
| 79 | LIBYUV_API |
| 80 | void CopyPlane_16(const uint16* src_y, int src_stride_y, |
| 81 | uint16* dst_y, int dst_stride_y, |
| 82 | int width, int height) { |
| 83 | int y; |
| 84 | void (*CopyRow)(const uint16* src, uint16* dst, int width) = CopyRow_16_C; |
| 85 | // Coalesce rows. |
| 86 | if (src_stride_y == width && |
| 87 | dst_stride_y == width) { |
| 88 | width *= height; |
| 89 | height = 1; |
| 90 | src_stride_y = dst_stride_y = 0; |
| 91 | } |
| 92 | #if defined(HAS_COPYROW_16_SSE2) |
| 93 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 32)) { |
| 94 | CopyRow = CopyRow_16_SSE2; |
| 95 | } |
| 96 | #endif |
| 97 | #if defined(HAS_COPYROW_16_ERMS) |
| 98 | if (TestCpuFlag(kCpuHasERMS)) { |
| 99 | CopyRow = CopyRow_16_ERMS; |
| 100 | } |
| 101 | #endif |
| 102 | #if defined(HAS_COPYROW_16_NEON) |
| 103 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 32)) { |
| 104 | CopyRow = CopyRow_16_NEON; |
| 105 | } |
| 106 | #endif |
| 107 | #if defined(HAS_COPYROW_16_MIPS) |
| 108 | if (TestCpuFlag(kCpuHasMIPS)) { |
| 109 | CopyRow = CopyRow_16_MIPS; |
| 110 | } |
| 111 | #endif |
| 112 |
| 113 | // Copy plane |
| 114 | for (y = 0; y < height; ++y) { |
| 115 | CopyRow(src_y, dst_y, width); |
| 116 | src_y += src_stride_y; |
| 117 | dst_y += dst_stride_y; |
| 118 | } |
| 119 | } |
| 120 |
| 121 | // Copy I422. |
| 122 | LIBYUV_API |
| 123 | int I422Copy(const uint8* src_y, int src_stride_y, |
| 124 | const uint8* src_u, int src_stride_u, |
| 125 | const uint8* src_v, int src_stride_v, |
| 126 | uint8* dst_y, int dst_stride_y, |
| 127 | uint8* dst_u, int dst_stride_u, |
| 128 | uint8* dst_v, int dst_stride_v, |
| 129 | int width, int height) { |
| 130 | int halfwidth = (width + 1) >> 1; |
| 131 | if (!src_y || !src_u || !src_v || |
| 132 | !dst_y || !dst_u || !dst_v || |
| 133 | width <= 0 || height == 0) { |
| 134 | return -1; |
| 135 | } |
| 136 | // Negative height means invert the image. |
| 137 | if (height < 0) { |
| 138 | height = -height; |
| 139 | src_y = src_y + (height - 1) * src_stride_y; |
| 140 | src_u = src_u + (height - 1) * src_stride_u; |
| 141 | src_v = src_v + (height - 1) * src_stride_v; |
| 142 | src_stride_y = -src_stride_y; |
| 143 | src_stride_u = -src_stride_u; |
| 144 | src_stride_v = -src_stride_v; |
| 145 | } |
| 146 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 147 | CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height); |
| 148 | CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height); |
| 149 | return 0; |
| 150 | } |
| 151 |
| 152 | // Copy I444. |
| 153 | LIBYUV_API |
| 154 | int I444Copy(const uint8* src_y, int src_stride_y, |
| 155 | const uint8* src_u, int src_stride_u, |
| 156 | const uint8* src_v, int src_stride_v, |
| 157 | uint8* dst_y, int dst_stride_y, |
| 158 | uint8* dst_u, int dst_stride_u, |
| 159 | uint8* dst_v, int dst_stride_v, |
| 160 | int width, int height) { |
| 161 | if (!src_y || !src_u || !src_v || |
| 162 | !dst_y || !dst_u || !dst_v || |
| 163 | width <= 0 || height == 0) { |
| 164 | return -1; |
| 165 | } |
| 166 | // Negative height means invert the image. |
| 167 | if (height < 0) { |
| 168 | height = -height; |
| 169 | src_y = src_y + (height - 1) * src_stride_y; |
| 170 | src_u = src_u + (height - 1) * src_stride_u; |
| 171 | src_v = src_v + (height - 1) * src_stride_v; |
| 172 | src_stride_y = -src_stride_y; |
| 173 | src_stride_u = -src_stride_u; |
| 174 | src_stride_v = -src_stride_v; |
| 175 | } |
| 176 |
| 177 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 178 | CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height); |
| 179 | CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height); |
| 180 | return 0; |
| 181 | } |
| 182 |
| 183 | // Copy I400. |
| 184 | LIBYUV_API |
| 185 | int I400ToI400(const uint8* src_y, int src_stride_y, |
| 186 | uint8* dst_y, int dst_stride_y, |
| 187 | int width, int height) { |
| 188 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 189 | return -1; |
| 190 | } |
| 191 | // Negative height means invert the image. |
| 192 | if (height < 0) { |
| 193 | height = -height; |
| 194 | src_y = src_y + (height - 1) * src_stride_y; |
| 195 | src_stride_y = -src_stride_y; |
| 196 | } |
| 197 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 198 | return 0; |
| 199 | } |
| 200 |
| 201 | // Convert I420 to I400. |
| 202 | LIBYUV_API |
| 203 | int I420ToI400(const uint8* src_y, int src_stride_y, |
| 204 | const uint8* src_u, int src_stride_u, |
| 205 | const uint8* src_v, int src_stride_v, |
| 206 | uint8* dst_y, int dst_stride_y, |
| 207 | int width, int height) { |
| 208 | if (!src_y || !dst_y || width <= 0 || height == 0) { |
| 209 | return -1; |
| 210 | } |
| 211 | // Negative height means invert the image. |
| 212 | if (height < 0) { |
| 213 | height = -height; |
| 214 | src_y = src_y + (height - 1) * src_stride_y; |
| 215 | src_stride_y = -src_stride_y; |
| 216 | } |
| 217 | CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 218 | return 0; |
| 219 | } |
| 220 |
| 221 | // Mirror a plane of data. |
| 222 | void MirrorPlane(const uint8* src_y, int src_stride_y, |
| 223 | uint8* dst_y, int dst_stride_y, |
| 224 | int width, int height) { |
| 225 | int y; |
| 226 | void (*MirrorRow)(const uint8* src, uint8* dst, int width) = MirrorRow_C; |
| 227 | // Negative height means invert the image. |
| 228 | if (height < 0) { |
| 229 | height = -height; |
| 230 | src_y = src_y + (height - 1) * src_stride_y; |
| 231 | src_stride_y = -src_stride_y; |
| 232 | } |
| 233 | #if defined(HAS_MIRRORROW_NEON) |
| 234 | if (TestCpuFlag(kCpuHasNEON)) { |
| 235 | MirrorRow = MirrorRow_Any_NEON; |
| 236 | if (IS_ALIGNED(width, 16)) { |
| 237 | MirrorRow = MirrorRow_NEON; |
| 238 | } |
| 239 | } |
| 240 | #endif |
| 241 | #if defined(HAS_MIRRORROW_SSSE3) |
| 242 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 243 | MirrorRow = MirrorRow_Any_SSSE3; |
| 244 | if (IS_ALIGNED(width, 16)) { |
| 245 | MirrorRow = MirrorRow_SSSE3; |
| 246 | } |
| 247 | } |
| 248 | #endif |
| 249 | #if defined(HAS_MIRRORROW_AVX2) |
| 250 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 251 | MirrorRow = MirrorRow_Any_AVX2; |
| 252 | if (IS_ALIGNED(width, 32)) { |
| 253 | MirrorRow = MirrorRow_AVX2; |
| 254 | } |
| 255 | } |
| 256 | #endif |
| 257 | // TODO(fbarchard): Mirror on mips handle unaligned memory. |
| 258 | #if defined(HAS_MIRRORROW_MIPS_DSPR2) |
| 259 | if (TestCpuFlag(kCpuHasMIPS_DSPR2) && |
| 260 | IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) && |
| 261 | IS_ALIGNED(dst_y, 4) && IS_ALIGNED(dst_stride_y, 4)) { |
| 262 | MirrorRow = MirrorRow_MIPS_DSPR2; |
| 263 | } |
| 264 | #endif |
| 265 |
| 266 | // Mirror plane |
| 267 | for (y = 0; y < height; ++y) { |
| 268 | MirrorRow(src_y, dst_y, width); |
| 269 | src_y += src_stride_y; |
| 270 | dst_y += dst_stride_y; |
| 271 | } |
| 272 | } |
| 273 |
| 274 | // Convert YUY2 to I422. |
| 275 | LIBYUV_API |
| 276 | int YUY2ToI422(const uint8* src_yuy2, int src_stride_yuy2, |
| 277 | uint8* dst_y, int dst_stride_y, |
| 278 | uint8* dst_u, int dst_stride_u, |
| 279 | uint8* dst_v, int dst_stride_v, |
| 280 | int width, int height) { |
| 281 | int y; |
| 282 | void (*YUY2ToUV422Row)(const uint8* src_yuy2, |
| 283 | uint8* dst_u, uint8* dst_v, int width) = |
| 284 | YUY2ToUV422Row_C; |
| 285 | void (*YUY2ToYRow)(const uint8* src_yuy2, uint8* dst_y, int width) = |
| 286 | YUY2ToYRow_C; |
| 287 | // Negative height means invert the image. |
| 288 | if (height < 0) { |
| 289 | height = -height; |
| 290 | src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2; |
| 291 | src_stride_yuy2 = -src_stride_yuy2; |
| 292 | } |
| 293 | // Coalesce rows. |
| 294 | if (src_stride_yuy2 == width * 2 && |
| 295 | dst_stride_y == width && |
| 296 | dst_stride_u * 2 == width && |
| 297 | dst_stride_v * 2 == width) { |
| 298 | width *= height; |
| 299 | height = 1; |
| 300 | src_stride_yuy2 = dst_stride_y = dst_stride_u = dst_stride_v = 0; |
| 301 | } |
| 302 | #if defined(HAS_YUY2TOYROW_SSE2) |
| 303 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 304 | YUY2ToUV422Row = YUY2ToUV422Row_Any_SSE2; |
| 305 | YUY2ToYRow = YUY2ToYRow_Any_SSE2; |
| 306 | if (IS_ALIGNED(width, 16)) { |
| 307 | YUY2ToUV422Row = YUY2ToUV422Row_SSE2; |
| 308 | YUY2ToYRow = YUY2ToYRow_SSE2; |
| 309 | } |
| 310 | } |
| 311 | #endif |
| 312 | #if defined(HAS_YUY2TOYROW_AVX2) |
| 313 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 314 | YUY2ToUV422Row = YUY2ToUV422Row_Any_AVX2; |
| 315 | YUY2ToYRow = YUY2ToYRow_Any_AVX2; |
| 316 | if (IS_ALIGNED(width, 32)) { |
| 317 | YUY2ToUV422Row = YUY2ToUV422Row_AVX2; |
| 318 | YUY2ToYRow = YUY2ToYRow_AVX2; |
| 319 | } |
| 320 | } |
| 321 | #endif |
| 322 | #if defined(HAS_YUY2TOYROW_NEON) |
| 323 | if (TestCpuFlag(kCpuHasNEON)) { |
| 324 | YUY2ToYRow = YUY2ToYRow_Any_NEON; |
| 325 | if (width >= 16) { |
| 326 | YUY2ToUV422Row = YUY2ToUV422Row_Any_NEON; |
| 327 | } |
| 328 | if (IS_ALIGNED(width, 16)) { |
| 329 | YUY2ToYRow = YUY2ToYRow_NEON; |
| 330 | YUY2ToUV422Row = YUY2ToUV422Row_NEON; |
| 331 | } |
| 332 | } |
| 333 | #endif |
| 334 |
| 335 | for (y = 0; y < height; ++y) { |
| 336 | YUY2ToUV422Row(src_yuy2, dst_u, dst_v, width); |
| 337 | YUY2ToYRow(src_yuy2, dst_y, width); |
| 338 | src_yuy2 += src_stride_yuy2; |
| 339 | dst_y += dst_stride_y; |
| 340 | dst_u += dst_stride_u; |
| 341 | dst_v += dst_stride_v; |
| 342 | } |
| 343 | return 0; |
| 344 | } |
| 345 |
| 346 | // Convert UYVY to I422. |
| 347 | LIBYUV_API |
| 348 | int UYVYToI422(const uint8* src_uyvy, int src_stride_uyvy, |
| 349 | uint8* dst_y, int dst_stride_y, |
| 350 | uint8* dst_u, int dst_stride_u, |
| 351 | uint8* dst_v, int dst_stride_v, |
| 352 | int width, int height) { |
| 353 | int y; |
| 354 | void (*UYVYToUV422Row)(const uint8* src_uyvy, |
| 355 | uint8* dst_u, uint8* dst_v, int width) = |
| 356 | UYVYToUV422Row_C; |
| 357 | void (*UYVYToYRow)(const uint8* src_uyvy, |
| 358 | uint8* dst_y, int width) = UYVYToYRow_C; |
| 359 | // Negative height means invert the image. |
| 360 | if (height < 0) { |
| 361 | height = -height; |
| 362 | src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy; |
| 363 | src_stride_uyvy = -src_stride_uyvy; |
| 364 | } |
| 365 | // Coalesce rows. |
| 366 | if (src_stride_uyvy == width * 2 && |
| 367 | dst_stride_y == width && |
| 368 | dst_stride_u * 2 == width && |
| 369 | dst_stride_v * 2 == width) { |
| 370 | width *= height; |
| 371 | height = 1; |
| 372 | src_stride_uyvy = dst_stride_y = dst_stride_u = dst_stride_v = 0; |
| 373 | } |
| 374 | #if defined(HAS_UYVYTOYROW_SSE2) |
| 375 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 376 | UYVYToUV422Row = UYVYToUV422Row_Any_SSE2; |
| 377 | UYVYToYRow = UYVYToYRow_Any_SSE2; |
| 378 | if (IS_ALIGNED(width, 16)) { |
| 379 | UYVYToUV422Row = UYVYToUV422Row_SSE2; |
| 380 | UYVYToYRow = UYVYToYRow_SSE2; |
| 381 | } |
| 382 | } |
| 383 | #endif |
| 384 | #if defined(HAS_UYVYTOYROW_AVX2) |
| 385 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 386 | UYVYToUV422Row = UYVYToUV422Row_Any_AVX2; |
| 387 | UYVYToYRow = UYVYToYRow_Any_AVX2; |
| 388 | if (IS_ALIGNED(width, 32)) { |
| 389 | UYVYToUV422Row = UYVYToUV422Row_AVX2; |
| 390 | UYVYToYRow = UYVYToYRow_AVX2; |
| 391 | } |
| 392 | } |
| 393 | #endif |
| 394 | #if defined(HAS_UYVYTOYROW_NEON) |
| 395 | if (TestCpuFlag(kCpuHasNEON)) { |
| 396 | UYVYToYRow = UYVYToYRow_Any_NEON; |
| 397 | if (width >= 16) { |
| 398 | UYVYToUV422Row = UYVYToUV422Row_Any_NEON; |
| 399 | } |
| 400 | if (IS_ALIGNED(width, 16)) { |
| 401 | UYVYToYRow = UYVYToYRow_NEON; |
| 402 | UYVYToUV422Row = UYVYToUV422Row_NEON; |
| 403 | } |
| 404 | } |
| 405 | #endif |
| 406 |
| 407 | for (y = 0; y < height; ++y) { |
| 408 | UYVYToUV422Row(src_uyvy, dst_u, dst_v, width); |
| 409 | UYVYToYRow(src_uyvy, dst_y, width); |
| 410 | src_uyvy += src_stride_uyvy; |
| 411 | dst_y += dst_stride_y; |
| 412 | dst_u += dst_stride_u; |
| 413 | dst_v += dst_stride_v; |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 |
| 418 | // Mirror I400 with optional flipping |
| 419 | LIBYUV_API |
| 420 | int I400Mirror(const uint8* src_y, int src_stride_y, |
| 421 | uint8* dst_y, int dst_stride_y, |
| 422 | int width, int height) { |
| 423 | if (!src_y || !dst_y || |
| 424 | width <= 0 || height == 0) { |
| 425 | return -1; |
| 426 | } |
| 427 | // Negative height means invert the image. |
| 428 | if (height < 0) { |
| 429 | height = -height; |
| 430 | src_y = src_y + (height - 1) * src_stride_y; |
| 431 | src_stride_y = -src_stride_y; |
| 432 | } |
| 433 |
| 434 | MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 435 | return 0; |
| 436 | } |
| 437 |
| 438 | // Mirror I420 with optional flipping |
| 439 | LIBYUV_API |
| 440 | int I420Mirror(const uint8* src_y, int src_stride_y, |
| 441 | const uint8* src_u, int src_stride_u, |
| 442 | const uint8* src_v, int src_stride_v, |
| 443 | uint8* dst_y, int dst_stride_y, |
| 444 | uint8* dst_u, int dst_stride_u, |
| 445 | uint8* dst_v, int dst_stride_v, |
| 446 | int width, int height) { |
| 447 | int halfwidth = (width + 1) >> 1; |
| 448 | int halfheight = (height + 1) >> 1; |
| 449 | if (!src_y || !src_u || !src_v || !dst_y || !dst_u || !dst_v || |
| 450 | width <= 0 || height == 0) { |
| 451 | return -1; |
| 452 | } |
| 453 | // Negative height means invert the image. |
| 454 | if (height < 0) { |
| 455 | height = -height; |
| 456 | halfheight = (height + 1) >> 1; |
| 457 | src_y = src_y + (height - 1) * src_stride_y; |
| 458 | src_u = src_u + (halfheight - 1) * src_stride_u; |
| 459 | src_v = src_v + (halfheight - 1) * src_stride_v; |
| 460 | src_stride_y = -src_stride_y; |
| 461 | src_stride_u = -src_stride_u; |
| 462 | src_stride_v = -src_stride_v; |
| 463 | } |
| 464 |
| 465 | if (dst_y) { |
| 466 | MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 467 | } |
| 468 | MirrorPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, halfheight); |
| 469 | MirrorPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, halfheight); |
| 470 | return 0; |
| 471 | } |
| 472 |
| 473 | // ARGB mirror. |
| 474 | LIBYUV_API |
| 475 | int ARGBMirror(const uint8* src_argb, int src_stride_argb, |
| 476 | uint8* dst_argb, int dst_stride_argb, |
| 477 | int width, int height) { |
| 478 | int y; |
| 479 | void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) = |
| 480 | ARGBMirrorRow_C; |
| 481 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 482 | return -1; |
| 483 | } |
| 484 | // Negative height means invert the image. |
| 485 | if (height < 0) { |
| 486 | height = -height; |
| 487 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 488 | src_stride_argb = -src_stride_argb; |
| 489 | } |
| 490 | #if defined(HAS_ARGBMIRRORROW_NEON) |
| 491 | if (TestCpuFlag(kCpuHasNEON)) { |
| 492 | ARGBMirrorRow = ARGBMirrorRow_Any_NEON; |
| 493 | if (IS_ALIGNED(width, 4)) { |
| 494 | ARGBMirrorRow = ARGBMirrorRow_NEON; |
| 495 | } |
| 496 | } |
| 497 | #endif |
| 498 | #if defined(HAS_ARGBMIRRORROW_SSE2) |
| 499 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 500 | ARGBMirrorRow = ARGBMirrorRow_Any_SSE2; |
| 501 | if (IS_ALIGNED(width, 4)) { |
| 502 | ARGBMirrorRow = ARGBMirrorRow_SSE2; |
| 503 | } |
| 504 | } |
| 505 | #endif |
| 506 | #if defined(HAS_ARGBMIRRORROW_AVX2) |
| 507 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 508 | ARGBMirrorRow = ARGBMirrorRow_Any_AVX2; |
| 509 | if (IS_ALIGNED(width, 8)) { |
| 510 | ARGBMirrorRow = ARGBMirrorRow_AVX2; |
| 511 | } |
| 512 | } |
| 513 | #endif |
| 514 |
| 515 | // Mirror plane |
| 516 | for (y = 0; y < height; ++y) { |
| 517 | ARGBMirrorRow(src_argb, dst_argb, width); |
| 518 | src_argb += src_stride_argb; |
| 519 | dst_argb += dst_stride_argb; |
| 520 | } |
| 521 | return 0; |
| 522 | } |
| 523 |
| 524 | // Get a blender that optimized for the CPU and pixel count. |
| 525 | // As there are 6 blenders to choose from, the caller should try to use |
| 526 | // the same blend function for all pixels if possible. |
| 527 | LIBYUV_API |
| 528 | ARGBBlendRow GetARGBBlend() { |
| 529 | void (*ARGBBlendRow)(const uint8* src_argb, const uint8* src_argb1, |
| 530 | uint8* dst_argb, int width) = ARGBBlendRow_C; |
| 531 | #if defined(HAS_ARGBBLENDROW_SSSE3) |
| 532 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 533 | ARGBBlendRow = ARGBBlendRow_SSSE3; |
| 534 | return ARGBBlendRow; |
| 535 | } |
| 536 | #endif |
| 537 | #if defined(HAS_ARGBBLENDROW_NEON) |
| 538 | if (TestCpuFlag(kCpuHasNEON)) { |
| 539 | ARGBBlendRow = ARGBBlendRow_NEON; |
| 540 | } |
| 541 | #endif |
| 542 | return ARGBBlendRow; |
| 543 | } |
| 544 |
| 545 | // Alpha Blend 2 ARGB images and store to destination. |
| 546 | LIBYUV_API |
| 547 | int ARGBBlend(const uint8* src_argb0, int src_stride_argb0, |
| 548 | const uint8* src_argb1, int src_stride_argb1, |
| 549 | uint8* dst_argb, int dst_stride_argb, |
| 550 | int width, int height) { |
| 551 | int y; |
| 552 | void (*ARGBBlendRow)(const uint8* src_argb, const uint8* src_argb1, |
| 553 | uint8* dst_argb, int width) = GetARGBBlend(); |
| 554 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 555 | return -1; |
| 556 | } |
| 557 | // Negative height means invert the image. |
| 558 | if (height < 0) { |
| 559 | height = -height; |
| 560 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 561 | dst_stride_argb = -dst_stride_argb; |
| 562 | } |
| 563 | // Coalesce rows. |
| 564 | if (src_stride_argb0 == width * 4 && |
| 565 | src_stride_argb1 == width * 4 && |
| 566 | dst_stride_argb == width * 4) { |
| 567 | width *= height; |
| 568 | height = 1; |
| 569 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 570 | } |
| 571 |
| 572 | for (y = 0; y < height; ++y) { |
| 573 | ARGBBlendRow(src_argb0, src_argb1, dst_argb, width); |
| 574 | src_argb0 += src_stride_argb0; |
| 575 | src_argb1 += src_stride_argb1; |
| 576 | dst_argb += dst_stride_argb; |
| 577 | } |
| 578 | return 0; |
| 579 | } |
| 580 |
| 581 | // Alpha Blend plane and store to destination. |
| 582 | LIBYUV_API |
| 583 | int BlendPlane(const uint8* src_y0, int src_stride_y0, |
| 584 | const uint8* src_y1, int src_stride_y1, |
| 585 | const uint8* alpha, int alpha_stride, |
| 586 | uint8* dst_y, int dst_stride_y, |
| 587 | int width, int height) { |
| 588 | int y; |
| 589 | void (*BlendPlaneRow)(const uint8* src0, const uint8* src1, |
| 590 | const uint8* alpha, uint8* dst, int width) = BlendPlaneRow_C; |
| 591 | if (!src_y0 || !src_y1 || !alpha || !dst_y || width <= 0 || height == 0) { |
| 592 | return -1; |
| 593 | } |
| 594 | // Negative height means invert the image. |
| 595 | if (height < 0) { |
| 596 | height = -height; |
| 597 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 598 | dst_stride_y = -dst_stride_y; |
| 599 | } |
| 600 |
| 601 | // Coalesce rows for Y plane. |
| 602 | if (src_stride_y0 == width && |
| 603 | src_stride_y1 == width && |
| 604 | alpha_stride == width && |
| 605 | dst_stride_y == width) { |
| 606 | width *= height; |
| 607 | height = 1; |
| 608 | src_stride_y0 = src_stride_y1 = alpha_stride = dst_stride_y = 0; |
| 609 | } |
| 610 |
| 611 | #if defined(HAS_BLENDPLANEROW_SSSE3) |
| 612 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 613 | BlendPlaneRow = BlendPlaneRow_Any_SSSE3; |
| 614 | if (IS_ALIGNED(width, 8)) { |
| 615 | BlendPlaneRow = BlendPlaneRow_SSSE3; |
| 616 | } |
| 617 | } |
| 618 | #endif |
| 619 | #if defined(HAS_BLENDPLANEROW_AVX2) |
| 620 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 621 | BlendPlaneRow = BlendPlaneRow_Any_AVX2; |
| 622 | if (IS_ALIGNED(width, 32)) { |
| 623 | BlendPlaneRow = BlendPlaneRow_AVX2; |
| 624 | } |
| 625 | } |
| 626 | #endif |
| 627 |
| 628 | for (y = 0; y < height; ++y) { |
| 629 | BlendPlaneRow(src_y0, src_y1, alpha, dst_y, width); |
| 630 | src_y0 += src_stride_y0; |
| 631 | src_y1 += src_stride_y1; |
| 632 | alpha += alpha_stride; |
| 633 | dst_y += dst_stride_y; |
| 634 | } |
| 635 | return 0; |
| 636 | } |
| 637 |
| 638 | #define MAXTWIDTH 2048 |
| 639 | // Alpha Blend YUV images and store to destination. |
| 640 | LIBYUV_API |
| 641 | int I420Blend(const uint8* src_y0, int src_stride_y0, |
| 642 | const uint8* src_u0, int src_stride_u0, |
| 643 | const uint8* src_v0, int src_stride_v0, |
| 644 | const uint8* src_y1, int src_stride_y1, |
| 645 | const uint8* src_u1, int src_stride_u1, |
| 646 | const uint8* src_v1, int src_stride_v1, |
| 647 | const uint8* alpha, int alpha_stride, |
| 648 | uint8* dst_y, int dst_stride_y, |
| 649 | uint8* dst_u, int dst_stride_u, |
| 650 | uint8* dst_v, int dst_stride_v, |
| 651 | int width, int height) { |
| 652 | int y; |
| 653 | // Half width/height for UV. |
| 654 | int halfwidth = (width + 1) >> 1; |
| 655 | void (*BlendPlaneRow)(const uint8* src0, const uint8* src1, |
| 656 | const uint8* alpha, uint8* dst, int width) = BlendPlaneRow_C; |
| 657 | void (*ScaleRowDown2)(const uint8* src_ptr, ptrdiff_t src_stride, |
| 658 | uint8* dst_ptr, int dst_width) = ScaleRowDown2Box_C; |
| 659 | if (!src_y0 || !src_u0 || !src_v0 || !src_y1 || !src_u1 || !src_v1 || |
| 660 | !alpha || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) { |
| 661 | return -1; |
| 662 | } |
| 663 |
| 664 | // Negative height means invert the image. |
| 665 | if (height < 0) { |
| 666 | height = -height; |
| 667 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 668 | dst_stride_y = -dst_stride_y; |
| 669 | } |
| 670 |
| 671 | // Blend Y plane. |
| 672 | BlendPlane(src_y0, src_stride_y0, |
| 673 | src_y1, src_stride_y1, |
| 674 | alpha, alpha_stride, |
| 675 | dst_y, dst_stride_y, |
| 676 | width, height); |
| 677 |
| 678 | #if defined(HAS_BLENDPLANEROW_SSSE3) |
| 679 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 680 | BlendPlaneRow = BlendPlaneRow_Any_SSSE3; |
| 681 | if (IS_ALIGNED(halfwidth, 8)) { |
| 682 | BlendPlaneRow = BlendPlaneRow_SSSE3; |
| 683 | } |
| 684 | } |
| 685 | #endif |
| 686 | #if defined(HAS_BLENDPLANEROW_AVX2) |
| 687 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 688 | BlendPlaneRow = BlendPlaneRow_Any_AVX2; |
| 689 | if (IS_ALIGNED(halfwidth, 32)) { |
| 690 | BlendPlaneRow = BlendPlaneRow_AVX2; |
| 691 | } |
| 692 | } |
| 693 | #endif |
| 694 | #if defined(HAS_SCALEROWDOWN2_NEON) |
| 695 | if (TestCpuFlag(kCpuHasNEON)) { |
| 696 | ScaleRowDown2 = ScaleRowDown2Box_Any_NEON; |
| 697 | if (IS_ALIGNED(halfwidth, 16)) { |
| 698 | ScaleRowDown2 = ScaleRowDown2Box_NEON; |
| 699 | } |
| 700 | } |
| 701 | #endif |
| 702 | #if defined(HAS_SCALEROWDOWN2_SSSE3) |
| 703 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 704 | ScaleRowDown2 = ScaleRowDown2Box_Any_SSSE3; |
| 705 | if (IS_ALIGNED(halfwidth, 16)) { |
| 706 | ScaleRowDown2 = ScaleRowDown2Box_SSSE3; |
| 707 | } |
| 708 | } |
| 709 | #endif |
| 710 | #if defined(HAS_SCALEROWDOWN2_AVX2) |
| 711 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 712 | ScaleRowDown2 = ScaleRowDown2Box_Any_AVX2; |
| 713 | if (IS_ALIGNED(halfwidth, 32)) { |
| 714 | ScaleRowDown2 = ScaleRowDown2Box_AVX2; |
| 715 | } |
| 716 | } |
| 717 | #endif |
| 718 |
| 719 | // Row buffer for intermediate alpha pixels. |
| 720 | align_buffer_64(halfalpha, halfwidth); |
| 721 | for (y = 0; y < height; y += 2) { |
| 722 | // last row of odd height image use 1 row of alpha instead of 2. |
| 723 | if (y == (height - 1)) { |
| 724 | alpha_stride = 0; |
| 725 | } |
| 726 | // Subsample 2 rows of UV to half width and half height. |
| 727 | ScaleRowDown2(alpha, alpha_stride, halfalpha, halfwidth); |
| 728 | alpha += alpha_stride * 2; |
| 729 | BlendPlaneRow(src_u0, src_u1, halfalpha, dst_u, halfwidth); |
| 730 | BlendPlaneRow(src_v0, src_v1, halfalpha, dst_v, halfwidth); |
| 731 | src_u0 += src_stride_u0; |
| 732 | src_u1 += src_stride_u1; |
| 733 | dst_u += dst_stride_u; |
| 734 | src_v0 += src_stride_v0; |
| 735 | src_v1 += src_stride_v1; |
| 736 | dst_v += dst_stride_v; |
| 737 | } |
| 738 | free_aligned_buffer_64(halfalpha); |
| 739 | return 0; |
| 740 | } |
| 741 |
| 742 | // Multiply 2 ARGB images and store to destination. |
| 743 | LIBYUV_API |
| 744 | int ARGBMultiply(const uint8* src_argb0, int src_stride_argb0, |
| 745 | const uint8* src_argb1, int src_stride_argb1, |
| 746 | uint8* dst_argb, int dst_stride_argb, |
| 747 | int width, int height) { |
| 748 | int y; |
| 749 | void (*ARGBMultiplyRow)(const uint8* src0, const uint8* src1, uint8* dst, |
| 750 | int width) = ARGBMultiplyRow_C; |
| 751 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 752 | return -1; |
| 753 | } |
| 754 | // Negative height means invert the image. |
| 755 | if (height < 0) { |
| 756 | height = -height; |
| 757 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 758 | dst_stride_argb = -dst_stride_argb; |
| 759 | } |
| 760 | // Coalesce rows. |
| 761 | if (src_stride_argb0 == width * 4 && |
| 762 | src_stride_argb1 == width * 4 && |
| 763 | dst_stride_argb == width * 4) { |
| 764 | width *= height; |
| 765 | height = 1; |
| 766 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 767 | } |
| 768 | #if defined(HAS_ARGBMULTIPLYROW_SSE2) |
| 769 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 770 | ARGBMultiplyRow = ARGBMultiplyRow_Any_SSE2; |
| 771 | if (IS_ALIGNED(width, 4)) { |
| 772 | ARGBMultiplyRow = ARGBMultiplyRow_SSE2; |
| 773 | } |
| 774 | } |
| 775 | #endif |
| 776 | #if defined(HAS_ARGBMULTIPLYROW_AVX2) |
| 777 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 778 | ARGBMultiplyRow = ARGBMultiplyRow_Any_AVX2; |
| 779 | if (IS_ALIGNED(width, 8)) { |
| 780 | ARGBMultiplyRow = ARGBMultiplyRow_AVX2; |
| 781 | } |
| 782 | } |
| 783 | #endif |
| 784 | #if defined(HAS_ARGBMULTIPLYROW_NEON) |
| 785 | if (TestCpuFlag(kCpuHasNEON)) { |
| 786 | ARGBMultiplyRow = ARGBMultiplyRow_Any_NEON; |
| 787 | if (IS_ALIGNED(width, 8)) { |
| 788 | ARGBMultiplyRow = ARGBMultiplyRow_NEON; |
| 789 | } |
| 790 | } |
| 791 | #endif |
| 792 |
| 793 | // Multiply plane |
| 794 | for (y = 0; y < height; ++y) { |
| 795 | ARGBMultiplyRow(src_argb0, src_argb1, dst_argb, width); |
| 796 | src_argb0 += src_stride_argb0; |
| 797 | src_argb1 += src_stride_argb1; |
| 798 | dst_argb += dst_stride_argb; |
| 799 | } |
| 800 | return 0; |
| 801 | } |
| 802 |
| 803 | // Add 2 ARGB images and store to destination. |
| 804 | LIBYUV_API |
| 805 | int ARGBAdd(const uint8* src_argb0, int src_stride_argb0, |
| 806 | const uint8* src_argb1, int src_stride_argb1, |
| 807 | uint8* dst_argb, int dst_stride_argb, |
| 808 | int width, int height) { |
| 809 | int y; |
| 810 | void (*ARGBAddRow)(const uint8* src0, const uint8* src1, uint8* dst, |
| 811 | int width) = ARGBAddRow_C; |
| 812 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 813 | return -1; |
| 814 | } |
| 815 | // Negative height means invert the image. |
| 816 | if (height < 0) { |
| 817 | height = -height; |
| 818 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 819 | dst_stride_argb = -dst_stride_argb; |
| 820 | } |
| 821 | // Coalesce rows. |
| 822 | if (src_stride_argb0 == width * 4 && |
| 823 | src_stride_argb1 == width * 4 && |
| 824 | dst_stride_argb == width * 4) { |
| 825 | width *= height; |
| 826 | height = 1; |
| 827 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 828 | } |
| 829 | #if defined(HAS_ARGBADDROW_SSE2) && (defined(_MSC_VER) && !defined(__clang__)) |
| 830 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 831 | ARGBAddRow = ARGBAddRow_SSE2; |
| 832 | } |
| 833 | #endif |
| 834 | #if defined(HAS_ARGBADDROW_SSE2) && !(defined(_MSC_VER) && !defined(__clang__)) |
| 835 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 836 | ARGBAddRow = ARGBAddRow_Any_SSE2; |
| 837 | if (IS_ALIGNED(width, 4)) { |
| 838 | ARGBAddRow = ARGBAddRow_SSE2; |
| 839 | } |
| 840 | } |
| 841 | #endif |
| 842 | #if defined(HAS_ARGBADDROW_AVX2) |
| 843 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 844 | ARGBAddRow = ARGBAddRow_Any_AVX2; |
| 845 | if (IS_ALIGNED(width, 8)) { |
| 846 | ARGBAddRow = ARGBAddRow_AVX2; |
| 847 | } |
| 848 | } |
| 849 | #endif |
| 850 | #if defined(HAS_ARGBADDROW_NEON) |
| 851 | if (TestCpuFlag(kCpuHasNEON)) { |
| 852 | ARGBAddRow = ARGBAddRow_Any_NEON; |
| 853 | if (IS_ALIGNED(width, 8)) { |
| 854 | ARGBAddRow = ARGBAddRow_NEON; |
| 855 | } |
| 856 | } |
| 857 | #endif |
| 858 |
| 859 | // Add plane |
| 860 | for (y = 0; y < height; ++y) { |
| 861 | ARGBAddRow(src_argb0, src_argb1, dst_argb, width); |
| 862 | src_argb0 += src_stride_argb0; |
| 863 | src_argb1 += src_stride_argb1; |
| 864 | dst_argb += dst_stride_argb; |
| 865 | } |
| 866 | return 0; |
| 867 | } |
| 868 |
| 869 | // Subtract 2 ARGB images and store to destination. |
| 870 | LIBYUV_API |
| 871 | int ARGBSubtract(const uint8* src_argb0, int src_stride_argb0, |
| 872 | const uint8* src_argb1, int src_stride_argb1, |
| 873 | uint8* dst_argb, int dst_stride_argb, |
| 874 | int width, int height) { |
| 875 | int y; |
| 876 | void (*ARGBSubtractRow)(const uint8* src0, const uint8* src1, uint8* dst, |
| 877 | int width) = ARGBSubtractRow_C; |
| 878 | if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) { |
| 879 | return -1; |
| 880 | } |
| 881 | // Negative height means invert the image. |
| 882 | if (height < 0) { |
| 883 | height = -height; |
| 884 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 885 | dst_stride_argb = -dst_stride_argb; |
| 886 | } |
| 887 | // Coalesce rows. |
| 888 | if (src_stride_argb0 == width * 4 && |
| 889 | src_stride_argb1 == width * 4 && |
| 890 | dst_stride_argb == width * 4) { |
| 891 | width *= height; |
| 892 | height = 1; |
| 893 | src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0; |
| 894 | } |
| 895 | #if defined(HAS_ARGBSUBTRACTROW_SSE2) |
| 896 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 897 | ARGBSubtractRow = ARGBSubtractRow_Any_SSE2; |
| 898 | if (IS_ALIGNED(width, 4)) { |
| 899 | ARGBSubtractRow = ARGBSubtractRow_SSE2; |
| 900 | } |
| 901 | } |
| 902 | #endif |
| 903 | #if defined(HAS_ARGBSUBTRACTROW_AVX2) |
| 904 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 905 | ARGBSubtractRow = ARGBSubtractRow_Any_AVX2; |
| 906 | if (IS_ALIGNED(width, 8)) { |
| 907 | ARGBSubtractRow = ARGBSubtractRow_AVX2; |
| 908 | } |
| 909 | } |
| 910 | #endif |
| 911 | #if defined(HAS_ARGBSUBTRACTROW_NEON) |
| 912 | if (TestCpuFlag(kCpuHasNEON)) { |
| 913 | ARGBSubtractRow = ARGBSubtractRow_Any_NEON; |
| 914 | if (IS_ALIGNED(width, 8)) { |
| 915 | ARGBSubtractRow = ARGBSubtractRow_NEON; |
| 916 | } |
| 917 | } |
| 918 | #endif |
| 919 |
| 920 | // Subtract plane |
| 921 | for (y = 0; y < height; ++y) { |
| 922 | ARGBSubtractRow(src_argb0, src_argb1, dst_argb, width); |
| 923 | src_argb0 += src_stride_argb0; |
| 924 | src_argb1 += src_stride_argb1; |
| 925 | dst_argb += dst_stride_argb; |
| 926 | } |
| 927 | return 0; |
| 928 | } |
| 929 | // Convert I422 to RGBA with matrix |
| 930 | static int I422ToRGBAMatrix(const uint8* src_y, int src_stride_y, |
| 931 | const uint8* src_u, int src_stride_u, |
| 932 | const uint8* src_v, int src_stride_v, |
| 933 | uint8* dst_rgba, int dst_stride_rgba, |
| 934 | const struct YuvConstants* yuvconstants, |
| 935 | int width, int height) { |
| 936 | int y; |
| 937 | void (*I422ToRGBARow)(const uint8* y_buf, |
| 938 | const uint8* u_buf, |
| 939 | const uint8* v_buf, |
| 940 | uint8* rgb_buf, |
| 941 | const struct YuvConstants* yuvconstants, |
| 942 | int width) = I422ToRGBARow_C; |
| 943 | if (!src_y || !src_u || !src_v || !dst_rgba || |
| 944 | width <= 0 || height == 0) { |
| 945 | return -1; |
| 946 | } |
| 947 | // Negative height means invert the image. |
| 948 | if (height < 0) { |
| 949 | height = -height; |
| 950 | dst_rgba = dst_rgba + (height - 1) * dst_stride_rgba; |
| 951 | dst_stride_rgba = -dst_stride_rgba; |
| 952 | } |
| 953 | #if defined(HAS_I422TORGBAROW_SSSE3) |
| 954 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 955 | I422ToRGBARow = I422ToRGBARow_Any_SSSE3; |
| 956 | if (IS_ALIGNED(width, 8)) { |
| 957 | I422ToRGBARow = I422ToRGBARow_SSSE3; |
| 958 | } |
| 959 | } |
| 960 | #endif |
| 961 | #if defined(HAS_I422TORGBAROW_AVX2) |
| 962 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 963 | I422ToRGBARow = I422ToRGBARow_Any_AVX2; |
| 964 | if (IS_ALIGNED(width, 16)) { |
| 965 | I422ToRGBARow = I422ToRGBARow_AVX2; |
| 966 | } |
| 967 | } |
| 968 | #endif |
| 969 | #if defined(HAS_I422TORGBAROW_NEON) |
| 970 | if (TestCpuFlag(kCpuHasNEON)) { |
| 971 | I422ToRGBARow = I422ToRGBARow_Any_NEON; |
| 972 | if (IS_ALIGNED(width, 8)) { |
| 973 | I422ToRGBARow = I422ToRGBARow_NEON; |
| 974 | } |
| 975 | } |
| 976 | #endif |
| 977 | #if defined(HAS_I422TORGBAROW_MIPS_DSPR2) |
| 978 | if (TestCpuFlag(kCpuHasMIPS_DSPR2) && IS_ALIGNED(width, 4) && |
| 979 | IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) && |
| 980 | IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) && |
| 981 | IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2) && |
| 982 | IS_ALIGNED(dst_rgba, 4) && IS_ALIGNED(dst_stride_rgba, 4)) { |
| 983 | I422ToRGBARow = I422ToRGBARow_MIPS_DSPR2; |
| 984 | } |
| 985 | #endif |
| 986 |
| 987 | for (y = 0; y < height; ++y) { |
| 988 | I422ToRGBARow(src_y, src_u, src_v, dst_rgba, yuvconstants, width); |
| 989 | dst_rgba += dst_stride_rgba; |
| 990 | src_y += src_stride_y; |
| 991 | src_u += src_stride_u; |
| 992 | src_v += src_stride_v; |
| 993 | } |
| 994 | return 0; |
| 995 | } |
| 996 |
| 997 | // Convert I422 to RGBA. |
| 998 | LIBYUV_API |
| 999 | int I422ToRGBA(const uint8* src_y, int src_stride_y, |
| 1000 | const uint8* src_u, int src_stride_u, |
| 1001 | const uint8* src_v, int src_stride_v, |
| 1002 | uint8* dst_rgba, int dst_stride_rgba, |
| 1003 | int width, int height) { |
| 1004 | return I422ToRGBAMatrix(src_y, src_stride_y, |
| 1005 | src_u, src_stride_u, |
| 1006 | src_v, src_stride_v, |
| 1007 | dst_rgba, dst_stride_rgba, |
| 1008 | &kYuvI601Constants, |
| 1009 | width, height); |
| 1010 | } |
| 1011 |
| 1012 | // Convert I422 to BGRA. |
| 1013 | LIBYUV_API |
| 1014 | int I422ToBGRA(const uint8* src_y, int src_stride_y, |
| 1015 | const uint8* src_u, int src_stride_u, |
| 1016 | const uint8* src_v, int src_stride_v, |
| 1017 | uint8* dst_bgra, int dst_stride_bgra, |
| 1018 | int width, int height) { |
| 1019 | return I422ToRGBAMatrix(src_y, src_stride_y, |
| 1020 | src_v, src_stride_v, // Swap U and V |
| 1021 | src_u, src_stride_u, |
| 1022 | dst_bgra, dst_stride_bgra, |
| 1023 | &kYvuI601Constants, // Use Yvu matrix |
| 1024 | width, height); |
| 1025 | } |
| 1026 |
| 1027 | // Convert NV12 to RGB565. |
| 1028 | LIBYUV_API |
| 1029 | int NV12ToRGB565(const uint8* src_y, int src_stride_y, |
| 1030 | const uint8* src_uv, int src_stride_uv, |
| 1031 | uint8* dst_rgb565, int dst_stride_rgb565, |
| 1032 | int width, int height) { |
| 1033 | int y; |
| 1034 | void (*NV12ToRGB565Row)(const uint8* y_buf, |
| 1035 | const uint8* uv_buf, |
| 1036 | uint8* rgb_buf, |
| 1037 | const struct YuvConstants* yuvconstants, |
| 1038 | int width) = NV12ToRGB565Row_C; |
| 1039 | if (!src_y || !src_uv || !dst_rgb565 || |
| 1040 | width <= 0 || height == 0) { |
| 1041 | return -1; |
| 1042 | } |
| 1043 | // Negative height means invert the image. |
| 1044 | if (height < 0) { |
| 1045 | height = -height; |
| 1046 | dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565; |
| 1047 | dst_stride_rgb565 = -dst_stride_rgb565; |
| 1048 | } |
| 1049 | #if defined(HAS_NV12TORGB565ROW_SSSE3) |
| 1050 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1051 | NV12ToRGB565Row = NV12ToRGB565Row_Any_SSSE3; |
| 1052 | if (IS_ALIGNED(width, 8)) { |
| 1053 | NV12ToRGB565Row = NV12ToRGB565Row_SSSE3; |
| 1054 | } |
| 1055 | } |
| 1056 | #endif |
| 1057 | #if defined(HAS_NV12TORGB565ROW_AVX2) |
| 1058 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1059 | NV12ToRGB565Row = NV12ToRGB565Row_Any_AVX2; |
| 1060 | if (IS_ALIGNED(width, 16)) { |
| 1061 | NV12ToRGB565Row = NV12ToRGB565Row_AVX2; |
| 1062 | } |
| 1063 | } |
| 1064 | #endif |
| 1065 | #if defined(HAS_NV12TORGB565ROW_NEON) |
| 1066 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1067 | NV12ToRGB565Row = NV12ToRGB565Row_Any_NEON; |
| 1068 | if (IS_ALIGNED(width, 8)) { |
| 1069 | NV12ToRGB565Row = NV12ToRGB565Row_NEON; |
| 1070 | } |
| 1071 | } |
| 1072 | #endif |
| 1073 |
| 1074 | for (y = 0; y < height; ++y) { |
| 1075 | NV12ToRGB565Row(src_y, src_uv, dst_rgb565, &kYuvI601Constants, width); |
| 1076 | dst_rgb565 += dst_stride_rgb565; |
| 1077 | src_y += src_stride_y; |
| 1078 | if (y & 1) { |
| 1079 | src_uv += src_stride_uv; |
| 1080 | } |
| 1081 | } |
| 1082 | return 0; |
| 1083 | } |
| 1084 |
| 1085 | // Convert RAW to RGB24. |
| 1086 | LIBYUV_API |
| 1087 | int RAWToRGB24(const uint8* src_raw, int src_stride_raw, |
| 1088 | uint8* dst_rgb24, int dst_stride_rgb24, |
| 1089 | int width, int height) { |
| 1090 | int y; |
| 1091 | void (*RAWToRGB24Row)(const uint8* src_rgb, uint8* dst_rgb24, int width) = |
| 1092 | RAWToRGB24Row_C; |
| 1093 | if (!src_raw || !dst_rgb24 || |
| 1094 | width <= 0 || height == 0) { |
| 1095 | return -1; |
| 1096 | } |
| 1097 | // Negative height means invert the image. |
| 1098 | if (height < 0) { |
| 1099 | height = -height; |
| 1100 | src_raw = src_raw + (height - 1) * src_stride_raw; |
| 1101 | src_stride_raw = -src_stride_raw; |
| 1102 | } |
| 1103 | // Coalesce rows. |
| 1104 | if (src_stride_raw == width * 3 && |
| 1105 | dst_stride_rgb24 == width * 3) { |
| 1106 | width *= height; |
| 1107 | height = 1; |
| 1108 | src_stride_raw = dst_stride_rgb24 = 0; |
| 1109 | } |
| 1110 | #if defined(HAS_RAWTORGB24ROW_SSSE3) |
| 1111 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1112 | RAWToRGB24Row = RAWToRGB24Row_Any_SSSE3; |
| 1113 | if (IS_ALIGNED(width, 8)) { |
| 1114 | RAWToRGB24Row = RAWToRGB24Row_SSSE3; |
| 1115 | } |
| 1116 | } |
| 1117 | #endif |
| 1118 | #if defined(HAS_RAWTORGB24ROW_NEON) |
| 1119 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1120 | RAWToRGB24Row = RAWToRGB24Row_Any_NEON; |
| 1121 | if (IS_ALIGNED(width, 8)) { |
| 1122 | RAWToRGB24Row = RAWToRGB24Row_NEON; |
| 1123 | } |
| 1124 | } |
| 1125 | #endif |
| 1126 |
| 1127 | for (y = 0; y < height; ++y) { |
| 1128 | RAWToRGB24Row(src_raw, dst_rgb24, width); |
| 1129 | src_raw += src_stride_raw; |
| 1130 | dst_rgb24 += dst_stride_rgb24; |
| 1131 | } |
| 1132 | return 0; |
| 1133 | } |
| 1134 |
| 1135 | LIBYUV_API |
| 1136 | void SetPlane(uint8* dst_y, int dst_stride_y, |
| 1137 | int width, int height, |
| 1138 | uint32 value) { |
| 1139 | int y; |
| 1140 | void (*SetRow)(uint8* dst, uint8 value, int width) = SetRow_C; |
| 1141 | if (height < 0) { |
| 1142 | height = -height; |
| 1143 | dst_y = dst_y + (height - 1) * dst_stride_y; |
| 1144 | dst_stride_y = -dst_stride_y; |
| 1145 | } |
| 1146 | // Coalesce rows. |
| 1147 | if (dst_stride_y == width) { |
| 1148 | width *= height; |
| 1149 | height = 1; |
| 1150 | dst_stride_y = 0; |
| 1151 | } |
| 1152 | #if defined(HAS_SETROW_NEON) |
| 1153 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1154 | SetRow = SetRow_Any_NEON; |
| 1155 | if (IS_ALIGNED(width, 16)) { |
| 1156 | SetRow = SetRow_NEON; |
| 1157 | } |
| 1158 | } |
| 1159 | #endif |
| 1160 | #if defined(HAS_SETROW_X86) |
| 1161 | if (TestCpuFlag(kCpuHasX86)) { |
| 1162 | SetRow = SetRow_Any_X86; |
| 1163 | if (IS_ALIGNED(width, 4)) { |
| 1164 | SetRow = SetRow_X86; |
| 1165 | } |
| 1166 | } |
| 1167 | #endif |
| 1168 | #if defined(HAS_SETROW_ERMS) |
| 1169 | if (TestCpuFlag(kCpuHasERMS)) { |
| 1170 | SetRow = SetRow_ERMS; |
| 1171 | } |
| 1172 | #endif |
| 1173 |
| 1174 | // Set plane |
| 1175 | for (y = 0; y < height; ++y) { |
| 1176 | SetRow(dst_y, value, width); |
| 1177 | dst_y += dst_stride_y; |
| 1178 | } |
| 1179 | } |
| 1180 |
| 1181 | // Draw a rectangle into I420 |
| 1182 | LIBYUV_API |
| 1183 | int I420Rect(uint8* dst_y, int dst_stride_y, |
| 1184 | uint8* dst_u, int dst_stride_u, |
| 1185 | uint8* dst_v, int dst_stride_v, |
| 1186 | int x, int y, |
| 1187 | int width, int height, |
| 1188 | int value_y, int value_u, int value_v) { |
| 1189 | int halfwidth = (width + 1) >> 1; |
| 1190 | int halfheight = (height + 1) >> 1; |
| 1191 | uint8* start_y = dst_y + y * dst_stride_y + x; |
| 1192 | uint8* start_u = dst_u + (y / 2) * dst_stride_u + (x / 2); |
| 1193 | uint8* start_v = dst_v + (y / 2) * dst_stride_v + (x / 2); |
| 1194 | if (!dst_y || !dst_u || !dst_v || |
| 1195 | width <= 0 || height == 0 || |
| 1196 | x < 0 || y < 0 || |
| 1197 | value_y < 0 || value_y > 255 || |
| 1198 | value_u < 0 || value_u > 255 || |
| 1199 | value_v < 0 || value_v > 255) { |
| 1200 | return -1; |
| 1201 | } |
| 1202 |
| 1203 | SetPlane(start_y, dst_stride_y, width, height, value_y); |
| 1204 | SetPlane(start_u, dst_stride_u, halfwidth, halfheight, value_u); |
| 1205 | SetPlane(start_v, dst_stride_v, halfwidth, halfheight, value_v); |
| 1206 | return 0; |
| 1207 | } |
| 1208 |
| 1209 | // Draw a rectangle into ARGB |
| 1210 | LIBYUV_API |
| 1211 | int ARGBRect(uint8* dst_argb, int dst_stride_argb, |
| 1212 | int dst_x, int dst_y, |
| 1213 | int width, int height, |
| 1214 | uint32 value) { |
| 1215 | int y; |
| 1216 | void (*ARGBSetRow)(uint8* dst_argb, uint32 value, int width) = ARGBSetRow_C; |
| 1217 | if (!dst_argb || |
| 1218 | width <= 0 || height == 0 || |
| 1219 | dst_x < 0 || dst_y < 0) { |
| 1220 | return -1; |
| 1221 | } |
| 1222 | if (height < 0) { |
| 1223 | height = -height; |
| 1224 | dst_argb = dst_argb + (height - 1) * dst_stride_argb; |
| 1225 | dst_stride_argb = -dst_stride_argb; |
| 1226 | } |
| 1227 | dst_argb += dst_y * dst_stride_argb + dst_x * 4; |
| 1228 | // Coalesce rows. |
| 1229 | if (dst_stride_argb == width * 4) { |
| 1230 | width *= height; |
| 1231 | height = 1; |
| 1232 | dst_stride_argb = 0; |
| 1233 | } |
| 1234 |
| 1235 | #if defined(HAS_ARGBSETROW_NEON) |
| 1236 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1237 | ARGBSetRow = ARGBSetRow_Any_NEON; |
| 1238 | if (IS_ALIGNED(width, 4)) { |
| 1239 | ARGBSetRow = ARGBSetRow_NEON; |
| 1240 | } |
| 1241 | } |
| 1242 | #endif |
| 1243 | #if defined(HAS_ARGBSETROW_X86) |
| 1244 | if (TestCpuFlag(kCpuHasX86)) { |
| 1245 | ARGBSetRow = ARGBSetRow_X86; |
| 1246 | } |
| 1247 | #endif |
| 1248 |
| 1249 | // Set plane |
| 1250 | for (y = 0; y < height; ++y) { |
| 1251 | ARGBSetRow(dst_argb, value, width); |
| 1252 | dst_argb += dst_stride_argb; |
| 1253 | } |
| 1254 | return 0; |
| 1255 | } |
| 1256 |
| 1257 | // Convert unattentuated ARGB to preattenuated ARGB. |
| 1258 | // An unattenutated ARGB alpha blend uses the formula |
| 1259 | // p = a * f + (1 - a) * b |
| 1260 | // where |
| 1261 | // p is output pixel |
| 1262 | // f is foreground pixel |
| 1263 | // b is background pixel |
| 1264 | // a is alpha value from foreground pixel |
| 1265 | // An preattenutated ARGB alpha blend uses the formula |
| 1266 | // p = f + (1 - a) * b |
| 1267 | // where |
| 1268 | // f is foreground pixel premultiplied by alpha |
| 1269 |
| 1270 | LIBYUV_API |
| 1271 | int ARGBAttenuate(const uint8* src_argb, int src_stride_argb, |
| 1272 | uint8* dst_argb, int dst_stride_argb, |
| 1273 | int width, int height) { |
| 1274 | int y; |
| 1275 | void (*ARGBAttenuateRow)(const uint8* src_argb, uint8* dst_argb, |
| 1276 | int width) = ARGBAttenuateRow_C; |
| 1277 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 1278 | return -1; |
| 1279 | } |
| 1280 | if (height < 0) { |
| 1281 | height = -height; |
| 1282 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 1283 | src_stride_argb = -src_stride_argb; |
| 1284 | } |
| 1285 | // Coalesce rows. |
| 1286 | if (src_stride_argb == width * 4 && |
| 1287 | dst_stride_argb == width * 4) { |
| 1288 | width *= height; |
| 1289 | height = 1; |
| 1290 | src_stride_argb = dst_stride_argb = 0; |
| 1291 | } |
| 1292 | #if defined(HAS_ARGBATTENUATEROW_SSSE3) |
| 1293 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1294 | ARGBAttenuateRow = ARGBAttenuateRow_Any_SSSE3; |
| 1295 | if (IS_ALIGNED(width, 4)) { |
| 1296 | ARGBAttenuateRow = ARGBAttenuateRow_SSSE3; |
| 1297 | } |
| 1298 | } |
| 1299 | #endif |
| 1300 | #if defined(HAS_ARGBATTENUATEROW_AVX2) |
| 1301 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1302 | ARGBAttenuateRow = ARGBAttenuateRow_Any_AVX2; |
| 1303 | if (IS_ALIGNED(width, 8)) { |
| 1304 | ARGBAttenuateRow = ARGBAttenuateRow_AVX2; |
| 1305 | } |
| 1306 | } |
| 1307 | #endif |
| 1308 | #if defined(HAS_ARGBATTENUATEROW_NEON) |
| 1309 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1310 | ARGBAttenuateRow = ARGBAttenuateRow_Any_NEON; |
| 1311 | if (IS_ALIGNED(width, 8)) { |
| 1312 | ARGBAttenuateRow = ARGBAttenuateRow_NEON; |
| 1313 | } |
| 1314 | } |
| 1315 | #endif |
| 1316 |
| 1317 | for (y = 0; y < height; ++y) { |
| 1318 | ARGBAttenuateRow(src_argb, dst_argb, width); |
| 1319 | src_argb += src_stride_argb; |
| 1320 | dst_argb += dst_stride_argb; |
| 1321 | } |
| 1322 | return 0; |
| 1323 | } |
| 1324 |
| 1325 | // Convert preattentuated ARGB to unattenuated ARGB. |
| 1326 | LIBYUV_API |
| 1327 | int ARGBUnattenuate(const uint8* src_argb, int src_stride_argb, |
| 1328 | uint8* dst_argb, int dst_stride_argb, |
| 1329 | int width, int height) { |
| 1330 | int y; |
| 1331 | void (*ARGBUnattenuateRow)(const uint8* src_argb, uint8* dst_argb, |
| 1332 | int width) = ARGBUnattenuateRow_C; |
| 1333 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 1334 | return -1; |
| 1335 | } |
| 1336 | if (height < 0) { |
| 1337 | height = -height; |
| 1338 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 1339 | src_stride_argb = -src_stride_argb; |
| 1340 | } |
| 1341 | // Coalesce rows. |
| 1342 | if (src_stride_argb == width * 4 && |
| 1343 | dst_stride_argb == width * 4) { |
| 1344 | width *= height; |
| 1345 | height = 1; |
| 1346 | src_stride_argb = dst_stride_argb = 0; |
| 1347 | } |
| 1348 | #if defined(HAS_ARGBUNATTENUATEROW_SSE2) |
| 1349 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1350 | ARGBUnattenuateRow = ARGBUnattenuateRow_Any_SSE2; |
| 1351 | if (IS_ALIGNED(width, 4)) { |
| 1352 | ARGBUnattenuateRow = ARGBUnattenuateRow_SSE2; |
| 1353 | } |
| 1354 | } |
| 1355 | #endif |
| 1356 | #if defined(HAS_ARGBUNATTENUATEROW_AVX2) |
| 1357 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1358 | ARGBUnattenuateRow = ARGBUnattenuateRow_Any_AVX2; |
| 1359 | if (IS_ALIGNED(width, 8)) { |
| 1360 | ARGBUnattenuateRow = ARGBUnattenuateRow_AVX2; |
| 1361 | } |
| 1362 | } |
| 1363 | #endif |
| 1364 | // TODO(fbarchard): Neon version. |
| 1365 |
| 1366 | for (y = 0; y < height; ++y) { |
| 1367 | ARGBUnattenuateRow(src_argb, dst_argb, width); |
| 1368 | src_argb += src_stride_argb; |
| 1369 | dst_argb += dst_stride_argb; |
| 1370 | } |
| 1371 | return 0; |
| 1372 | } |
| 1373 |
| 1374 | // Convert ARGB to Grayed ARGB. |
| 1375 | LIBYUV_API |
| 1376 | int ARGBGrayTo(const uint8* src_argb, int src_stride_argb, |
| 1377 | uint8* dst_argb, int dst_stride_argb, |
| 1378 | int width, int height) { |
| 1379 | int y; |
| 1380 | void (*ARGBGrayRow)(const uint8* src_argb, uint8* dst_argb, |
| 1381 | int width) = ARGBGrayRow_C; |
| 1382 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 1383 | return -1; |
| 1384 | } |
| 1385 | if (height < 0) { |
| 1386 | height = -height; |
| 1387 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 1388 | src_stride_argb = -src_stride_argb; |
| 1389 | } |
| 1390 | // Coalesce rows. |
| 1391 | if (src_stride_argb == width * 4 && |
| 1392 | dst_stride_argb == width * 4) { |
| 1393 | width *= height; |
| 1394 | height = 1; |
| 1395 | src_stride_argb = dst_stride_argb = 0; |
| 1396 | } |
| 1397 | #if defined(HAS_ARGBGRAYROW_SSSE3) |
| 1398 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 1399 | ARGBGrayRow = ARGBGrayRow_SSSE3; |
| 1400 | } |
| 1401 | #endif |
| 1402 | #if defined(HAS_ARGBGRAYROW_NEON) |
| 1403 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 1404 | ARGBGrayRow = ARGBGrayRow_NEON; |
| 1405 | } |
| 1406 | #endif |
| 1407 |
| 1408 | for (y = 0; y < height; ++y) { |
| 1409 | ARGBGrayRow(src_argb, dst_argb, width); |
| 1410 | src_argb += src_stride_argb; |
| 1411 | dst_argb += dst_stride_argb; |
| 1412 | } |
| 1413 | return 0; |
| 1414 | } |
| 1415 |
| 1416 | // Make a rectangle of ARGB gray scale. |
| 1417 | LIBYUV_API |
| 1418 | int ARGBGray(uint8* dst_argb, int dst_stride_argb, |
| 1419 | int dst_x, int dst_y, |
| 1420 | int width, int height) { |
| 1421 | int y; |
| 1422 | void (*ARGBGrayRow)(const uint8* src_argb, uint8* dst_argb, |
| 1423 | int width) = ARGBGrayRow_C; |
| 1424 | uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 1425 | if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) { |
| 1426 | return -1; |
| 1427 | } |
| 1428 | // Coalesce rows. |
| 1429 | if (dst_stride_argb == width * 4) { |
| 1430 | width *= height; |
| 1431 | height = 1; |
| 1432 | dst_stride_argb = 0; |
| 1433 | } |
| 1434 | #if defined(HAS_ARGBGRAYROW_SSSE3) |
| 1435 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 1436 | ARGBGrayRow = ARGBGrayRow_SSSE3; |
| 1437 | } |
| 1438 | #endif |
| 1439 | #if defined(HAS_ARGBGRAYROW_NEON) |
| 1440 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 1441 | ARGBGrayRow = ARGBGrayRow_NEON; |
| 1442 | } |
| 1443 | #endif |
| 1444 | for (y = 0; y < height; ++y) { |
| 1445 | ARGBGrayRow(dst, dst, width); |
| 1446 | dst += dst_stride_argb; |
| 1447 | } |
| 1448 | return 0; |
| 1449 | } |
| 1450 |
| 1451 | // Make a rectangle of ARGB Sepia tone. |
| 1452 | LIBYUV_API |
| 1453 | int ARGBSepia(uint8* dst_argb, int dst_stride_argb, |
| 1454 | int dst_x, int dst_y, int width, int height) { |
| 1455 | int y; |
| 1456 | void (*ARGBSepiaRow)(uint8* dst_argb, int width) = ARGBSepiaRow_C; |
| 1457 | uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 1458 | if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) { |
| 1459 | return -1; |
| 1460 | } |
| 1461 | // Coalesce rows. |
| 1462 | if (dst_stride_argb == width * 4) { |
| 1463 | width *= height; |
| 1464 | height = 1; |
| 1465 | dst_stride_argb = 0; |
| 1466 | } |
| 1467 | #if defined(HAS_ARGBSEPIAROW_SSSE3) |
| 1468 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 1469 | ARGBSepiaRow = ARGBSepiaRow_SSSE3; |
| 1470 | } |
| 1471 | #endif |
| 1472 | #if defined(HAS_ARGBSEPIAROW_NEON) |
| 1473 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 1474 | ARGBSepiaRow = ARGBSepiaRow_NEON; |
| 1475 | } |
| 1476 | #endif |
| 1477 | for (y = 0; y < height; ++y) { |
| 1478 | ARGBSepiaRow(dst, width); |
| 1479 | dst += dst_stride_argb; |
| 1480 | } |
| 1481 | return 0; |
| 1482 | } |
| 1483 |
| 1484 | // Apply a 4x4 matrix to each ARGB pixel. |
| 1485 | // Note: Normally for shading, but can be used to swizzle or invert. |
| 1486 | LIBYUV_API |
| 1487 | int ARGBColorMatrix(const uint8* src_argb, int src_stride_argb, |
| 1488 | uint8* dst_argb, int dst_stride_argb, |
| 1489 | const int8* matrix_argb, |
| 1490 | int width, int height) { |
| 1491 | int y; |
| 1492 | void (*ARGBColorMatrixRow)(const uint8* src_argb, uint8* dst_argb, |
| 1493 | const int8* matrix_argb, int width) = ARGBColorMatrixRow_C; |
| 1494 | if (!src_argb || !dst_argb || !matrix_argb || width <= 0 || height == 0) { |
| 1495 | return -1; |
| 1496 | } |
| 1497 | if (height < 0) { |
| 1498 | height = -height; |
| 1499 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 1500 | src_stride_argb = -src_stride_argb; |
| 1501 | } |
| 1502 | // Coalesce rows. |
| 1503 | if (src_stride_argb == width * 4 && |
| 1504 | dst_stride_argb == width * 4) { |
| 1505 | width *= height; |
| 1506 | height = 1; |
| 1507 | src_stride_argb = dst_stride_argb = 0; |
| 1508 | } |
| 1509 | #if defined(HAS_ARGBCOLORMATRIXROW_SSSE3) |
| 1510 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) { |
| 1511 | ARGBColorMatrixRow = ARGBColorMatrixRow_SSSE3; |
| 1512 | } |
| 1513 | #endif |
| 1514 | #if defined(HAS_ARGBCOLORMATRIXROW_NEON) |
| 1515 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 1516 | ARGBColorMatrixRow = ARGBColorMatrixRow_NEON; |
| 1517 | } |
| 1518 | #endif |
| 1519 | for (y = 0; y < height; ++y) { |
| 1520 | ARGBColorMatrixRow(src_argb, dst_argb, matrix_argb, width); |
| 1521 | src_argb += src_stride_argb; |
| 1522 | dst_argb += dst_stride_argb; |
| 1523 | } |
| 1524 | return 0; |
| 1525 | } |
| 1526 |
| 1527 | // Apply a 4x3 matrix to each ARGB pixel. |
| 1528 | // Deprecated. |
| 1529 | LIBYUV_API |
| 1530 | int RGBColorMatrix(uint8* dst_argb, int dst_stride_argb, |
| 1531 | const int8* matrix_rgb, |
| 1532 | int dst_x, int dst_y, int width, int height) { |
| 1533 | SIMD_ALIGNED(int8 matrix_argb[16]); |
| 1534 | uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 1535 | if (!dst_argb || !matrix_rgb || width <= 0 || height <= 0 || |
| 1536 | dst_x < 0 || dst_y < 0) { |
| 1537 | return -1; |
| 1538 | } |
| 1539 |
| 1540 | // Convert 4x3 7 bit matrix to 4x4 6 bit matrix. |
| 1541 | matrix_argb[0] = matrix_rgb[0] / 2; |
| 1542 | matrix_argb[1] = matrix_rgb[1] / 2; |
| 1543 | matrix_argb[2] = matrix_rgb[2] / 2; |
| 1544 | matrix_argb[3] = matrix_rgb[3] / 2; |
| 1545 | matrix_argb[4] = matrix_rgb[4] / 2; |
| 1546 | matrix_argb[5] = matrix_rgb[5] / 2; |
| 1547 | matrix_argb[6] = matrix_rgb[6] / 2; |
| 1548 | matrix_argb[7] = matrix_rgb[7] / 2; |
| 1549 | matrix_argb[8] = matrix_rgb[8] / 2; |
| 1550 | matrix_argb[9] = matrix_rgb[9] / 2; |
| 1551 | matrix_argb[10] = matrix_rgb[10] / 2; |
| 1552 | matrix_argb[11] = matrix_rgb[11] / 2; |
| 1553 | matrix_argb[14] = matrix_argb[13] = matrix_argb[12] = 0; |
| 1554 | matrix_argb[15] = 64; // 1.0 |
| 1555 |
| 1556 | return ARGBColorMatrix((const uint8*)(dst), dst_stride_argb, |
| 1557 | dst, dst_stride_argb, |
| 1558 | &matrix_argb[0], width, height); |
| 1559 | } |
| 1560 |
| 1561 | // Apply a color table each ARGB pixel. |
| 1562 | // Table contains 256 ARGB values. |
| 1563 | LIBYUV_API |
| 1564 | int ARGBColorTable(uint8* dst_argb, int dst_stride_argb, |
| 1565 | const uint8* table_argb, |
| 1566 | int dst_x, int dst_y, int width, int height) { |
| 1567 | int y; |
| 1568 | void (*ARGBColorTableRow)(uint8* dst_argb, const uint8* table_argb, |
| 1569 | int width) = ARGBColorTableRow_C; |
| 1570 | uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 1571 | if (!dst_argb || !table_argb || width <= 0 || height <= 0 || |
| 1572 | dst_x < 0 || dst_y < 0) { |
| 1573 | return -1; |
| 1574 | } |
| 1575 | // Coalesce rows. |
| 1576 | if (dst_stride_argb == width * 4) { |
| 1577 | width *= height; |
| 1578 | height = 1; |
| 1579 | dst_stride_argb = 0; |
| 1580 | } |
| 1581 | #if defined(HAS_ARGBCOLORTABLEROW_X86) |
| 1582 | if (TestCpuFlag(kCpuHasX86)) { |
| 1583 | ARGBColorTableRow = ARGBColorTableRow_X86; |
| 1584 | } |
| 1585 | #endif |
| 1586 | for (y = 0; y < height; ++y) { |
| 1587 | ARGBColorTableRow(dst, table_argb, width); |
| 1588 | dst += dst_stride_argb; |
| 1589 | } |
| 1590 | return 0; |
| 1591 | } |
| 1592 |
| 1593 | // Apply a color table each ARGB pixel but preserve destination alpha. |
| 1594 | // Table contains 256 ARGB values. |
| 1595 | LIBYUV_API |
| 1596 | int RGBColorTable(uint8* dst_argb, int dst_stride_argb, |
| 1597 | const uint8* table_argb, |
| 1598 | int dst_x, int dst_y, int width, int height) { |
| 1599 | int y; |
| 1600 | void (*RGBColorTableRow)(uint8* dst_argb, const uint8* table_argb, |
| 1601 | int width) = RGBColorTableRow_C; |
| 1602 | uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 1603 | if (!dst_argb || !table_argb || width <= 0 || height <= 0 || |
| 1604 | dst_x < 0 || dst_y < 0) { |
| 1605 | return -1; |
| 1606 | } |
| 1607 | // Coalesce rows. |
| 1608 | if (dst_stride_argb == width * 4) { |
| 1609 | width *= height; |
| 1610 | height = 1; |
| 1611 | dst_stride_argb = 0; |
| 1612 | } |
| 1613 | #if defined(HAS_RGBCOLORTABLEROW_X86) |
| 1614 | if (TestCpuFlag(kCpuHasX86)) { |
| 1615 | RGBColorTableRow = RGBColorTableRow_X86; |
| 1616 | } |
| 1617 | #endif |
| 1618 | for (y = 0; y < height; ++y) { |
| 1619 | RGBColorTableRow(dst, table_argb, width); |
| 1620 | dst += dst_stride_argb; |
| 1621 | } |
| 1622 | return 0; |
| 1623 | } |
| 1624 |
| 1625 | // ARGBQuantize is used to posterize art. |
| 1626 | // e.g. rgb / qvalue * qvalue + qvalue / 2 |
| 1627 | // But the low levels implement efficiently with 3 parameters, and could be |
| 1628 | // used for other high level operations. |
| 1629 | // dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset; |
| 1630 | // where scale is 1 / interval_size as a fixed point value. |
| 1631 | // The divide is replaces with a multiply by reciprocal fixed point multiply. |
| 1632 | // Caveat - although SSE2 saturates, the C function does not and should be used |
| 1633 | // with care if doing anything but quantization. |
| 1634 | LIBYUV_API |
| 1635 | int ARGBQuantize(uint8* dst_argb, int dst_stride_argb, |
| 1636 | int scale, int interval_size, int interval_offset, |
| 1637 | int dst_x, int dst_y, int width, int height) { |
| 1638 | int y; |
| 1639 | void (*ARGBQuantizeRow)(uint8* dst_argb, int scale, int interval_size, |
| 1640 | int interval_offset, int width) = ARGBQuantizeRow_C; |
| 1641 | uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; |
| 1642 | if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0 || |
| 1643 | interval_size < 1 || interval_size > 255) { |
| 1644 | return -1; |
| 1645 | } |
| 1646 | // Coalesce rows. |
| 1647 | if (dst_stride_argb == width * 4) { |
| 1648 | width *= height; |
| 1649 | height = 1; |
| 1650 | dst_stride_argb = 0; |
| 1651 | } |
| 1652 | #if defined(HAS_ARGBQUANTIZEROW_SSE2) |
| 1653 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) { |
| 1654 | ARGBQuantizeRow = ARGBQuantizeRow_SSE2; |
| 1655 | } |
| 1656 | #endif |
| 1657 | #if defined(HAS_ARGBQUANTIZEROW_NEON) |
| 1658 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 1659 | ARGBQuantizeRow = ARGBQuantizeRow_NEON; |
| 1660 | } |
| 1661 | #endif |
| 1662 | for (y = 0; y < height; ++y) { |
| 1663 | ARGBQuantizeRow(dst, scale, interval_size, interval_offset, width); |
| 1664 | dst += dst_stride_argb; |
| 1665 | } |
| 1666 | return 0; |
| 1667 | } |
| 1668 |
| 1669 | // Computes table of cumulative sum for image where the value is the sum |
| 1670 | // of all values above and to the left of the entry. Used by ARGBBlur. |
| 1671 | LIBYUV_API |
| 1672 | int ARGBComputeCumulativeSum(const uint8* src_argb, int src_stride_argb, |
| 1673 | int32* dst_cumsum, int dst_stride32_cumsum, |
| 1674 | int width, int height) { |
| 1675 | int y; |
| 1676 | void (*ComputeCumulativeSumRow)(const uint8* row, int32* cumsum, |
| 1677 | const int32* previous_cumsum, int width) = ComputeCumulativeSumRow_C; |
| 1678 | int32* previous_cumsum = dst_cumsum; |
| 1679 | if (!dst_cumsum || !src_argb || width <= 0 || height <= 0) { |
| 1680 | return -1; |
| 1681 | } |
| 1682 | #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2) |
| 1683 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1684 | ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2; |
| 1685 | } |
| 1686 | #endif |
| 1687 | memset(dst_cumsum, 0, width * sizeof(dst_cumsum[0]) * 4); // 4 int per pixel. |
| 1688 | for (y = 0; y < height; ++y) { |
| 1689 | ComputeCumulativeSumRow(src_argb, dst_cumsum, previous_cumsum, width); |
| 1690 | previous_cumsum = dst_cumsum; |
| 1691 | dst_cumsum += dst_stride32_cumsum; |
| 1692 | src_argb += src_stride_argb; |
| 1693 | } |
| 1694 | return 0; |
| 1695 | } |
| 1696 |
| 1697 | // Blur ARGB image. |
| 1698 | // Caller should allocate CumulativeSum table of width * height * 16 bytes |
| 1699 | // aligned to 16 byte boundary. height can be radius * 2 + 2 to save memory |
| 1700 | // as the buffer is treated as circular. |
| 1701 | LIBYUV_API |
| 1702 | int ARGBBlur(const uint8* src_argb, int src_stride_argb, |
| 1703 | uint8* dst_argb, int dst_stride_argb, |
| 1704 | int32* dst_cumsum, int dst_stride32_cumsum, |
| 1705 | int width, int height, int radius) { |
| 1706 | int y; |
| 1707 | void (*ComputeCumulativeSumRow)(const uint8 *row, int32 *cumsum, |
| 1708 | const int32* previous_cumsum, int width) = ComputeCumulativeSumRow_C; |
| 1709 | void (*CumulativeSumToAverageRow)(const int32* topleft, const int32* botleft, |
| 1710 | int width, int area, uint8* dst, int count) = CumulativeSumToAverageRow_C; |
| 1711 | int32* cumsum_bot_row; |
| 1712 | int32* max_cumsum_bot_row; |
| 1713 | int32* cumsum_top_row; |
| 1714 |
| 1715 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 1716 | return -1; |
| 1717 | } |
| 1718 | if (height < 0) { |
| 1719 | height = -height; |
| 1720 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 1721 | src_stride_argb = -src_stride_argb; |
| 1722 | } |
| 1723 | if (radius > height) { |
| 1724 | radius = height; |
| 1725 | } |
| 1726 | if (radius > (width / 2 - 1)) { |
| 1727 | radius = width / 2 - 1; |
| 1728 | } |
| 1729 | if (radius <= 0) { |
| 1730 | return -1; |
| 1731 | } |
| 1732 | #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2) |
| 1733 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1734 | ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2; |
| 1735 | CumulativeSumToAverageRow = CumulativeSumToAverageRow_SSE2; |
| 1736 | } |
| 1737 | #endif |
| 1738 | // Compute enough CumulativeSum for first row to be blurred. After this |
| 1739 | // one row of CumulativeSum is updated at a time. |
| 1740 | ARGBComputeCumulativeSum(src_argb, src_stride_argb, |
| 1741 | dst_cumsum, dst_stride32_cumsum, |
| 1742 | width, radius); |
| 1743 |
| 1744 | src_argb = src_argb + radius * src_stride_argb; |
| 1745 | cumsum_bot_row = &dst_cumsum[(radius - 1) * dst_stride32_cumsum]; |
| 1746 |
| 1747 | max_cumsum_bot_row = &dst_cumsum[(radius * 2 + 2) * dst_stride32_cumsum]; |
| 1748 | cumsum_top_row = &dst_cumsum[0]; |
| 1749 |
| 1750 | for (y = 0; y < height; ++y) { |
| 1751 | int top_y = ((y - radius - 1) >= 0) ? (y - radius - 1) : 0; |
| 1752 | int bot_y = ((y + radius) < height) ? (y + radius) : (height - 1); |
| 1753 | int area = radius * (bot_y - top_y); |
| 1754 | int boxwidth = radius * 4; |
| 1755 | int x; |
| 1756 | int n; |
| 1757 |
| 1758 | // Increment cumsum_top_row pointer with circular buffer wrap around. |
| 1759 | if (top_y) { |
| 1760 | cumsum_top_row += dst_stride32_cumsum; |
| 1761 | if (cumsum_top_row >= max_cumsum_bot_row) { |
| 1762 | cumsum_top_row = dst_cumsum; |
| 1763 | } |
| 1764 | } |
| 1765 | // Increment cumsum_bot_row pointer with circular buffer wrap around and |
| 1766 | // then fill in a row of CumulativeSum. |
| 1767 | if ((y + radius) < height) { |
| 1768 | const int32* prev_cumsum_bot_row = cumsum_bot_row; |
| 1769 | cumsum_bot_row += dst_stride32_cumsum; |
| 1770 | if (cumsum_bot_row >= max_cumsum_bot_row) { |
| 1771 | cumsum_bot_row = dst_cumsum; |
| 1772 | } |
| 1773 | ComputeCumulativeSumRow(src_argb, cumsum_bot_row, prev_cumsum_bot_row, |
| 1774 | width); |
| 1775 | src_argb += src_stride_argb; |
| 1776 | } |
| 1777 |
| 1778 | // Left clipped. |
| 1779 | for (x = 0; x < radius + 1; ++x) { |
| 1780 | CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, |
| 1781 | boxwidth, area, &dst_argb[x * 4], 1); |
| 1782 | area += (bot_y - top_y); |
| 1783 | boxwidth += 4; |
| 1784 | } |
| 1785 |
| 1786 | // Middle unclipped. |
| 1787 | n = (width - 1) - radius - x + 1; |
| 1788 | CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, |
| 1789 | boxwidth, area, &dst_argb[x * 4], n); |
| 1790 |
| 1791 | // Right clipped. |
| 1792 | for (x += n; x <= width - 1; ++x) { |
| 1793 | area -= (bot_y - top_y); |
| 1794 | boxwidth -= 4; |
| 1795 | CumulativeSumToAverageRow(cumsum_top_row + (x - radius - 1) * 4, |
| 1796 | cumsum_bot_row + (x - radius - 1) * 4, |
| 1797 | boxwidth, area, &dst_argb[x * 4], 1); |
| 1798 | } |
| 1799 | dst_argb += dst_stride_argb; |
| 1800 | } |
| 1801 | return 0; |
| 1802 | } |
| 1803 |
| 1804 | // Multiply ARGB image by a specified ARGB value. |
| 1805 | LIBYUV_API |
| 1806 | int ARGBShade(const uint8* src_argb, int src_stride_argb, |
| 1807 | uint8* dst_argb, int dst_stride_argb, |
| 1808 | int width, int height, uint32 value) { |
| 1809 | int y; |
| 1810 | void (*ARGBShadeRow)(const uint8* src_argb, uint8* dst_argb, |
| 1811 | int width, uint32 value) = ARGBShadeRow_C; |
| 1812 | if (!src_argb || !dst_argb || width <= 0 || height == 0 || value == 0u) { |
| 1813 | return -1; |
| 1814 | } |
| 1815 | if (height < 0) { |
| 1816 | height = -height; |
| 1817 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 1818 | src_stride_argb = -src_stride_argb; |
| 1819 | } |
| 1820 | // Coalesce rows. |
| 1821 | if (src_stride_argb == width * 4 && |
| 1822 | dst_stride_argb == width * 4) { |
| 1823 | width *= height; |
| 1824 | height = 1; |
| 1825 | src_stride_argb = dst_stride_argb = 0; |
| 1826 | } |
| 1827 | #if defined(HAS_ARGBSHADEROW_SSE2) |
| 1828 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) { |
| 1829 | ARGBShadeRow = ARGBShadeRow_SSE2; |
| 1830 | } |
| 1831 | #endif |
| 1832 | #if defined(HAS_ARGBSHADEROW_NEON) |
| 1833 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) { |
| 1834 | ARGBShadeRow = ARGBShadeRow_NEON; |
| 1835 | } |
| 1836 | #endif |
| 1837 |
| 1838 | for (y = 0; y < height; ++y) { |
| 1839 | ARGBShadeRow(src_argb, dst_argb, width, value); |
| 1840 | src_argb += src_stride_argb; |
| 1841 | dst_argb += dst_stride_argb; |
| 1842 | } |
| 1843 | return 0; |
| 1844 | } |
| 1845 |
| 1846 | // Interpolate 2 planes by specified amount (0 to 255). |
| 1847 | LIBYUV_API |
| 1848 | int InterpolatePlane(const uint8* src0, int src_stride0, |
| 1849 | const uint8* src1, int src_stride1, |
| 1850 | uint8* dst, int dst_stride, |
| 1851 | int width, int height, int interpolation) { |
| 1852 | int y; |
| 1853 | void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr, |
| 1854 | ptrdiff_t src_stride, int dst_width, |
| 1855 | int source_y_fraction) = InterpolateRow_C; |
| 1856 | if (!src0 || !src1 || !dst || width <= 0 || height == 0) { |
| 1857 | return -1; |
| 1858 | } |
| 1859 | // Negative height means invert the image. |
| 1860 | if (height < 0) { |
| 1861 | height = -height; |
| 1862 | dst = dst + (height - 1) * dst_stride; |
| 1863 | dst_stride = -dst_stride; |
| 1864 | } |
| 1865 | // Coalesce rows. |
| 1866 | if (src_stride0 == width && |
| 1867 | src_stride1 == width && |
| 1868 | dst_stride == width) { |
| 1869 | width *= height; |
| 1870 | height = 1; |
| 1871 | src_stride0 = src_stride1 = dst_stride = 0; |
| 1872 | } |
| 1873 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 1874 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1875 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 1876 | if (IS_ALIGNED(width, 16)) { |
| 1877 | InterpolateRow = InterpolateRow_SSSE3; |
| 1878 | } |
| 1879 | } |
| 1880 | #endif |
| 1881 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 1882 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1883 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 1884 | if (IS_ALIGNED(width, 32)) { |
| 1885 | InterpolateRow = InterpolateRow_AVX2; |
| 1886 | } |
| 1887 | } |
| 1888 | #endif |
| 1889 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 1890 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1891 | InterpolateRow = InterpolateRow_Any_NEON; |
| 1892 | if (IS_ALIGNED(width, 16)) { |
| 1893 | InterpolateRow = InterpolateRow_NEON; |
| 1894 | } |
| 1895 | } |
| 1896 | #endif |
| 1897 | #if defined(HAS_INTERPOLATEROW_MIPS_DSPR2) |
| 1898 | if (TestCpuFlag(kCpuHasMIPS_DSPR2) && |
| 1899 | IS_ALIGNED(src0, 4) && IS_ALIGNED(src_stride0, 4) && |
| 1900 | IS_ALIGNED(src1, 4) && IS_ALIGNED(src_stride1, 4) && |
| 1901 | IS_ALIGNED(dst, 4) && IS_ALIGNED(dst_stride, 4) && |
| 1902 | IS_ALIGNED(width, 4)) { |
| 1903 | InterpolateRow = InterpolateRow_MIPS_DSPR2; |
| 1904 | } |
| 1905 | #endif |
| 1906 |
| 1907 | for (y = 0; y < height; ++y) { |
| 1908 | InterpolateRow(dst, src0, src1 - src0, |
| 1909 | width, interpolation); |
| 1910 | src0 += src_stride0; |
| 1911 | src1 += src_stride1; |
| 1912 | dst += dst_stride; |
| 1913 | } |
| 1914 | return 0; |
| 1915 | } |
| 1916 |
| 1917 | // Interpolate 2 ARGB images by specified amount (0 to 255). |
| 1918 | LIBYUV_API |
| 1919 | int ARGBInterpolate(const uint8* src_argb0, int src_stride_argb0, |
| 1920 | const uint8* src_argb1, int src_stride_argb1, |
| 1921 | uint8* dst_argb, int dst_stride_argb, |
| 1922 | int width, int height, int interpolation) { |
| 1923 | return InterpolatePlane(src_argb0, src_stride_argb0, |
| 1924 | src_argb1, src_stride_argb1, |
| 1925 | dst_argb, dst_stride_argb, |
| 1926 | width * 4, height, interpolation); |
| 1927 | } |
| 1928 |
| 1929 | // Interpolate 2 YUV images by specified amount (0 to 255). |
| 1930 | LIBYUV_API |
| 1931 | int I420Interpolate(const uint8* src0_y, int src0_stride_y, |
| 1932 | const uint8* src0_u, int src0_stride_u, |
| 1933 | const uint8* src0_v, int src0_stride_v, |
| 1934 | const uint8* src1_y, int src1_stride_y, |
| 1935 | const uint8* src1_u, int src1_stride_u, |
| 1936 | const uint8* src1_v, int src1_stride_v, |
| 1937 | uint8* dst_y, int dst_stride_y, |
| 1938 | uint8* dst_u, int dst_stride_u, |
| 1939 | uint8* dst_v, int dst_stride_v, |
| 1940 | int width, int height, int interpolation) { |
| 1941 | int halfwidth = (width + 1) >> 1; |
| 1942 | int halfheight = (height + 1) >> 1; |
| 1943 | if (!src0_y || !src0_u || !src0_v || |
| 1944 | !src1_y || !src1_u || !src1_v || |
| 1945 | !dst_y || !dst_u || !dst_v || |
| 1946 | width <= 0 || height == 0) { |
| 1947 | return -1; |
| 1948 | } |
| 1949 | InterpolatePlane(src0_y, src0_stride_y, |
| 1950 | src1_y, src1_stride_y, |
| 1951 | dst_y, dst_stride_y, |
| 1952 | width, height, interpolation); |
| 1953 | InterpolatePlane(src0_u, src0_stride_u, |
| 1954 | src1_u, src1_stride_u, |
| 1955 | dst_u, dst_stride_u, |
| 1956 | halfwidth, halfheight, interpolation); |
| 1957 | InterpolatePlane(src0_v, src0_stride_v, |
| 1958 | src1_v, src1_stride_v, |
| 1959 | dst_v, dst_stride_v, |
| 1960 | halfwidth, halfheight, interpolation); |
| 1961 | return 0; |
| 1962 | } |
| 1963 |
| 1964 | // Shuffle ARGB channel order. e.g. BGRA to ARGB. |
| 1965 | LIBYUV_API |
| 1966 | int ARGBShuffle(const uint8* src_bgra, int src_stride_bgra, |
| 1967 | uint8* dst_argb, int dst_stride_argb, |
| 1968 | const uint8* shuffler, int width, int height) { |
| 1969 | int y; |
| 1970 | void (*ARGBShuffleRow)(const uint8* src_bgra, uint8* dst_argb, |
| 1971 | const uint8* shuffler, int width) = ARGBShuffleRow_C; |
| 1972 | if (!src_bgra || !dst_argb || |
| 1973 | width <= 0 || height == 0) { |
| 1974 | return -1; |
| 1975 | } |
| 1976 | // Negative height means invert the image. |
| 1977 | if (height < 0) { |
| 1978 | height = -height; |
| 1979 | src_bgra = src_bgra + (height - 1) * src_stride_bgra; |
| 1980 | src_stride_bgra = -src_stride_bgra; |
| 1981 | } |
| 1982 | // Coalesce rows. |
| 1983 | if (src_stride_bgra == width * 4 && |
| 1984 | dst_stride_argb == width * 4) { |
| 1985 | width *= height; |
| 1986 | height = 1; |
| 1987 | src_stride_bgra = dst_stride_argb = 0; |
| 1988 | } |
| 1989 | #if defined(HAS_ARGBSHUFFLEROW_SSE2) |
| 1990 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1991 | ARGBShuffleRow = ARGBShuffleRow_Any_SSE2; |
| 1992 | if (IS_ALIGNED(width, 4)) { |
| 1993 | ARGBShuffleRow = ARGBShuffleRow_SSE2; |
| 1994 | } |
| 1995 | } |
| 1996 | #endif |
| 1997 | #if defined(HAS_ARGBSHUFFLEROW_SSSE3) |
| 1998 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1999 | ARGBShuffleRow = ARGBShuffleRow_Any_SSSE3; |
| 2000 | if (IS_ALIGNED(width, 8)) { |
| 2001 | ARGBShuffleRow = ARGBShuffleRow_SSSE3; |
| 2002 | } |
| 2003 | } |
| 2004 | #endif |
| 2005 | #if defined(HAS_ARGBSHUFFLEROW_AVX2) |
| 2006 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2007 | ARGBShuffleRow = ARGBShuffleRow_Any_AVX2; |
| 2008 | if (IS_ALIGNED(width, 16)) { |
| 2009 | ARGBShuffleRow = ARGBShuffleRow_AVX2; |
| 2010 | } |
| 2011 | } |
| 2012 | #endif |
| 2013 | #if defined(HAS_ARGBSHUFFLEROW_NEON) |
| 2014 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2015 | ARGBShuffleRow = ARGBShuffleRow_Any_NEON; |
| 2016 | if (IS_ALIGNED(width, 4)) { |
| 2017 | ARGBShuffleRow = ARGBShuffleRow_NEON; |
| 2018 | } |
| 2019 | } |
| 2020 | #endif |
| 2021 |
| 2022 | for (y = 0; y < height; ++y) { |
| 2023 | ARGBShuffleRow(src_bgra, dst_argb, shuffler, width); |
| 2024 | src_bgra += src_stride_bgra; |
| 2025 | dst_argb += dst_stride_argb; |
| 2026 | } |
| 2027 | return 0; |
| 2028 | } |
| 2029 |
| 2030 | // Sobel ARGB effect. |
| 2031 | static int ARGBSobelize(const uint8* src_argb, int src_stride_argb, |
| 2032 | uint8* dst_argb, int dst_stride_argb, |
| 2033 | int width, int height, |
| 2034 | void (*SobelRow)(const uint8* src_sobelx, |
| 2035 | const uint8* src_sobely, |
| 2036 | uint8* dst, int width)) { |
| 2037 | int y; |
| 2038 | void (*ARGBToYJRow)(const uint8* src_argb, uint8* dst_g, int width) = |
| 2039 | ARGBToYJRow_C; |
| 2040 | void (*SobelYRow)(const uint8* src_y0, const uint8* src_y1, |
| 2041 | uint8* dst_sobely, int width) = SobelYRow_C; |
| 2042 | void (*SobelXRow)(const uint8* src_y0, const uint8* src_y1, |
| 2043 | const uint8* src_y2, uint8* dst_sobely, int width) = |
| 2044 | SobelXRow_C; |
| 2045 | const int kEdge = 16; // Extra pixels at start of row for extrude/align. |
| 2046 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 2047 | return -1; |
| 2048 | } |
| 2049 | // Negative height means invert the image. |
| 2050 | if (height < 0) { |
| 2051 | height = -height; |
| 2052 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2053 | src_stride_argb = -src_stride_argb; |
| 2054 | } |
| 2055 |
| 2056 | #if defined(HAS_ARGBTOYJROW_SSSE3) |
| 2057 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2058 | ARGBToYJRow = ARGBToYJRow_Any_SSSE3; |
| 2059 | if (IS_ALIGNED(width, 16)) { |
| 2060 | ARGBToYJRow = ARGBToYJRow_SSSE3; |
| 2061 | } |
| 2062 | } |
| 2063 | #endif |
| 2064 | #if defined(HAS_ARGBTOYJROW_AVX2) |
| 2065 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2066 | ARGBToYJRow = ARGBToYJRow_Any_AVX2; |
| 2067 | if (IS_ALIGNED(width, 32)) { |
| 2068 | ARGBToYJRow = ARGBToYJRow_AVX2; |
| 2069 | } |
| 2070 | } |
| 2071 | #endif |
| 2072 | #if defined(HAS_ARGBTOYJROW_NEON) |
| 2073 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2074 | ARGBToYJRow = ARGBToYJRow_Any_NEON; |
| 2075 | if (IS_ALIGNED(width, 8)) { |
| 2076 | ARGBToYJRow = ARGBToYJRow_NEON; |
| 2077 | } |
| 2078 | } |
| 2079 | #endif |
| 2080 |
| 2081 | #if defined(HAS_SOBELYROW_SSE2) |
| 2082 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2083 | SobelYRow = SobelYRow_SSE2; |
| 2084 | } |
| 2085 | #endif |
| 2086 | #if defined(HAS_SOBELYROW_NEON) |
| 2087 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2088 | SobelYRow = SobelYRow_NEON; |
| 2089 | } |
| 2090 | #endif |
| 2091 | #if defined(HAS_SOBELXROW_SSE2) |
| 2092 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2093 | SobelXRow = SobelXRow_SSE2; |
| 2094 | } |
| 2095 | #endif |
| 2096 | #if defined(HAS_SOBELXROW_NEON) |
| 2097 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2098 | SobelXRow = SobelXRow_NEON; |
| 2099 | } |
| 2100 | #endif |
| 2101 | { |
| 2102 | // 3 rows with edges before/after. |
| 2103 | const int kRowSize = (width + kEdge + 31) & ~31; |
| 2104 | align_buffer_64(rows, kRowSize * 2 + (kEdge + kRowSize * 3 + kEdge)); |
| 2105 | uint8* row_sobelx = rows; |
| 2106 | uint8* row_sobely = rows + kRowSize; |
| 2107 | uint8* row_y = rows + kRowSize * 2; |
| 2108 |
| 2109 | // Convert first row. |
| 2110 | uint8* row_y0 = row_y + kEdge; |
| 2111 | uint8* row_y1 = row_y0 + kRowSize; |
| 2112 | uint8* row_y2 = row_y1 + kRowSize; |
| 2113 | ARGBToYJRow(src_argb, row_y0, width); |
| 2114 | row_y0[-1] = row_y0[0]; |
| 2115 | memset(row_y0 + width, row_y0[width - 1], 16); // Extrude 16 for valgrind. |
| 2116 | ARGBToYJRow(src_argb, row_y1, width); |
| 2117 | row_y1[-1] = row_y1[0]; |
| 2118 | memset(row_y1 + width, row_y1[width - 1], 16); |
| 2119 | memset(row_y2 + width, 0, 16); |
| 2120 |
| 2121 | for (y = 0; y < height; ++y) { |
| 2122 | // Convert next row of ARGB to G. |
| 2123 | if (y < (height - 1)) { |
| 2124 | src_argb += src_stride_argb; |
| 2125 | } |
| 2126 | ARGBToYJRow(src_argb, row_y2, width); |
| 2127 | row_y2[-1] = row_y2[0]; |
| 2128 | row_y2[width] = row_y2[width - 1]; |
| 2129 |
| 2130 | SobelXRow(row_y0 - 1, row_y1 - 1, row_y2 - 1, row_sobelx, width); |
| 2131 | SobelYRow(row_y0 - 1, row_y2 - 1, row_sobely, width); |
| 2132 | SobelRow(row_sobelx, row_sobely, dst_argb, width); |
| 2133 |
| 2134 | // Cycle thru circular queue of 3 row_y buffers. |
| 2135 | { |
| 2136 | uint8* row_yt = row_y0; |
| 2137 | row_y0 = row_y1; |
| 2138 | row_y1 = row_y2; |
| 2139 | row_y2 = row_yt; |
| 2140 | } |
| 2141 |
| 2142 | dst_argb += dst_stride_argb; |
| 2143 | } |
| 2144 | free_aligned_buffer_64(rows); |
| 2145 | } |
| 2146 | return 0; |
| 2147 | } |
| 2148 |
| 2149 | // Sobel ARGB effect. |
| 2150 | LIBYUV_API |
| 2151 | int ARGBSobel(const uint8* src_argb, int src_stride_argb, |
| 2152 | uint8* dst_argb, int dst_stride_argb, |
| 2153 | int width, int height) { |
| 2154 | void (*SobelRow)(const uint8* src_sobelx, const uint8* src_sobely, |
| 2155 | uint8* dst_argb, int width) = SobelRow_C; |
| 2156 | #if defined(HAS_SOBELROW_SSE2) |
| 2157 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2158 | SobelRow = SobelRow_Any_SSE2; |
| 2159 | if (IS_ALIGNED(width, 16)) { |
| 2160 | SobelRow = SobelRow_SSE2; |
| 2161 | } |
| 2162 | } |
| 2163 | #endif |
| 2164 | #if defined(HAS_SOBELROW_NEON) |
| 2165 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2166 | SobelRow = SobelRow_Any_NEON; |
| 2167 | if (IS_ALIGNED(width, 8)) { |
| 2168 | SobelRow = SobelRow_NEON; |
| 2169 | } |
| 2170 | } |
| 2171 | #endif |
| 2172 | return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb, |
| 2173 | width, height, SobelRow); |
| 2174 | } |
| 2175 |
| 2176 | // Sobel ARGB effect with planar output. |
| 2177 | LIBYUV_API |
| 2178 | int ARGBSobelToPlane(const uint8* src_argb, int src_stride_argb, |
| 2179 | uint8* dst_y, int dst_stride_y, |
| 2180 | int width, int height) { |
| 2181 | void (*SobelToPlaneRow)(const uint8* src_sobelx, const uint8* src_sobely, |
| 2182 | uint8* dst_, int width) = SobelToPlaneRow_C; |
| 2183 | #if defined(HAS_SOBELTOPLANEROW_SSE2) |
| 2184 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2185 | SobelToPlaneRow = SobelToPlaneRow_Any_SSE2; |
| 2186 | if (IS_ALIGNED(width, 16)) { |
| 2187 | SobelToPlaneRow = SobelToPlaneRow_SSE2; |
| 2188 | } |
| 2189 | } |
| 2190 | #endif |
| 2191 | #if defined(HAS_SOBELTOPLANEROW_NEON) |
| 2192 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2193 | SobelToPlaneRow = SobelToPlaneRow_Any_NEON; |
| 2194 | if (IS_ALIGNED(width, 16)) { |
| 2195 | SobelToPlaneRow = SobelToPlaneRow_NEON; |
| 2196 | } |
| 2197 | } |
| 2198 | #endif |
| 2199 | return ARGBSobelize(src_argb, src_stride_argb, dst_y, dst_stride_y, |
| 2200 | width, height, SobelToPlaneRow); |
| 2201 | } |
| 2202 |
| 2203 | // SobelXY ARGB effect. |
| 2204 | // Similar to Sobel, but also stores Sobel X in R and Sobel Y in B. G = Sobel. |
| 2205 | LIBYUV_API |
| 2206 | int ARGBSobelXY(const uint8* src_argb, int src_stride_argb, |
| 2207 | uint8* dst_argb, int dst_stride_argb, |
| 2208 | int width, int height) { |
| 2209 | void (*SobelXYRow)(const uint8* src_sobelx, const uint8* src_sobely, |
| 2210 | uint8* dst_argb, int width) = SobelXYRow_C; |
| 2211 | #if defined(HAS_SOBELXYROW_SSE2) |
| 2212 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2213 | SobelXYRow = SobelXYRow_Any_SSE2; |
| 2214 | if (IS_ALIGNED(width, 16)) { |
| 2215 | SobelXYRow = SobelXYRow_SSE2; |
| 2216 | } |
| 2217 | } |
| 2218 | #endif |
| 2219 | #if defined(HAS_SOBELXYROW_NEON) |
| 2220 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2221 | SobelXYRow = SobelXYRow_Any_NEON; |
| 2222 | if (IS_ALIGNED(width, 8)) { |
| 2223 | SobelXYRow = SobelXYRow_NEON; |
| 2224 | } |
| 2225 | } |
| 2226 | #endif |
| 2227 | return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb, |
| 2228 | width, height, SobelXYRow); |
| 2229 | } |
| 2230 |
| 2231 | // Apply a 4x4 polynomial to each ARGB pixel. |
| 2232 | LIBYUV_API |
| 2233 | int ARGBPolynomial(const uint8* src_argb, int src_stride_argb, |
| 2234 | uint8* dst_argb, int dst_stride_argb, |
| 2235 | const float* poly, |
| 2236 | int width, int height) { |
| 2237 | int y; |
| 2238 | void (*ARGBPolynomialRow)(const uint8* src_argb, |
| 2239 | uint8* dst_argb, const float* poly, |
| 2240 | int width) = ARGBPolynomialRow_C; |
| 2241 | if (!src_argb || !dst_argb || !poly || width <= 0 || height == 0) { |
| 2242 | return -1; |
| 2243 | } |
| 2244 | // Negative height means invert the image. |
| 2245 | if (height < 0) { |
| 2246 | height = -height; |
| 2247 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2248 | src_stride_argb = -src_stride_argb; |
| 2249 | } |
| 2250 | // Coalesce rows. |
| 2251 | if (src_stride_argb == width * 4 && |
| 2252 | dst_stride_argb == width * 4) { |
| 2253 | width *= height; |
| 2254 | height = 1; |
| 2255 | src_stride_argb = dst_stride_argb = 0; |
| 2256 | } |
| 2257 | #if defined(HAS_ARGBPOLYNOMIALROW_SSE2) |
| 2258 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 2)) { |
| 2259 | ARGBPolynomialRow = ARGBPolynomialRow_SSE2; |
| 2260 | } |
| 2261 | #endif |
| 2262 | #if defined(HAS_ARGBPOLYNOMIALROW_AVX2) |
| 2263 | if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasFMA3) && |
| 2264 | IS_ALIGNED(width, 2)) { |
| 2265 | ARGBPolynomialRow = ARGBPolynomialRow_AVX2; |
| 2266 | } |
| 2267 | #endif |
| 2268 |
| 2269 | for (y = 0; y < height; ++y) { |
| 2270 | ARGBPolynomialRow(src_argb, dst_argb, poly, width); |
| 2271 | src_argb += src_stride_argb; |
| 2272 | dst_argb += dst_stride_argb; |
| 2273 | } |
| 2274 | return 0; |
| 2275 | } |
| 2276 |
| 2277 | // Apply a lumacolortable to each ARGB pixel. |
| 2278 | LIBYUV_API |
| 2279 | int ARGBLumaColorTable(const uint8* src_argb, int src_stride_argb, |
| 2280 | uint8* dst_argb, int dst_stride_argb, |
| 2281 | const uint8* luma, |
| 2282 | int width, int height) { |
| 2283 | int y; |
| 2284 | void (*ARGBLumaColorTableRow)(const uint8* src_argb, uint8* dst_argb, |
| 2285 | int width, const uint8* luma, const uint32 lumacoeff) = |
| 2286 | ARGBLumaColorTableRow_C; |
| 2287 | if (!src_argb || !dst_argb || !luma || width <= 0 || height == 0) { |
| 2288 | return -1; |
| 2289 | } |
| 2290 | // Negative height means invert the image. |
| 2291 | if (height < 0) { |
| 2292 | height = -height; |
| 2293 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2294 | src_stride_argb = -src_stride_argb; |
| 2295 | } |
| 2296 | // Coalesce rows. |
| 2297 | if (src_stride_argb == width * 4 && |
| 2298 | dst_stride_argb == width * 4) { |
| 2299 | width *= height; |
| 2300 | height = 1; |
| 2301 | src_stride_argb = dst_stride_argb = 0; |
| 2302 | } |
| 2303 | #if defined(HAS_ARGBLUMACOLORTABLEROW_SSSE3) |
| 2304 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4)) { |
| 2305 | ARGBLumaColorTableRow = ARGBLumaColorTableRow_SSSE3; |
| 2306 | } |
| 2307 | #endif |
| 2308 |
| 2309 | for (y = 0; y < height; ++y) { |
| 2310 | ARGBLumaColorTableRow(src_argb, dst_argb, width, luma, 0x00264b0f); |
| 2311 | src_argb += src_stride_argb; |
| 2312 | dst_argb += dst_stride_argb; |
| 2313 | } |
| 2314 | return 0; |
| 2315 | } |
| 2316 |
| 2317 | // Copy Alpha from one ARGB image to another. |
| 2318 | LIBYUV_API |
| 2319 | int ARGBCopyAlpha(const uint8* src_argb, int src_stride_argb, |
| 2320 | uint8* dst_argb, int dst_stride_argb, |
| 2321 | int width, int height) { |
| 2322 | int y; |
| 2323 | void (*ARGBCopyAlphaRow)(const uint8* src_argb, uint8* dst_argb, int width) = |
| 2324 | ARGBCopyAlphaRow_C; |
| 2325 | if (!src_argb || !dst_argb || width <= 0 || height == 0) { |
| 2326 | return -1; |
| 2327 | } |
| 2328 | // Negative height means invert the image. |
| 2329 | if (height < 0) { |
| 2330 | height = -height; |
| 2331 | src_argb = src_argb + (height - 1) * src_stride_argb; |
| 2332 | src_stride_argb = -src_stride_argb; |
| 2333 | } |
| 2334 | // Coalesce rows. |
| 2335 | if (src_stride_argb == width * 4 && |
| 2336 | dst_stride_argb == width * 4) { |
| 2337 | width *= height; |
| 2338 | height = 1; |
| 2339 | src_stride_argb = dst_stride_argb = 0; |
| 2340 | } |
| 2341 | #if defined(HAS_ARGBCOPYALPHAROW_SSE2) |
| 2342 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2343 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_SSE2; |
| 2344 | if (IS_ALIGNED(width, 8)) { |
| 2345 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_SSE2; |
| 2346 | } |
| 2347 | } |
| 2348 | #endif |
| 2349 | #if defined(HAS_ARGBCOPYALPHAROW_AVX2) |
| 2350 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2351 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_AVX2; |
| 2352 | if (IS_ALIGNED(width, 16)) { |
| 2353 | ARGBCopyAlphaRow = ARGBCopyAlphaRow_AVX2; |
| 2354 | } |
| 2355 | } |
| 2356 | #endif |
| 2357 |
| 2358 | for (y = 0; y < height; ++y) { |
| 2359 | ARGBCopyAlphaRow(src_argb, dst_argb, width); |
| 2360 | src_argb += src_stride_argb; |
| 2361 | dst_argb += dst_stride_argb; |
| 2362 | } |
| 2363 | return 0; |
| 2364 | } |
| 2365 |
| 2366 | // Copy a planar Y channel to the alpha channel of a destination ARGB image. |
| 2367 | LIBYUV_API |
| 2368 | int ARGBCopyYToAlpha(const uint8* src_y, int src_stride_y, |
| 2369 | uint8* dst_argb, int dst_stride_argb, |
| 2370 | int width, int height) { |
| 2371 | int y; |
| 2372 | void (*ARGBCopyYToAlphaRow)(const uint8* src_y, uint8* dst_argb, int width) = |
| 2373 | ARGBCopyYToAlphaRow_C; |
| 2374 | if (!src_y || !dst_argb || width <= 0 || height == 0) { |
| 2375 | return -1; |
| 2376 | } |
| 2377 | // Negative height means invert the image. |
| 2378 | if (height < 0) { |
| 2379 | height = -height; |
| 2380 | src_y = src_y + (height - 1) * src_stride_y; |
| 2381 | src_stride_y = -src_stride_y; |
| 2382 | } |
| 2383 | // Coalesce rows. |
| 2384 | if (src_stride_y == width && |
| 2385 | dst_stride_argb == width * 4) { |
| 2386 | width *= height; |
| 2387 | height = 1; |
| 2388 | src_stride_y = dst_stride_argb = 0; |
| 2389 | } |
| 2390 | #if defined(HAS_ARGBCOPYYTOALPHAROW_SSE2) |
| 2391 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2392 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_SSE2; |
| 2393 | if (IS_ALIGNED(width, 8)) { |
| 2394 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_SSE2; |
| 2395 | } |
| 2396 | } |
| 2397 | #endif |
| 2398 | #if defined(HAS_ARGBCOPYYTOALPHAROW_AVX2) |
| 2399 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2400 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_AVX2; |
| 2401 | if (IS_ALIGNED(width, 16)) { |
| 2402 | ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_AVX2; |
| 2403 | } |
| 2404 | } |
| 2405 | #endif |
| 2406 |
| 2407 | for (y = 0; y < height; ++y) { |
| 2408 | ARGBCopyYToAlphaRow(src_y, dst_argb, width); |
| 2409 | src_y += src_stride_y; |
| 2410 | dst_argb += dst_stride_argb; |
| 2411 | } |
| 2412 | return 0; |
| 2413 | } |
| 2414 |
| 2415 | LIBYUV_API |
| 2416 | int YUY2ToNV12(const uint8* src_yuy2, int src_stride_yuy2, |
| 2417 | uint8* dst_y, int dst_stride_y, |
| 2418 | uint8* dst_uv, int dst_stride_uv, |
| 2419 | int width, int height) { |
| 2420 | int y; |
| 2421 | int halfwidth = (width + 1) >> 1; |
| 2422 | void (*SplitUVRow)(const uint8* src_uv, uint8* dst_u, uint8* dst_v, |
| 2423 | int width) = SplitUVRow_C; |
| 2424 | void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr, |
| 2425 | ptrdiff_t src_stride, int dst_width, |
| 2426 | int source_y_fraction) = InterpolateRow_C; |
| 2427 | if (!src_yuy2 || |
| 2428 | !dst_y || !dst_uv || |
| 2429 | width <= 0 || height == 0) { |
| 2430 | return -1; |
| 2431 | } |
| 2432 | // Negative height means invert the image. |
| 2433 | if (height < 0) { |
| 2434 | height = -height; |
| 2435 | src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2; |
| 2436 | src_stride_yuy2 = -src_stride_yuy2; |
| 2437 | } |
| 2438 | #if defined(HAS_SPLITUVROW_SSE2) |
| 2439 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2440 | SplitUVRow = SplitUVRow_Any_SSE2; |
| 2441 | if (IS_ALIGNED(width, 16)) { |
| 2442 | SplitUVRow = SplitUVRow_SSE2; |
| 2443 | } |
| 2444 | } |
| 2445 | #endif |
| 2446 | #if defined(HAS_SPLITUVROW_AVX2) |
| 2447 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2448 | SplitUVRow = SplitUVRow_Any_AVX2; |
| 2449 | if (IS_ALIGNED(width, 32)) { |
| 2450 | SplitUVRow = SplitUVRow_AVX2; |
| 2451 | } |
| 2452 | } |
| 2453 | #endif |
| 2454 | #if defined(HAS_SPLITUVROW_NEON) |
| 2455 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2456 | SplitUVRow = SplitUVRow_Any_NEON; |
| 2457 | if (IS_ALIGNED(width, 16)) { |
| 2458 | SplitUVRow = SplitUVRow_NEON; |
| 2459 | } |
| 2460 | } |
| 2461 | #endif |
| 2462 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 2463 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2464 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 2465 | if (IS_ALIGNED(width, 16)) { |
| 2466 | InterpolateRow = InterpolateRow_SSSE3; |
| 2467 | } |
| 2468 | } |
| 2469 | #endif |
| 2470 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 2471 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2472 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 2473 | if (IS_ALIGNED(width, 32)) { |
| 2474 | InterpolateRow = InterpolateRow_AVX2; |
| 2475 | } |
| 2476 | } |
| 2477 | #endif |
| 2478 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 2479 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2480 | InterpolateRow = InterpolateRow_Any_NEON; |
| 2481 | if (IS_ALIGNED(width, 16)) { |
| 2482 | InterpolateRow = InterpolateRow_NEON; |
| 2483 | } |
| 2484 | } |
| 2485 | #endif |
| 2486 |
| 2487 | { |
| 2488 | int awidth = halfwidth * 2; |
| 2489 | // 2 rows of uv |
| 2490 | align_buffer_64(rows, awidth * 2); |
| 2491 |
| 2492 | for (y = 0; y < height - 1; y += 2) { |
| 2493 | // Split Y from UV. |
| 2494 | SplitUVRow(src_yuy2, dst_y, rows, awidth); |
| 2495 | SplitUVRow(src_yuy2 + src_stride_yuy2, dst_y + dst_stride_y, |
| 2496 | rows + awidth, awidth); |
| 2497 | InterpolateRow(dst_uv, rows, awidth, awidth, 128); |
| 2498 | src_yuy2 += src_stride_yuy2 * 2; |
| 2499 | dst_y += dst_stride_y * 2; |
| 2500 | dst_uv += dst_stride_uv; |
| 2501 | } |
| 2502 | if (height & 1) { |
| 2503 | // Split Y from UV. |
| 2504 | SplitUVRow(src_yuy2, dst_y, dst_uv, awidth); |
| 2505 | } |
| 2506 | free_aligned_buffer_64(rows); |
| 2507 | } |
| 2508 | return 0; |
| 2509 | } |
| 2510 |
| 2511 | LIBYUV_API |
| 2512 | int UYVYToNV12(const uint8* src_uyvy, int src_stride_uyvy, |
| 2513 | uint8* dst_y, int dst_stride_y, |
| 2514 | uint8* dst_uv, int dst_stride_uv, |
| 2515 | int width, int height) { |
| 2516 | int y; |
| 2517 | int halfwidth = (width + 1) >> 1; |
| 2518 | void (*SplitUVRow)(const uint8* src_uv, uint8* dst_u, uint8* dst_v, |
| 2519 | int width) = SplitUVRow_C; |
| 2520 | void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr, |
| 2521 | ptrdiff_t src_stride, int dst_width, |
| 2522 | int source_y_fraction) = InterpolateRow_C; |
| 2523 | if (!src_uyvy || |
| 2524 | !dst_y || !dst_uv || |
| 2525 | width <= 0 || height == 0) { |
| 2526 | return -1; |
| 2527 | } |
| 2528 | // Negative height means invert the image. |
| 2529 | if (height < 0) { |
| 2530 | height = -height; |
| 2531 | src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy; |
| 2532 | src_stride_uyvy = -src_stride_uyvy; |
| 2533 | } |
| 2534 | #if defined(HAS_SPLITUVROW_SSE2) |
| 2535 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 2536 | SplitUVRow = SplitUVRow_Any_SSE2; |
| 2537 | if (IS_ALIGNED(width, 16)) { |
| 2538 | SplitUVRow = SplitUVRow_SSE2; |
| 2539 | } |
| 2540 | } |
| 2541 | #endif |
| 2542 | #if defined(HAS_SPLITUVROW_AVX2) |
| 2543 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2544 | SplitUVRow = SplitUVRow_Any_AVX2; |
| 2545 | if (IS_ALIGNED(width, 32)) { |
| 2546 | SplitUVRow = SplitUVRow_AVX2; |
| 2547 | } |
| 2548 | } |
| 2549 | #endif |
| 2550 | #if defined(HAS_SPLITUVROW_NEON) |
| 2551 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2552 | SplitUVRow = SplitUVRow_Any_NEON; |
| 2553 | if (IS_ALIGNED(width, 16)) { |
| 2554 | SplitUVRow = SplitUVRow_NEON; |
| 2555 | } |
| 2556 | } |
| 2557 | #endif |
| 2558 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 2559 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 2560 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 2561 | if (IS_ALIGNED(width, 16)) { |
| 2562 | InterpolateRow = InterpolateRow_SSSE3; |
| 2563 | } |
| 2564 | } |
| 2565 | #endif |
| 2566 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 2567 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 2568 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 2569 | if (IS_ALIGNED(width, 32)) { |
| 2570 | InterpolateRow = InterpolateRow_AVX2; |
| 2571 | } |
| 2572 | } |
| 2573 | #endif |
| 2574 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 2575 | if (TestCpuFlag(kCpuHasNEON)) { |
| 2576 | InterpolateRow = InterpolateRow_Any_NEON; |
| 2577 | if (IS_ALIGNED(width, 16)) { |
| 2578 | InterpolateRow = InterpolateRow_NEON; |
| 2579 | } |
| 2580 | } |
| 2581 | #endif |
| 2582 |
| 2583 | { |
| 2584 | int awidth = halfwidth * 2; |
| 2585 | // 2 rows of uv |
| 2586 | align_buffer_64(rows, awidth * 2); |
| 2587 |
| 2588 | for (y = 0; y < height - 1; y += 2) { |
| 2589 | // Split Y from UV. |
| 2590 | SplitUVRow(src_uyvy, rows, dst_y, awidth); |
| 2591 | SplitUVRow(src_uyvy + src_stride_uyvy, rows + awidth, |
| 2592 | dst_y + dst_stride_y, awidth); |
| 2593 | InterpolateRow(dst_uv, rows, awidth, awidth, 128); |
| 2594 | src_uyvy += src_stride_uyvy * 2; |
| 2595 | dst_y += dst_stride_y * 2; |
| 2596 | dst_uv += dst_stride_uv; |
| 2597 | } |
| 2598 | if (height & 1) { |
| 2599 | // Split Y from UV. |
| 2600 | SplitUVRow(src_uyvy, dst_uv, dst_y, awidth); |
| 2601 | } |
| 2602 | free_aligned_buffer_64(rows); |
| 2603 | } |
| 2604 | return 0; |
| 2605 | } |
| 2606 |
| 2607 | #ifdef __cplusplus |
| 2608 | } // extern "C" |
| 2609 | } // namespace libyuv |
| 2610 | #endif |
| 2611 |