| 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/scale.h" |
| 12 |
| 13 | #include <assert.h> |
| 14 | #include <string.h> |
| 15 |
| 16 | #include "libyuv/cpu_id.h" |
| 17 | #include "libyuv/planar_functions.h" // For CopyPlane |
| 18 | #include "libyuv/row.h" |
| 19 | #include "libyuv/scale_row.h" |
| 20 | #include "libyuv/scale_uv.h" // For UVScale |
| 21 |
| 22 | #ifdef __cplusplus |
| 23 | namespace libyuv { |
| 24 | extern "C" { |
| 25 | #endif |
| 26 |
| 27 | static __inline int Abs(int v) { |
| 28 | return v >= 0 ? v : -v; |
| 29 | } |
| 30 |
| 31 | #define SUBSAMPLE(v, a, s) (v < 0) ? (-((-v + a) >> s)) : ((v + a) >> s) |
| 32 | #define CENTERSTART(dx, s) (dx < 0) ? -((-dx >> 1) + s) : ((dx >> 1) + s) |
| 33 |
| 34 | // Scale plane, 1/2 |
| 35 | // This is an optimized version for scaling down a plane to 1/2 of |
| 36 | // its original size. |
| 37 |
| 38 | static void ScalePlaneDown2(int src_width, |
| 39 | int src_height, |
| 40 | int dst_width, |
| 41 | int dst_height, |
| 42 | int src_stride, |
| 43 | int dst_stride, |
| 44 | const uint8_t* src_ptr, |
| 45 | uint8_t* dst_ptr, |
| 46 | enum FilterMode filtering) { |
| 47 | int y; |
| 48 | void (*ScaleRowDown2)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 49 | uint8_t* dst_ptr, int dst_width) = |
| 50 | filtering == kFilterNone |
| 51 | ? ScaleRowDown2_C |
| 52 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_C |
| 53 | : ScaleRowDown2Box_C); |
| 54 | int row_stride = src_stride * 2; |
| 55 | (void)src_width; |
| 56 | (void)src_height; |
| 57 | if (!filtering) { |
| 58 | src_ptr += src_stride; // Point to odd rows. |
| 59 | src_stride = 0; |
| 60 | } |
| 61 |
| 62 | #if defined(HAS_SCALEROWDOWN2_NEON) |
| 63 | if (TestCpuFlag(kCpuHasNEON)) { |
| 64 | ScaleRowDown2 = |
| 65 | filtering == kFilterNone |
| 66 | ? ScaleRowDown2_Any_NEON |
| 67 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_NEON |
| 68 | : ScaleRowDown2Box_Any_NEON); |
| 69 | if (IS_ALIGNED(dst_width, 16)) { |
| 70 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_NEON |
| 71 | : (filtering == kFilterLinear |
| 72 | ? ScaleRowDown2Linear_NEON |
| 73 | : ScaleRowDown2Box_NEON); |
| 74 | } |
| 75 | } |
| 76 | #endif |
| 77 | #if defined(HAS_SCALEROWDOWN2_SSSE3) |
| 78 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 79 | ScaleRowDown2 = |
| 80 | filtering == kFilterNone |
| 81 | ? ScaleRowDown2_Any_SSSE3 |
| 82 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_SSSE3 |
| 83 | : ScaleRowDown2Box_Any_SSSE3); |
| 84 | if (IS_ALIGNED(dst_width, 16)) { |
| 85 | ScaleRowDown2 = |
| 86 | filtering == kFilterNone |
| 87 | ? ScaleRowDown2_SSSE3 |
| 88 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_SSSE3 |
| 89 | : ScaleRowDown2Box_SSSE3); |
| 90 | } |
| 91 | } |
| 92 | #endif |
| 93 | #if defined(HAS_SCALEROWDOWN2_AVX2) |
| 94 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 95 | ScaleRowDown2 = |
| 96 | filtering == kFilterNone |
| 97 | ? ScaleRowDown2_Any_AVX2 |
| 98 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_AVX2 |
| 99 | : ScaleRowDown2Box_Any_AVX2); |
| 100 | if (IS_ALIGNED(dst_width, 32)) { |
| 101 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_AVX2 |
| 102 | : (filtering == kFilterLinear |
| 103 | ? ScaleRowDown2Linear_AVX2 |
| 104 | : ScaleRowDown2Box_AVX2); |
| 105 | } |
| 106 | } |
| 107 | #endif |
| 108 | #if defined(HAS_SCALEROWDOWN2_MSA) |
| 109 | if (TestCpuFlag(kCpuHasMSA)) { |
| 110 | ScaleRowDown2 = |
| 111 | filtering == kFilterNone |
| 112 | ? ScaleRowDown2_Any_MSA |
| 113 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_MSA |
| 114 | : ScaleRowDown2Box_Any_MSA); |
| 115 | if (IS_ALIGNED(dst_width, 32)) { |
| 116 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_MSA |
| 117 | : (filtering == kFilterLinear |
| 118 | ? ScaleRowDown2Linear_MSA |
| 119 | : ScaleRowDown2Box_MSA); |
| 120 | } |
| 121 | } |
| 122 | #endif |
| 123 | #if defined(HAS_SCALEROWDOWN2_LSX) |
| 124 | if (TestCpuFlag(kCpuHasLSX)) { |
| 125 | ScaleRowDown2 = |
| 126 | filtering == kFilterNone |
| 127 | ? ScaleRowDown2_Any_LSX |
| 128 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_LSX |
| 129 | : ScaleRowDown2Box_Any_LSX); |
| 130 | if (IS_ALIGNED(dst_width, 32)) { |
| 131 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_LSX |
| 132 | : (filtering == kFilterLinear |
| 133 | ? ScaleRowDown2Linear_LSX |
| 134 | : ScaleRowDown2Box_LSX); |
| 135 | } |
| 136 | } |
| 137 | #endif |
| 138 |
| 139 | if (filtering == kFilterLinear) { |
| 140 | src_stride = 0; |
| 141 | } |
| 142 | // TODO(fbarchard): Loop through source height to allow odd height. |
| 143 | for (y = 0; y < dst_height; ++y) { |
| 144 | ScaleRowDown2(src_ptr, src_stride, dst_ptr, dst_width); |
| 145 | src_ptr += row_stride; |
| 146 | dst_ptr += dst_stride; |
| 147 | } |
| 148 | } |
| 149 |
| 150 | static void ScalePlaneDown2_16(int src_width, |
| 151 | int src_height, |
| 152 | int dst_width, |
| 153 | int dst_height, |
| 154 | int src_stride, |
| 155 | int dst_stride, |
| 156 | const uint16_t* src_ptr, |
| 157 | uint16_t* dst_ptr, |
| 158 | enum FilterMode filtering) { |
| 159 | int y; |
| 160 | void (*ScaleRowDown2)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 161 | uint16_t* dst_ptr, int dst_width) = |
| 162 | filtering == kFilterNone |
| 163 | ? ScaleRowDown2_16_C |
| 164 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_16_C |
| 165 | : ScaleRowDown2Box_16_C); |
| 166 | int row_stride = src_stride * 2; |
| 167 | (void)src_width; |
| 168 | (void)src_height; |
| 169 | if (!filtering) { |
| 170 | src_ptr += src_stride; // Point to odd rows. |
| 171 | src_stride = 0; |
| 172 | } |
| 173 |
| 174 | #if defined(HAS_SCALEROWDOWN2_16_NEON) |
| 175 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 16)) { |
| 176 | ScaleRowDown2 = |
| 177 | filtering ? ScaleRowDown2Box_16_NEON : ScaleRowDown2_16_NEON; |
| 178 | } |
| 179 | #endif |
| 180 | #if defined(HAS_SCALEROWDOWN2_16_SSE2) |
| 181 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 16)) { |
| 182 | ScaleRowDown2 = |
| 183 | filtering == kFilterNone |
| 184 | ? ScaleRowDown2_16_SSE2 |
| 185 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_16_SSE2 |
| 186 | : ScaleRowDown2Box_16_SSE2); |
| 187 | } |
| 188 | #endif |
| 189 |
| 190 | if (filtering == kFilterLinear) { |
| 191 | src_stride = 0; |
| 192 | } |
| 193 | // TODO(fbarchard): Loop through source height to allow odd height. |
| 194 | for (y = 0; y < dst_height; ++y) { |
| 195 | ScaleRowDown2(src_ptr, src_stride, dst_ptr, dst_width); |
| 196 | src_ptr += row_stride; |
| 197 | dst_ptr += dst_stride; |
| 198 | } |
| 199 | } |
| 200 |
| 201 | void ScalePlaneDown2_16To8(int src_width, |
| 202 | int src_height, |
| 203 | int dst_width, |
| 204 | int dst_height, |
| 205 | int src_stride, |
| 206 | int dst_stride, |
| 207 | const uint16_t* src_ptr, |
| 208 | uint8_t* dst_ptr, |
| 209 | int scale, |
| 210 | enum FilterMode filtering) { |
| 211 | int y; |
| 212 | void (*ScaleRowDown2)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 213 | uint8_t* dst_ptr, int dst_width, int scale) = |
| 214 | (src_width & 1) |
| 215 | ? (filtering == kFilterNone |
| 216 | ? ScaleRowDown2_16To8_Odd_C |
| 217 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_16To8_Odd_C |
| 218 | : ScaleRowDown2Box_16To8_Odd_C)) |
| 219 | : (filtering == kFilterNone |
| 220 | ? ScaleRowDown2_16To8_C |
| 221 | : (filtering == kFilterLinear ? ScaleRowDown2Linear_16To8_C |
| 222 | : ScaleRowDown2Box_16To8_C)); |
| 223 | int row_stride = src_stride * 2; |
| 224 | (void)dst_height; |
| 225 | if (!filtering) { |
| 226 | src_ptr += src_stride; // Point to odd rows. |
| 227 | src_stride = 0; |
| 228 | } |
| 229 |
| 230 | if (filtering == kFilterLinear) { |
| 231 | src_stride = 0; |
| 232 | } |
| 233 | for (y = 0; y < src_height / 2; ++y) { |
| 234 | ScaleRowDown2(src_ptr, src_stride, dst_ptr, dst_width, scale); |
| 235 | src_ptr += row_stride; |
| 236 | dst_ptr += dst_stride; |
| 237 | } |
| 238 | if (src_height & 1) { |
| 239 | if (!filtering) { |
| 240 | src_ptr -= src_stride; // Point to last row. |
| 241 | } |
| 242 | ScaleRowDown2(src_ptr, 0, dst_ptr, dst_width, scale); |
| 243 | } |
| 244 | } |
| 245 |
| 246 | // Scale plane, 1/4 |
| 247 | // This is an optimized version for scaling down a plane to 1/4 of |
| 248 | // its original size. |
| 249 |
| 250 | static void ScalePlaneDown4(int src_width, |
| 251 | int src_height, |
| 252 | int dst_width, |
| 253 | int dst_height, |
| 254 | int src_stride, |
| 255 | int dst_stride, |
| 256 | const uint8_t* src_ptr, |
| 257 | uint8_t* dst_ptr, |
| 258 | enum FilterMode filtering) { |
| 259 | int y; |
| 260 | void (*ScaleRowDown4)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 261 | uint8_t* dst_ptr, int dst_width) = |
| 262 | filtering ? ScaleRowDown4Box_C : ScaleRowDown4_C; |
| 263 | int row_stride = src_stride * 4; |
| 264 | (void)src_width; |
| 265 | (void)src_height; |
| 266 | if (!filtering) { |
| 267 | src_ptr += src_stride * 2; // Point to row 2. |
| 268 | src_stride = 0; |
| 269 | } |
| 270 | #if defined(HAS_SCALEROWDOWN4_NEON) |
| 271 | if (TestCpuFlag(kCpuHasNEON)) { |
| 272 | ScaleRowDown4 = |
| 273 | filtering ? ScaleRowDown4Box_Any_NEON : ScaleRowDown4_Any_NEON; |
| 274 | if (IS_ALIGNED(dst_width, 8)) { |
| 275 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_NEON : ScaleRowDown4_NEON; |
| 276 | } |
| 277 | } |
| 278 | #endif |
| 279 | #if defined(HAS_SCALEROWDOWN4_SSSE3) |
| 280 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 281 | ScaleRowDown4 = |
| 282 | filtering ? ScaleRowDown4Box_Any_SSSE3 : ScaleRowDown4_Any_SSSE3; |
| 283 | if (IS_ALIGNED(dst_width, 8)) { |
| 284 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_SSSE3 : ScaleRowDown4_SSSE3; |
| 285 | } |
| 286 | } |
| 287 | #endif |
| 288 | #if defined(HAS_SCALEROWDOWN4_AVX2) |
| 289 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 290 | ScaleRowDown4 = |
| 291 | filtering ? ScaleRowDown4Box_Any_AVX2 : ScaleRowDown4_Any_AVX2; |
| 292 | if (IS_ALIGNED(dst_width, 16)) { |
| 293 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_AVX2 : ScaleRowDown4_AVX2; |
| 294 | } |
| 295 | } |
| 296 | #endif |
| 297 | #if defined(HAS_SCALEROWDOWN4_MSA) |
| 298 | if (TestCpuFlag(kCpuHasMSA)) { |
| 299 | ScaleRowDown4 = |
| 300 | filtering ? ScaleRowDown4Box_Any_MSA : ScaleRowDown4_Any_MSA; |
| 301 | if (IS_ALIGNED(dst_width, 16)) { |
| 302 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_MSA : ScaleRowDown4_MSA; |
| 303 | } |
| 304 | } |
| 305 | #endif |
| 306 | #if defined(HAS_SCALEROWDOWN4_LSX) |
| 307 | if (TestCpuFlag(kCpuHasLSX)) { |
| 308 | ScaleRowDown4 = |
| 309 | filtering ? ScaleRowDown4Box_Any_LSX : ScaleRowDown4_Any_LSX; |
| 310 | if (IS_ALIGNED(dst_width, 16)) { |
| 311 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_LSX : ScaleRowDown4_LSX; |
| 312 | } |
| 313 | } |
| 314 | #endif |
| 315 |
| 316 | if (filtering == kFilterLinear) { |
| 317 | src_stride = 0; |
| 318 | } |
| 319 | for (y = 0; y < dst_height; ++y) { |
| 320 | ScaleRowDown4(src_ptr, src_stride, dst_ptr, dst_width); |
| 321 | src_ptr += row_stride; |
| 322 | dst_ptr += dst_stride; |
| 323 | } |
| 324 | } |
| 325 |
| 326 | static void ScalePlaneDown4_16(int src_width, |
| 327 | int src_height, |
| 328 | int dst_width, |
| 329 | int dst_height, |
| 330 | int src_stride, |
| 331 | int dst_stride, |
| 332 | const uint16_t* src_ptr, |
| 333 | uint16_t* dst_ptr, |
| 334 | enum FilterMode filtering) { |
| 335 | int y; |
| 336 | void (*ScaleRowDown4)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 337 | uint16_t* dst_ptr, int dst_width) = |
| 338 | filtering ? ScaleRowDown4Box_16_C : ScaleRowDown4_16_C; |
| 339 | int row_stride = src_stride * 4; |
| 340 | (void)src_width; |
| 341 | (void)src_height; |
| 342 | if (!filtering) { |
| 343 | src_ptr += src_stride * 2; // Point to row 2. |
| 344 | src_stride = 0; |
| 345 | } |
| 346 | #if defined(HAS_SCALEROWDOWN4_16_NEON) |
| 347 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 8)) { |
| 348 | ScaleRowDown4 = |
| 349 | filtering ? ScaleRowDown4Box_16_NEON : ScaleRowDown4_16_NEON; |
| 350 | } |
| 351 | #endif |
| 352 | #if defined(HAS_SCALEROWDOWN4_16_SSE2) |
| 353 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 354 | ScaleRowDown4 = |
| 355 | filtering ? ScaleRowDown4Box_16_SSE2 : ScaleRowDown4_16_SSE2; |
| 356 | } |
| 357 | #endif |
| 358 |
| 359 | if (filtering == kFilterLinear) { |
| 360 | src_stride = 0; |
| 361 | } |
| 362 | for (y = 0; y < dst_height; ++y) { |
| 363 | ScaleRowDown4(src_ptr, src_stride, dst_ptr, dst_width); |
| 364 | src_ptr += row_stride; |
| 365 | dst_ptr += dst_stride; |
| 366 | } |
| 367 | } |
| 368 |
| 369 | // Scale plane down, 3/4 |
| 370 | static void ScalePlaneDown34(int src_width, |
| 371 | int src_height, |
| 372 | int dst_width, |
| 373 | int dst_height, |
| 374 | int src_stride, |
| 375 | int dst_stride, |
| 376 | const uint8_t* src_ptr, |
| 377 | uint8_t* dst_ptr, |
| 378 | enum FilterMode filtering) { |
| 379 | int y; |
| 380 | void (*ScaleRowDown34_0)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 381 | uint8_t* dst_ptr, int dst_width); |
| 382 | void (*ScaleRowDown34_1)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 383 | uint8_t* dst_ptr, int dst_width); |
| 384 | const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; |
| 385 | (void)src_width; |
| 386 | (void)src_height; |
| 387 | assert(dst_width % 3 == 0); |
| 388 | if (!filtering) { |
| 389 | ScaleRowDown34_0 = ScaleRowDown34_C; |
| 390 | ScaleRowDown34_1 = ScaleRowDown34_C; |
| 391 | } else { |
| 392 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_C; |
| 393 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_C; |
| 394 | } |
| 395 | #if defined(HAS_SCALEROWDOWN34_NEON) |
| 396 | if (TestCpuFlag(kCpuHasNEON)) { |
| 397 | if (!filtering) { |
| 398 | ScaleRowDown34_0 = ScaleRowDown34_Any_NEON; |
| 399 | ScaleRowDown34_1 = ScaleRowDown34_Any_NEON; |
| 400 | } else { |
| 401 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_Any_NEON; |
| 402 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_Any_NEON; |
| 403 | } |
| 404 | if (dst_width % 24 == 0) { |
| 405 | if (!filtering) { |
| 406 | ScaleRowDown34_0 = ScaleRowDown34_NEON; |
| 407 | ScaleRowDown34_1 = ScaleRowDown34_NEON; |
| 408 | } else { |
| 409 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_NEON; |
| 410 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_NEON; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | #endif |
| 415 | #if defined(HAS_SCALEROWDOWN34_MSA) |
| 416 | if (TestCpuFlag(kCpuHasMSA)) { |
| 417 | if (!filtering) { |
| 418 | ScaleRowDown34_0 = ScaleRowDown34_Any_MSA; |
| 419 | ScaleRowDown34_1 = ScaleRowDown34_Any_MSA; |
| 420 | } else { |
| 421 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_Any_MSA; |
| 422 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_Any_MSA; |
| 423 | } |
| 424 | if (dst_width % 48 == 0) { |
| 425 | if (!filtering) { |
| 426 | ScaleRowDown34_0 = ScaleRowDown34_MSA; |
| 427 | ScaleRowDown34_1 = ScaleRowDown34_MSA; |
| 428 | } else { |
| 429 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_MSA; |
| 430 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_MSA; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | #endif |
| 435 | #if defined(HAS_SCALEROWDOWN34_LSX) |
| 436 | if (TestCpuFlag(kCpuHasLSX)) { |
| 437 | if (!filtering) { |
| 438 | ScaleRowDown34_0 = ScaleRowDown34_Any_LSX; |
| 439 | ScaleRowDown34_1 = ScaleRowDown34_Any_LSX; |
| 440 | } else { |
| 441 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_Any_LSX; |
| 442 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_Any_LSX; |
| 443 | } |
| 444 | if (dst_width % 48 == 0) { |
| 445 | if (!filtering) { |
| 446 | ScaleRowDown34_0 = ScaleRowDown34_LSX; |
| 447 | ScaleRowDown34_1 = ScaleRowDown34_LSX; |
| 448 | } else { |
| 449 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_LSX; |
| 450 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_LSX; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | #endif |
| 455 | #if defined(HAS_SCALEROWDOWN34_SSSE3) |
| 456 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 457 | if (!filtering) { |
| 458 | ScaleRowDown34_0 = ScaleRowDown34_Any_SSSE3; |
| 459 | ScaleRowDown34_1 = ScaleRowDown34_Any_SSSE3; |
| 460 | } else { |
| 461 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_Any_SSSE3; |
| 462 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_Any_SSSE3; |
| 463 | } |
| 464 | if (dst_width % 24 == 0) { |
| 465 | if (!filtering) { |
| 466 | ScaleRowDown34_0 = ScaleRowDown34_SSSE3; |
| 467 | ScaleRowDown34_1 = ScaleRowDown34_SSSE3; |
| 468 | } else { |
| 469 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_SSSE3; |
| 470 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_SSSE3; |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | #endif |
| 475 |
| 476 | for (y = 0; y < dst_height - 2; y += 3) { |
| 477 | ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); |
| 478 | src_ptr += src_stride; |
| 479 | dst_ptr += dst_stride; |
| 480 | ScaleRowDown34_1(src_ptr, filter_stride, dst_ptr, dst_width); |
| 481 | src_ptr += src_stride; |
| 482 | dst_ptr += dst_stride; |
| 483 | ScaleRowDown34_0(src_ptr + src_stride, -filter_stride, dst_ptr, dst_width); |
| 484 | src_ptr += src_stride * 2; |
| 485 | dst_ptr += dst_stride; |
| 486 | } |
| 487 |
| 488 | // Remainder 1 or 2 rows with last row vertically unfiltered |
| 489 | if ((dst_height % 3) == 2) { |
| 490 | ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); |
| 491 | src_ptr += src_stride; |
| 492 | dst_ptr += dst_stride; |
| 493 | ScaleRowDown34_1(src_ptr, 0, dst_ptr, dst_width); |
| 494 | } else if ((dst_height % 3) == 1) { |
| 495 | ScaleRowDown34_0(src_ptr, 0, dst_ptr, dst_width); |
| 496 | } |
| 497 | } |
| 498 |
| 499 | static void ScalePlaneDown34_16(int src_width, |
| 500 | int src_height, |
| 501 | int dst_width, |
| 502 | int dst_height, |
| 503 | int src_stride, |
| 504 | int dst_stride, |
| 505 | const uint16_t* src_ptr, |
| 506 | uint16_t* dst_ptr, |
| 507 | enum FilterMode filtering) { |
| 508 | int y; |
| 509 | void (*ScaleRowDown34_0)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 510 | uint16_t* dst_ptr, int dst_width); |
| 511 | void (*ScaleRowDown34_1)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 512 | uint16_t* dst_ptr, int dst_width); |
| 513 | const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; |
| 514 | (void)src_width; |
| 515 | (void)src_height; |
| 516 | assert(dst_width % 3 == 0); |
| 517 | if (!filtering) { |
| 518 | ScaleRowDown34_0 = ScaleRowDown34_16_C; |
| 519 | ScaleRowDown34_1 = ScaleRowDown34_16_C; |
| 520 | } else { |
| 521 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_C; |
| 522 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_C; |
| 523 | } |
| 524 | #if defined(HAS_SCALEROWDOWN34_16_NEON) |
| 525 | if (TestCpuFlag(kCpuHasNEON) && (dst_width % 24 == 0)) { |
| 526 | if (!filtering) { |
| 527 | ScaleRowDown34_0 = ScaleRowDown34_16_NEON; |
| 528 | ScaleRowDown34_1 = ScaleRowDown34_16_NEON; |
| 529 | } else { |
| 530 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_NEON; |
| 531 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_NEON; |
| 532 | } |
| 533 | } |
| 534 | #endif |
| 535 | #if defined(HAS_SCALEROWDOWN34_16_SSSE3) |
| 536 | if (TestCpuFlag(kCpuHasSSSE3) && (dst_width % 24 == 0)) { |
| 537 | if (!filtering) { |
| 538 | ScaleRowDown34_0 = ScaleRowDown34_16_SSSE3; |
| 539 | ScaleRowDown34_1 = ScaleRowDown34_16_SSSE3; |
| 540 | } else { |
| 541 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_SSSE3; |
| 542 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_SSSE3; |
| 543 | } |
| 544 | } |
| 545 | #endif |
| 546 |
| 547 | for (y = 0; y < dst_height - 2; y += 3) { |
| 548 | ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); |
| 549 | src_ptr += src_stride; |
| 550 | dst_ptr += dst_stride; |
| 551 | ScaleRowDown34_1(src_ptr, filter_stride, dst_ptr, dst_width); |
| 552 | src_ptr += src_stride; |
| 553 | dst_ptr += dst_stride; |
| 554 | ScaleRowDown34_0(src_ptr + src_stride, -filter_stride, dst_ptr, dst_width); |
| 555 | src_ptr += src_stride * 2; |
| 556 | dst_ptr += dst_stride; |
| 557 | } |
| 558 |
| 559 | // Remainder 1 or 2 rows with last row vertically unfiltered |
| 560 | if ((dst_height % 3) == 2) { |
| 561 | ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); |
| 562 | src_ptr += src_stride; |
| 563 | dst_ptr += dst_stride; |
| 564 | ScaleRowDown34_1(src_ptr, 0, dst_ptr, dst_width); |
| 565 | } else if ((dst_height % 3) == 1) { |
| 566 | ScaleRowDown34_0(src_ptr, 0, dst_ptr, dst_width); |
| 567 | } |
| 568 | } |
| 569 |
| 570 | // Scale plane, 3/8 |
| 571 | // This is an optimized version for scaling down a plane to 3/8 |
| 572 | // of its original size. |
| 573 | // |
| 574 | // Uses box filter arranges like this |
| 575 | // aaabbbcc -> abc |
| 576 | // aaabbbcc def |
| 577 | // aaabbbcc ghi |
| 578 | // dddeeeff |
| 579 | // dddeeeff |
| 580 | // dddeeeff |
| 581 | // ggghhhii |
| 582 | // ggghhhii |
| 583 | // Boxes are 3x3, 2x3, 3x2 and 2x2 |
| 584 |
| 585 | static void ScalePlaneDown38(int src_width, |
| 586 | int src_height, |
| 587 | int dst_width, |
| 588 | int dst_height, |
| 589 | int src_stride, |
| 590 | int dst_stride, |
| 591 | const uint8_t* src_ptr, |
| 592 | uint8_t* dst_ptr, |
| 593 | enum FilterMode filtering) { |
| 594 | int y; |
| 595 | void (*ScaleRowDown38_3)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 596 | uint8_t* dst_ptr, int dst_width); |
| 597 | void (*ScaleRowDown38_2)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 598 | uint8_t* dst_ptr, int dst_width); |
| 599 | const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; |
| 600 | assert(dst_width % 3 == 0); |
| 601 | (void)src_width; |
| 602 | (void)src_height; |
| 603 | if (!filtering) { |
| 604 | ScaleRowDown38_3 = ScaleRowDown38_C; |
| 605 | ScaleRowDown38_2 = ScaleRowDown38_C; |
| 606 | } else { |
| 607 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_C; |
| 608 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_C; |
| 609 | } |
| 610 |
| 611 | #if defined(HAS_SCALEROWDOWN38_NEON) |
| 612 | if (TestCpuFlag(kCpuHasNEON)) { |
| 613 | if (!filtering) { |
| 614 | ScaleRowDown38_3 = ScaleRowDown38_Any_NEON; |
| 615 | ScaleRowDown38_2 = ScaleRowDown38_Any_NEON; |
| 616 | } else { |
| 617 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_Any_NEON; |
| 618 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_Any_NEON; |
| 619 | } |
| 620 | if (dst_width % 12 == 0) { |
| 621 | if (!filtering) { |
| 622 | ScaleRowDown38_3 = ScaleRowDown38_NEON; |
| 623 | ScaleRowDown38_2 = ScaleRowDown38_NEON; |
| 624 | } else { |
| 625 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_NEON; |
| 626 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_NEON; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | #endif |
| 631 | #if defined(HAS_SCALEROWDOWN38_SSSE3) |
| 632 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 633 | if (!filtering) { |
| 634 | ScaleRowDown38_3 = ScaleRowDown38_Any_SSSE3; |
| 635 | ScaleRowDown38_2 = ScaleRowDown38_Any_SSSE3; |
| 636 | } else { |
| 637 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_Any_SSSE3; |
| 638 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_Any_SSSE3; |
| 639 | } |
| 640 | if (dst_width % 12 == 0 && !filtering) { |
| 641 | ScaleRowDown38_3 = ScaleRowDown38_SSSE3; |
| 642 | ScaleRowDown38_2 = ScaleRowDown38_SSSE3; |
| 643 | } |
| 644 | if (dst_width % 6 == 0 && filtering) { |
| 645 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_SSSE3; |
| 646 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_SSSE3; |
| 647 | } |
| 648 | } |
| 649 | #endif |
| 650 | #if defined(HAS_SCALEROWDOWN38_MSA) |
| 651 | if (TestCpuFlag(kCpuHasMSA)) { |
| 652 | if (!filtering) { |
| 653 | ScaleRowDown38_3 = ScaleRowDown38_Any_MSA; |
| 654 | ScaleRowDown38_2 = ScaleRowDown38_Any_MSA; |
| 655 | } else { |
| 656 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_Any_MSA; |
| 657 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_Any_MSA; |
| 658 | } |
| 659 | if (dst_width % 12 == 0) { |
| 660 | if (!filtering) { |
| 661 | ScaleRowDown38_3 = ScaleRowDown38_MSA; |
| 662 | ScaleRowDown38_2 = ScaleRowDown38_MSA; |
| 663 | } else { |
| 664 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_MSA; |
| 665 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_MSA; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | #endif |
| 670 | #if defined(HAS_SCALEROWDOWN38_LSX) |
| 671 | if (TestCpuFlag(kCpuHasLSX)) { |
| 672 | if (!filtering) { |
| 673 | ScaleRowDown38_3 = ScaleRowDown38_Any_LSX; |
| 674 | ScaleRowDown38_2 = ScaleRowDown38_Any_LSX; |
| 675 | } else { |
| 676 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_Any_LSX; |
| 677 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_Any_LSX; |
| 678 | } |
| 679 | if (dst_width % 12 == 0) { |
| 680 | if (!filtering) { |
| 681 | ScaleRowDown38_3 = ScaleRowDown38_LSX; |
| 682 | ScaleRowDown38_2 = ScaleRowDown38_LSX; |
| 683 | } else { |
| 684 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_LSX; |
| 685 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_LSX; |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | #endif |
| 690 |
| 691 | for (y = 0; y < dst_height - 2; y += 3) { |
| 692 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 693 | src_ptr += src_stride * 3; |
| 694 | dst_ptr += dst_stride; |
| 695 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 696 | src_ptr += src_stride * 3; |
| 697 | dst_ptr += dst_stride; |
| 698 | ScaleRowDown38_2(src_ptr, filter_stride, dst_ptr, dst_width); |
| 699 | src_ptr += src_stride * 2; |
| 700 | dst_ptr += dst_stride; |
| 701 | } |
| 702 |
| 703 | // Remainder 1 or 2 rows with last row vertically unfiltered |
| 704 | if ((dst_height % 3) == 2) { |
| 705 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 706 | src_ptr += src_stride * 3; |
| 707 | dst_ptr += dst_stride; |
| 708 | ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); |
| 709 | } else if ((dst_height % 3) == 1) { |
| 710 | ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); |
| 711 | } |
| 712 | } |
| 713 |
| 714 | static void ScalePlaneDown38_16(int src_width, |
| 715 | int src_height, |
| 716 | int dst_width, |
| 717 | int dst_height, |
| 718 | int src_stride, |
| 719 | int dst_stride, |
| 720 | const uint16_t* src_ptr, |
| 721 | uint16_t* dst_ptr, |
| 722 | enum FilterMode filtering) { |
| 723 | int y; |
| 724 | void (*ScaleRowDown38_3)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 725 | uint16_t* dst_ptr, int dst_width); |
| 726 | void (*ScaleRowDown38_2)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 727 | uint16_t* dst_ptr, int dst_width); |
| 728 | const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; |
| 729 | (void)src_width; |
| 730 | (void)src_height; |
| 731 | assert(dst_width % 3 == 0); |
| 732 | if (!filtering) { |
| 733 | ScaleRowDown38_3 = ScaleRowDown38_16_C; |
| 734 | ScaleRowDown38_2 = ScaleRowDown38_16_C; |
| 735 | } else { |
| 736 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_C; |
| 737 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_C; |
| 738 | } |
| 739 | #if defined(HAS_SCALEROWDOWN38_16_NEON) |
| 740 | if (TestCpuFlag(kCpuHasNEON) && (dst_width % 12 == 0)) { |
| 741 | if (!filtering) { |
| 742 | ScaleRowDown38_3 = ScaleRowDown38_16_NEON; |
| 743 | ScaleRowDown38_2 = ScaleRowDown38_16_NEON; |
| 744 | } else { |
| 745 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_NEON; |
| 746 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_NEON; |
| 747 | } |
| 748 | } |
| 749 | #endif |
| 750 | #if defined(HAS_SCALEROWDOWN38_16_SSSE3) |
| 751 | if (TestCpuFlag(kCpuHasSSSE3) && (dst_width % 24 == 0)) { |
| 752 | if (!filtering) { |
| 753 | ScaleRowDown38_3 = ScaleRowDown38_16_SSSE3; |
| 754 | ScaleRowDown38_2 = ScaleRowDown38_16_SSSE3; |
| 755 | } else { |
| 756 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_SSSE3; |
| 757 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_SSSE3; |
| 758 | } |
| 759 | } |
| 760 | #endif |
| 761 |
| 762 | for (y = 0; y < dst_height - 2; y += 3) { |
| 763 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 764 | src_ptr += src_stride * 3; |
| 765 | dst_ptr += dst_stride; |
| 766 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 767 | src_ptr += src_stride * 3; |
| 768 | dst_ptr += dst_stride; |
| 769 | ScaleRowDown38_2(src_ptr, filter_stride, dst_ptr, dst_width); |
| 770 | src_ptr += src_stride * 2; |
| 771 | dst_ptr += dst_stride; |
| 772 | } |
| 773 |
| 774 | // Remainder 1 or 2 rows with last row vertically unfiltered |
| 775 | if ((dst_height % 3) == 2) { |
| 776 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 777 | src_ptr += src_stride * 3; |
| 778 | dst_ptr += dst_stride; |
| 779 | ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); |
| 780 | } else if ((dst_height % 3) == 1) { |
| 781 | ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); |
| 782 | } |
| 783 | } |
| 784 |
| 785 | #define MIN1(x) ((x) < 1 ? 1 : (x)) |
| 786 |
| 787 | static __inline uint32_t SumPixels(int iboxwidth, const uint16_t* src_ptr) { |
| 788 | uint32_t sum = 0u; |
| 789 | int x; |
| 790 | assert(iboxwidth > 0); |
| 791 | for (x = 0; x < iboxwidth; ++x) { |
| 792 | sum += src_ptr[x]; |
| 793 | } |
| 794 | return sum; |
| 795 | } |
| 796 |
| 797 | static __inline uint32_t SumPixels_16(int iboxwidth, const uint32_t* src_ptr) { |
| 798 | uint32_t sum = 0u; |
| 799 | int x; |
| 800 | assert(iboxwidth > 0); |
| 801 | for (x = 0; x < iboxwidth; ++x) { |
| 802 | sum += src_ptr[x]; |
| 803 | } |
| 804 | return sum; |
| 805 | } |
| 806 |
| 807 | static void ScaleAddCols2_C(int dst_width, |
| 808 | int boxheight, |
| 809 | int x, |
| 810 | int dx, |
| 811 | const uint16_t* src_ptr, |
| 812 | uint8_t* dst_ptr) { |
| 813 | int i; |
| 814 | int scaletbl[2]; |
| 815 | int minboxwidth = dx >> 16; |
| 816 | int boxwidth; |
| 817 | scaletbl[0] = 65536 / (MIN1(minboxwidth) * boxheight); |
| 818 | scaletbl[1] = 65536 / (MIN1(minboxwidth + 1) * boxheight); |
| 819 | for (i = 0; i < dst_width; ++i) { |
| 820 | int ix = x >> 16; |
| 821 | x += dx; |
| 822 | boxwidth = MIN1((x >> 16) - ix); |
| 823 | *dst_ptr++ = (uint8_t)(SumPixels(boxwidth, src_ptr + ix) * |
| 824 | scaletbl[boxwidth - minboxwidth] >> |
| 825 | 16); |
| 826 | } |
| 827 | } |
| 828 |
| 829 | static void ScaleAddCols2_16_C(int dst_width, |
| 830 | int boxheight, |
| 831 | int x, |
| 832 | int dx, |
| 833 | const uint32_t* src_ptr, |
| 834 | uint16_t* dst_ptr) { |
| 835 | int i; |
| 836 | int scaletbl[2]; |
| 837 | int minboxwidth = dx >> 16; |
| 838 | int boxwidth; |
| 839 | scaletbl[0] = 65536 / (MIN1(minboxwidth) * boxheight); |
| 840 | scaletbl[1] = 65536 / (MIN1(minboxwidth + 1) * boxheight); |
| 841 | for (i = 0; i < dst_width; ++i) { |
| 842 | int ix = x >> 16; |
| 843 | x += dx; |
| 844 | boxwidth = MIN1((x >> 16) - ix); |
| 845 | *dst_ptr++ = SumPixels_16(boxwidth, src_ptr + ix) * |
| 846 | scaletbl[boxwidth - minboxwidth] >> |
| 847 | 16; |
| 848 | } |
| 849 | } |
| 850 |
| 851 | static void ScaleAddCols0_C(int dst_width, |
| 852 | int boxheight, |
| 853 | int x, |
| 854 | int dx, |
| 855 | const uint16_t* src_ptr, |
| 856 | uint8_t* dst_ptr) { |
| 857 | int scaleval = 65536 / boxheight; |
| 858 | int i; |
| 859 | (void)dx; |
| 860 | src_ptr += (x >> 16); |
| 861 | for (i = 0; i < dst_width; ++i) { |
| 862 | *dst_ptr++ = (uint8_t)(src_ptr[i] * scaleval >> 16); |
| 863 | } |
| 864 | } |
| 865 |
| 866 | static void ScaleAddCols1_C(int dst_width, |
| 867 | int boxheight, |
| 868 | int x, |
| 869 | int dx, |
| 870 | const uint16_t* src_ptr, |
| 871 | uint8_t* dst_ptr) { |
| 872 | int boxwidth = MIN1(dx >> 16); |
| 873 | int scaleval = 65536 / (boxwidth * boxheight); |
| 874 | int i; |
| 875 | x >>= 16; |
| 876 | for (i = 0; i < dst_width; ++i) { |
| 877 | *dst_ptr++ = (uint8_t)(SumPixels(boxwidth, src_ptr + x) * scaleval >> 16); |
| 878 | x += boxwidth; |
| 879 | } |
| 880 | } |
| 881 |
| 882 | static void ScaleAddCols1_16_C(int dst_width, |
| 883 | int boxheight, |
| 884 | int x, |
| 885 | int dx, |
| 886 | const uint32_t* src_ptr, |
| 887 | uint16_t* dst_ptr) { |
| 888 | int boxwidth = MIN1(dx >> 16); |
| 889 | int scaleval = 65536 / (boxwidth * boxheight); |
| 890 | int i; |
| 891 | for (i = 0; i < dst_width; ++i) { |
| 892 | *dst_ptr++ = SumPixels_16(boxwidth, src_ptr + x) * scaleval >> 16; |
| 893 | x += boxwidth; |
| 894 | } |
| 895 | } |
| 896 |
| 897 | // Scale plane down to any dimensions, with interpolation. |
| 898 | // (boxfilter). |
| 899 | // |
| 900 | // Same method as SimpleScale, which is fixed point, outputting |
| 901 | // one pixel of destination using fixed point (16.16) to step |
| 902 | // through source, sampling a box of pixel with simple |
| 903 | // averaging. |
| 904 | static void ScalePlaneBox(int src_width, |
| 905 | int src_height, |
| 906 | int dst_width, |
| 907 | int dst_height, |
| 908 | int src_stride, |
| 909 | int dst_stride, |
| 910 | const uint8_t* src_ptr, |
| 911 | uint8_t* dst_ptr) { |
| 912 | int j, k; |
| 913 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 914 | int x = 0; |
| 915 | int y = 0; |
| 916 | int dx = 0; |
| 917 | int dy = 0; |
| 918 | const int max_y = (src_height << 16); |
| 919 | ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterBox, &x, &y, |
| 920 | &dx, &dy); |
| 921 | src_width = Abs(src_width); |
| 922 | { |
| 923 | // Allocate a row buffer of uint16_t. |
| 924 | align_buffer_64(row16, src_width * 2); |
| 925 | void (*ScaleAddCols)(int dst_width, int boxheight, int x, int dx, |
| 926 | const uint16_t* src_ptr, uint8_t* dst_ptr) = |
| 927 | (dx & 0xffff) ? ScaleAddCols2_C |
| 928 | : ((dx != 0x10000) ? ScaleAddCols1_C : ScaleAddCols0_C); |
| 929 | void (*ScaleAddRow)(const uint8_t* src_ptr, uint16_t* dst_ptr, |
| 930 | int src_width) = ScaleAddRow_C; |
| 931 | #if defined(HAS_SCALEADDROW_SSE2) |
| 932 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 933 | ScaleAddRow = ScaleAddRow_Any_SSE2; |
| 934 | if (IS_ALIGNED(src_width, 16)) { |
| 935 | ScaleAddRow = ScaleAddRow_SSE2; |
| 936 | } |
| 937 | } |
| 938 | #endif |
| 939 | #if defined(HAS_SCALEADDROW_AVX2) |
| 940 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 941 | ScaleAddRow = ScaleAddRow_Any_AVX2; |
| 942 | if (IS_ALIGNED(src_width, 32)) { |
| 943 | ScaleAddRow = ScaleAddRow_AVX2; |
| 944 | } |
| 945 | } |
| 946 | #endif |
| 947 | #if defined(HAS_SCALEADDROW_NEON) |
| 948 | if (TestCpuFlag(kCpuHasNEON)) { |
| 949 | ScaleAddRow = ScaleAddRow_Any_NEON; |
| 950 | if (IS_ALIGNED(src_width, 16)) { |
| 951 | ScaleAddRow = ScaleAddRow_NEON; |
| 952 | } |
| 953 | } |
| 954 | #endif |
| 955 | #if defined(HAS_SCALEADDROW_MSA) |
| 956 | if (TestCpuFlag(kCpuHasMSA)) { |
| 957 | ScaleAddRow = ScaleAddRow_Any_MSA; |
| 958 | if (IS_ALIGNED(src_width, 16)) { |
| 959 | ScaleAddRow = ScaleAddRow_MSA; |
| 960 | } |
| 961 | } |
| 962 | #endif |
| 963 | #if defined(HAS_SCALEADDROW_LSX) |
| 964 | if (TestCpuFlag(kCpuHasLSX)) { |
| 965 | ScaleAddRow = ScaleAddRow_Any_LSX; |
| 966 | if (IS_ALIGNED(src_width, 16)) { |
| 967 | ScaleAddRow = ScaleAddRow_LSX; |
| 968 | } |
| 969 | } |
| 970 | #endif |
| 971 |
| 972 | for (j = 0; j < dst_height; ++j) { |
| 973 | int boxheight; |
| 974 | int iy = y >> 16; |
| 975 | const uint8_t* src = src_ptr + iy * (int64_t)src_stride; |
| 976 | y += dy; |
| 977 | if (y > max_y) { |
| 978 | y = max_y; |
| 979 | } |
| 980 | boxheight = MIN1((y >> 16) - iy); |
| 981 | memset(row16, 0, src_width * 2); |
| 982 | for (k = 0; k < boxheight; ++k) { |
| 983 | ScaleAddRow(src, (uint16_t*)(row16), src_width); |
| 984 | src += src_stride; |
| 985 | } |
| 986 | ScaleAddCols(dst_width, boxheight, x, dx, (uint16_t*)(row16), dst_ptr); |
| 987 | dst_ptr += dst_stride; |
| 988 | } |
| 989 | free_aligned_buffer_64(row16); |
| 990 | } |
| 991 | } |
| 992 |
| 993 | static void ScalePlaneBox_16(int src_width, |
| 994 | int src_height, |
| 995 | int dst_width, |
| 996 | int dst_height, |
| 997 | int src_stride, |
| 998 | int dst_stride, |
| 999 | const uint16_t* src_ptr, |
| 1000 | uint16_t* dst_ptr) { |
| 1001 | int j, k; |
| 1002 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1003 | int x = 0; |
| 1004 | int y = 0; |
| 1005 | int dx = 0; |
| 1006 | int dy = 0; |
| 1007 | const int max_y = (src_height << 16); |
| 1008 | ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterBox, &x, &y, |
| 1009 | &dx, &dy); |
| 1010 | src_width = Abs(src_width); |
| 1011 | { |
| 1012 | // Allocate a row buffer of uint32_t. |
| 1013 | align_buffer_64(row32, src_width * 4); |
| 1014 | void (*ScaleAddCols)(int dst_width, int boxheight, int x, int dx, |
| 1015 | const uint32_t* src_ptr, uint16_t* dst_ptr) = |
| 1016 | (dx & 0xffff) ? ScaleAddCols2_16_C : ScaleAddCols1_16_C; |
| 1017 | void (*ScaleAddRow)(const uint16_t* src_ptr, uint32_t* dst_ptr, |
| 1018 | int src_width) = ScaleAddRow_16_C; |
| 1019 |
| 1020 | #if defined(HAS_SCALEADDROW_16_SSE2) |
| 1021 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(src_width, 16)) { |
| 1022 | ScaleAddRow = ScaleAddRow_16_SSE2; |
| 1023 | } |
| 1024 | #endif |
| 1025 |
| 1026 | for (j = 0; j < dst_height; ++j) { |
| 1027 | int boxheight; |
| 1028 | int iy = y >> 16; |
| 1029 | const uint16_t* src = src_ptr + iy * (int64_t)src_stride; |
| 1030 | y += dy; |
| 1031 | if (y > max_y) { |
| 1032 | y = max_y; |
| 1033 | } |
| 1034 | boxheight = MIN1((y >> 16) - iy); |
| 1035 | memset(row32, 0, src_width * 4); |
| 1036 | for (k = 0; k < boxheight; ++k) { |
| 1037 | ScaleAddRow(src, (uint32_t*)(row32), src_width); |
| 1038 | src += src_stride; |
| 1039 | } |
| 1040 | ScaleAddCols(dst_width, boxheight, x, dx, (uint32_t*)(row32), dst_ptr); |
| 1041 | dst_ptr += dst_stride; |
| 1042 | } |
| 1043 | free_aligned_buffer_64(row32); |
| 1044 | } |
| 1045 | } |
| 1046 |
| 1047 | // Scale plane down with bilinear interpolation. |
| 1048 | void ScalePlaneBilinearDown(int src_width, |
| 1049 | int src_height, |
| 1050 | int dst_width, |
| 1051 | int dst_height, |
| 1052 | int src_stride, |
| 1053 | int dst_stride, |
| 1054 | const uint8_t* src_ptr, |
| 1055 | uint8_t* dst_ptr, |
| 1056 | enum FilterMode filtering) { |
| 1057 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1058 | int x = 0; |
| 1059 | int y = 0; |
| 1060 | int dx = 0; |
| 1061 | int dy = 0; |
| 1062 | // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear. |
| 1063 | // Allocate a row buffer. |
| 1064 | align_buffer_64(row, src_width); |
| 1065 |
| 1066 | const int max_y = (src_height - 1) << 16; |
| 1067 | int j; |
| 1068 | void (*ScaleFilterCols)(uint8_t * dst_ptr, const uint8_t* src_ptr, |
| 1069 | int dst_width, int x, int dx) = |
| 1070 | (src_width >= 32768) ? ScaleFilterCols64_C : ScaleFilterCols_C; |
| 1071 | void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr, |
| 1072 | ptrdiff_t src_stride, int dst_width, |
| 1073 | int source_y_fraction) = InterpolateRow_C; |
| 1074 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, &x, &y, |
| 1075 | &dx, &dy); |
| 1076 | src_width = Abs(src_width); |
| 1077 |
| 1078 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 1079 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1080 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 1081 | if (IS_ALIGNED(src_width, 16)) { |
| 1082 | InterpolateRow = InterpolateRow_SSSE3; |
| 1083 | } |
| 1084 | } |
| 1085 | #endif |
| 1086 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 1087 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1088 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 1089 | if (IS_ALIGNED(src_width, 32)) { |
| 1090 | InterpolateRow = InterpolateRow_AVX2; |
| 1091 | } |
| 1092 | } |
| 1093 | #endif |
| 1094 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 1095 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1096 | InterpolateRow = InterpolateRow_Any_NEON; |
| 1097 | if (IS_ALIGNED(src_width, 16)) { |
| 1098 | InterpolateRow = InterpolateRow_NEON; |
| 1099 | } |
| 1100 | } |
| 1101 | #endif |
| 1102 | #if defined(HAS_INTERPOLATEROW_MSA) |
| 1103 | if (TestCpuFlag(kCpuHasMSA)) { |
| 1104 | InterpolateRow = InterpolateRow_Any_MSA; |
| 1105 | if (IS_ALIGNED(src_width, 32)) { |
| 1106 | InterpolateRow = InterpolateRow_MSA; |
| 1107 | } |
| 1108 | } |
| 1109 | #endif |
| 1110 | #if defined(HAS_INTERPOLATEROW_LSX) |
| 1111 | if (TestCpuFlag(kCpuHasLSX)) { |
| 1112 | InterpolateRow = InterpolateRow_Any_LSX; |
| 1113 | if (IS_ALIGNED(src_width, 32)) { |
| 1114 | InterpolateRow = InterpolateRow_LSX; |
| 1115 | } |
| 1116 | } |
| 1117 | #endif |
| 1118 |
| 1119 | #if defined(HAS_SCALEFILTERCOLS_SSSE3) |
| 1120 | if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 1121 | ScaleFilterCols = ScaleFilterCols_SSSE3; |
| 1122 | } |
| 1123 | #endif |
| 1124 | #if defined(HAS_SCALEFILTERCOLS_NEON) |
| 1125 | if (TestCpuFlag(kCpuHasNEON) && src_width < 32768) { |
| 1126 | ScaleFilterCols = ScaleFilterCols_Any_NEON; |
| 1127 | if (IS_ALIGNED(dst_width, 8)) { |
| 1128 | ScaleFilterCols = ScaleFilterCols_NEON; |
| 1129 | } |
| 1130 | } |
| 1131 | #endif |
| 1132 | #if defined(HAS_SCALEFILTERCOLS_MSA) |
| 1133 | if (TestCpuFlag(kCpuHasMSA) && src_width < 32768) { |
| 1134 | ScaleFilterCols = ScaleFilterCols_Any_MSA; |
| 1135 | if (IS_ALIGNED(dst_width, 16)) { |
| 1136 | ScaleFilterCols = ScaleFilterCols_MSA; |
| 1137 | } |
| 1138 | } |
| 1139 | #endif |
| 1140 | #if defined(HAS_SCALEFILTERCOLS_LSX) |
| 1141 | if (TestCpuFlag(kCpuHasLSX) && src_width < 32768) { |
| 1142 | ScaleFilterCols = ScaleFilterCols_Any_LSX; |
| 1143 | if (IS_ALIGNED(dst_width, 16)) { |
| 1144 | ScaleFilterCols = ScaleFilterCols_LSX; |
| 1145 | } |
| 1146 | } |
| 1147 | #endif |
| 1148 | if (y > max_y) { |
| 1149 | y = max_y; |
| 1150 | } |
| 1151 |
| 1152 | for (j = 0; j < dst_height; ++j) { |
| 1153 | int yi = y >> 16; |
| 1154 | const uint8_t* src = src_ptr + yi * (int64_t)src_stride; |
| 1155 | if (filtering == kFilterLinear) { |
| 1156 | ScaleFilterCols(dst_ptr, src, dst_width, x, dx); |
| 1157 | } else { |
| 1158 | int yf = (y >> 8) & 255; |
| 1159 | InterpolateRow(row, src, src_stride, src_width, yf); |
| 1160 | ScaleFilterCols(dst_ptr, row, dst_width, x, dx); |
| 1161 | } |
| 1162 | dst_ptr += dst_stride; |
| 1163 | y += dy; |
| 1164 | if (y > max_y) { |
| 1165 | y = max_y; |
| 1166 | } |
| 1167 | } |
| 1168 | free_aligned_buffer_64(row); |
| 1169 | } |
| 1170 |
| 1171 | void ScalePlaneBilinearDown_16(int src_width, |
| 1172 | int src_height, |
| 1173 | int dst_width, |
| 1174 | int dst_height, |
| 1175 | int src_stride, |
| 1176 | int dst_stride, |
| 1177 | const uint16_t* src_ptr, |
| 1178 | uint16_t* dst_ptr, |
| 1179 | enum FilterMode filtering) { |
| 1180 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1181 | int x = 0; |
| 1182 | int y = 0; |
| 1183 | int dx = 0; |
| 1184 | int dy = 0; |
| 1185 | // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear. |
| 1186 | // Allocate a row buffer. |
| 1187 | align_buffer_64(row, src_width * 2); |
| 1188 |
| 1189 | const int max_y = (src_height - 1) << 16; |
| 1190 | int j; |
| 1191 | void (*ScaleFilterCols)(uint16_t * dst_ptr, const uint16_t* src_ptr, |
| 1192 | int dst_width, int x, int dx) = |
| 1193 | (src_width >= 32768) ? ScaleFilterCols64_16_C : ScaleFilterCols_16_C; |
| 1194 | void (*InterpolateRow)(uint16_t * dst_ptr, const uint16_t* src_ptr, |
| 1195 | ptrdiff_t src_stride, int dst_width, |
| 1196 | int source_y_fraction) = InterpolateRow_16_C; |
| 1197 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, &x, &y, |
| 1198 | &dx, &dy); |
| 1199 | src_width = Abs(src_width); |
| 1200 |
| 1201 | #if defined(HAS_INTERPOLATEROW_16_SSE2) |
| 1202 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1203 | InterpolateRow = InterpolateRow_16_Any_SSE2; |
| 1204 | if (IS_ALIGNED(src_width, 16)) { |
| 1205 | InterpolateRow = InterpolateRow_16_SSE2; |
| 1206 | } |
| 1207 | } |
| 1208 | #endif |
| 1209 | #if defined(HAS_INTERPOLATEROW_16_SSSE3) |
| 1210 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1211 | InterpolateRow = InterpolateRow_16_Any_SSSE3; |
| 1212 | if (IS_ALIGNED(src_width, 16)) { |
| 1213 | InterpolateRow = InterpolateRow_16_SSSE3; |
| 1214 | } |
| 1215 | } |
| 1216 | #endif |
| 1217 | #if defined(HAS_INTERPOLATEROW_16_AVX2) |
| 1218 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1219 | InterpolateRow = InterpolateRow_16_Any_AVX2; |
| 1220 | if (IS_ALIGNED(src_width, 32)) { |
| 1221 | InterpolateRow = InterpolateRow_16_AVX2; |
| 1222 | } |
| 1223 | } |
| 1224 | #endif |
| 1225 | #if defined(HAS_INTERPOLATEROW_16_NEON) |
| 1226 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1227 | InterpolateRow = InterpolateRow_16_Any_NEON; |
| 1228 | if (IS_ALIGNED(src_width, 16)) { |
| 1229 | InterpolateRow = InterpolateRow_16_NEON; |
| 1230 | } |
| 1231 | } |
| 1232 | #endif |
| 1233 |
| 1234 | #if defined(HAS_SCALEFILTERCOLS_16_SSSE3) |
| 1235 | if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 1236 | ScaleFilterCols = ScaleFilterCols_16_SSSE3; |
| 1237 | } |
| 1238 | #endif |
| 1239 | if (y > max_y) { |
| 1240 | y = max_y; |
| 1241 | } |
| 1242 |
| 1243 | for (j = 0; j < dst_height; ++j) { |
| 1244 | int yi = y >> 16; |
| 1245 | const uint16_t* src = src_ptr + yi * (int64_t)src_stride; |
| 1246 | if (filtering == kFilterLinear) { |
| 1247 | ScaleFilterCols(dst_ptr, src, dst_width, x, dx); |
| 1248 | } else { |
| 1249 | int yf = (y >> 8) & 255; |
| 1250 | InterpolateRow((uint16_t*)row, src, src_stride, src_width, yf); |
| 1251 | ScaleFilterCols(dst_ptr, (uint16_t*)row, dst_width, x, dx); |
| 1252 | } |
| 1253 | dst_ptr += dst_stride; |
| 1254 | y += dy; |
| 1255 | if (y > max_y) { |
| 1256 | y = max_y; |
| 1257 | } |
| 1258 | } |
| 1259 | free_aligned_buffer_64(row); |
| 1260 | } |
| 1261 |
| 1262 | // Scale up down with bilinear interpolation. |
| 1263 | void ScalePlaneBilinearUp(int src_width, |
| 1264 | int src_height, |
| 1265 | int dst_width, |
| 1266 | int dst_height, |
| 1267 | int src_stride, |
| 1268 | int dst_stride, |
| 1269 | const uint8_t* src_ptr, |
| 1270 | uint8_t* dst_ptr, |
| 1271 | enum FilterMode filtering) { |
| 1272 | int j; |
| 1273 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1274 | int x = 0; |
| 1275 | int y = 0; |
| 1276 | int dx = 0; |
| 1277 | int dy = 0; |
| 1278 | const int max_y = (src_height - 1) << 16; |
| 1279 | void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr, |
| 1280 | ptrdiff_t src_stride, int dst_width, |
| 1281 | int source_y_fraction) = InterpolateRow_C; |
| 1282 | void (*ScaleFilterCols)(uint8_t * dst_ptr, const uint8_t* src_ptr, |
| 1283 | int dst_width, int x, int dx) = |
| 1284 | filtering ? ScaleFilterCols_C : ScaleCols_C; |
| 1285 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, &x, &y, |
| 1286 | &dx, &dy); |
| 1287 | src_width = Abs(src_width); |
| 1288 |
| 1289 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 1290 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1291 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 1292 | if (IS_ALIGNED(dst_width, 16)) { |
| 1293 | InterpolateRow = InterpolateRow_SSSE3; |
| 1294 | } |
| 1295 | } |
| 1296 | #endif |
| 1297 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 1298 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1299 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 1300 | if (IS_ALIGNED(dst_width, 32)) { |
| 1301 | InterpolateRow = InterpolateRow_AVX2; |
| 1302 | } |
| 1303 | } |
| 1304 | #endif |
| 1305 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 1306 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1307 | InterpolateRow = InterpolateRow_Any_NEON; |
| 1308 | if (IS_ALIGNED(dst_width, 16)) { |
| 1309 | InterpolateRow = InterpolateRow_NEON; |
| 1310 | } |
| 1311 | } |
| 1312 | #endif |
| 1313 |
| 1314 | if (filtering && src_width >= 32768) { |
| 1315 | ScaleFilterCols = ScaleFilterCols64_C; |
| 1316 | } |
| 1317 | #if defined(HAS_SCALEFILTERCOLS_SSSE3) |
| 1318 | if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 1319 | ScaleFilterCols = ScaleFilterCols_SSSE3; |
| 1320 | } |
| 1321 | #endif |
| 1322 | #if defined(HAS_SCALEFILTERCOLS_NEON) |
| 1323 | if (filtering && TestCpuFlag(kCpuHasNEON) && src_width < 32768) { |
| 1324 | ScaleFilterCols = ScaleFilterCols_Any_NEON; |
| 1325 | if (IS_ALIGNED(dst_width, 8)) { |
| 1326 | ScaleFilterCols = ScaleFilterCols_NEON; |
| 1327 | } |
| 1328 | } |
| 1329 | #endif |
| 1330 | #if defined(HAS_SCALEFILTERCOLS_MSA) |
| 1331 | if (filtering && TestCpuFlag(kCpuHasMSA) && src_width < 32768) { |
| 1332 | ScaleFilterCols = ScaleFilterCols_Any_MSA; |
| 1333 | if (IS_ALIGNED(dst_width, 16)) { |
| 1334 | ScaleFilterCols = ScaleFilterCols_MSA; |
| 1335 | } |
| 1336 | } |
| 1337 | #endif |
| 1338 | #if defined(HAS_SCALEFILTERCOLS_LSX) |
| 1339 | if (filtering && TestCpuFlag(kCpuHasLSX) && src_width < 32768) { |
| 1340 | ScaleFilterCols = ScaleFilterCols_Any_LSX; |
| 1341 | if (IS_ALIGNED(dst_width, 16)) { |
| 1342 | ScaleFilterCols = ScaleFilterCols_LSX; |
| 1343 | } |
| 1344 | } |
| 1345 | #endif |
| 1346 | if (!filtering && src_width * 2 == dst_width && x < 0x8000) { |
| 1347 | ScaleFilterCols = ScaleColsUp2_C; |
| 1348 | #if defined(HAS_SCALECOLS_SSE2) |
| 1349 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 1350 | ScaleFilterCols = ScaleColsUp2_SSE2; |
| 1351 | } |
| 1352 | #endif |
| 1353 | } |
| 1354 |
| 1355 | if (y > max_y) { |
| 1356 | y = max_y; |
| 1357 | } |
| 1358 | { |
| 1359 | int yi = y >> 16; |
| 1360 | const uint8_t* src = src_ptr + yi * (int64_t)src_stride; |
| 1361 |
| 1362 | // Allocate 2 row buffers. |
| 1363 | const int row_size = (dst_width + 31) & ~31; |
| 1364 | align_buffer_64(row, row_size * 2); |
| 1365 |
| 1366 | uint8_t* rowptr = row; |
| 1367 | int rowstride = row_size; |
| 1368 | int lasty = yi; |
| 1369 |
| 1370 | ScaleFilterCols(rowptr, src, dst_width, x, dx); |
| 1371 | if (src_height > 1) { |
| 1372 | src += src_stride; |
| 1373 | } |
| 1374 | ScaleFilterCols(rowptr + rowstride, src, dst_width, x, dx); |
| 1375 | if (src_height > 2) { |
| 1376 | src += src_stride; |
| 1377 | } |
| 1378 |
| 1379 | for (j = 0; j < dst_height; ++j) { |
| 1380 | yi = y >> 16; |
| 1381 | if (yi != lasty) { |
| 1382 | if (y > max_y) { |
| 1383 | y = max_y; |
| 1384 | yi = y >> 16; |
| 1385 | src = src_ptr + yi * (int64_t)src_stride; |
| 1386 | } |
| 1387 | if (yi != lasty) { |
| 1388 | ScaleFilterCols(rowptr, src, dst_width, x, dx); |
| 1389 | rowptr += rowstride; |
| 1390 | rowstride = -rowstride; |
| 1391 | lasty = yi; |
| 1392 | if ((y + 65536) < max_y) { |
| 1393 | src += src_stride; |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | if (filtering == kFilterLinear) { |
| 1398 | InterpolateRow(dst_ptr, rowptr, 0, dst_width, 0); |
| 1399 | } else { |
| 1400 | int yf = (y >> 8) & 255; |
| 1401 | InterpolateRow(dst_ptr, rowptr, rowstride, dst_width, yf); |
| 1402 | } |
| 1403 | dst_ptr += dst_stride; |
| 1404 | y += dy; |
| 1405 | } |
| 1406 | free_aligned_buffer_64(row); |
| 1407 | } |
| 1408 | } |
| 1409 |
| 1410 | // Scale plane, horizontally up by 2 times. |
| 1411 | // Uses linear filter horizontally, nearest vertically. |
| 1412 | // This is an optimized version for scaling up a plane to 2 times of |
| 1413 | // its original width, using linear interpolation. |
| 1414 | // This is used to scale U and V planes of I422 to I444. |
| 1415 | void ScalePlaneUp2_Linear(int src_width, |
| 1416 | int src_height, |
| 1417 | int dst_width, |
| 1418 | int dst_height, |
| 1419 | int src_stride, |
| 1420 | int dst_stride, |
| 1421 | const uint8_t* src_ptr, |
| 1422 | uint8_t* dst_ptr) { |
| 1423 | void (*ScaleRowUp)(const uint8_t* src_ptr, uint8_t* dst_ptr, int dst_width) = |
| 1424 | ScaleRowUp2_Linear_Any_C; |
| 1425 | int i; |
| 1426 | int y; |
| 1427 | int dy; |
| 1428 |
| 1429 | // This function can only scale up by 2 times horizontally. |
| 1430 | assert(src_width == ((dst_width + 1) / 2)); |
| 1431 |
| 1432 | #ifdef HAS_SCALEROWUP2_LINEAR_SSE2 |
| 1433 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1434 | ScaleRowUp = ScaleRowUp2_Linear_Any_SSE2; |
| 1435 | } |
| 1436 | #endif |
| 1437 |
| 1438 | #ifdef HAS_SCALEROWUP2_LINEAR_SSSE3 |
| 1439 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1440 | ScaleRowUp = ScaleRowUp2_Linear_Any_SSSE3; |
| 1441 | } |
| 1442 | #endif |
| 1443 |
| 1444 | #ifdef HAS_SCALEROWUP2_LINEAR_AVX2 |
| 1445 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1446 | ScaleRowUp = ScaleRowUp2_Linear_Any_AVX2; |
| 1447 | } |
| 1448 | #endif |
| 1449 |
| 1450 | #ifdef HAS_SCALEROWUP2_LINEAR_NEON |
| 1451 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1452 | ScaleRowUp = ScaleRowUp2_Linear_Any_NEON; |
| 1453 | } |
| 1454 | #endif |
| 1455 |
| 1456 | if (dst_height == 1) { |
| 1457 | ScaleRowUp(src_ptr + ((src_height - 1) / 2) * (int64_t)src_stride, dst_ptr, |
| 1458 | dst_width); |
| 1459 | } else { |
| 1460 | dy = FixedDiv(src_height - 1, dst_height - 1); |
| 1461 | y = (1 << 15) - 1; |
| 1462 | for (i = 0; i < dst_height; ++i) { |
| 1463 | ScaleRowUp(src_ptr + (y >> 16) * (int64_t)src_stride, dst_ptr, dst_width); |
| 1464 | dst_ptr += dst_stride; |
| 1465 | y += dy; |
| 1466 | } |
| 1467 | } |
| 1468 | } |
| 1469 |
| 1470 | // Scale plane, up by 2 times. |
| 1471 | // This is an optimized version for scaling up a plane to 2 times of |
| 1472 | // its original size, using bilinear interpolation. |
| 1473 | // This is used to scale U and V planes of I420 to I444. |
| 1474 | void ScalePlaneUp2_Bilinear(int src_width, |
| 1475 | int src_height, |
| 1476 | int dst_width, |
| 1477 | int dst_height, |
| 1478 | int src_stride, |
| 1479 | int dst_stride, |
| 1480 | const uint8_t* src_ptr, |
| 1481 | uint8_t* dst_ptr) { |
| 1482 | void (*Scale2RowUp)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 1483 | uint8_t* dst_ptr, ptrdiff_t dst_stride, int dst_width) = |
| 1484 | ScaleRowUp2_Bilinear_Any_C; |
| 1485 | int x; |
| 1486 |
| 1487 | // This function can only scale up by 2 times. |
| 1488 | assert(src_width == ((dst_width + 1) / 2)); |
| 1489 | assert(src_height == ((dst_height + 1) / 2)); |
| 1490 |
| 1491 | #ifdef HAS_SCALEROWUP2_BILINEAR_SSE2 |
| 1492 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1493 | Scale2RowUp = ScaleRowUp2_Bilinear_Any_SSE2; |
| 1494 | } |
| 1495 | #endif |
| 1496 |
| 1497 | #ifdef HAS_SCALEROWUP2_BILINEAR_SSSE3 |
| 1498 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1499 | Scale2RowUp = ScaleRowUp2_Bilinear_Any_SSSE3; |
| 1500 | } |
| 1501 | #endif |
| 1502 |
| 1503 | #ifdef HAS_SCALEROWUP2_BILINEAR_AVX2 |
| 1504 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1505 | Scale2RowUp = ScaleRowUp2_Bilinear_Any_AVX2; |
| 1506 | } |
| 1507 | #endif |
| 1508 |
| 1509 | #ifdef HAS_SCALEROWUP2_BILINEAR_NEON |
| 1510 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1511 | Scale2RowUp = ScaleRowUp2_Bilinear_Any_NEON; |
| 1512 | } |
| 1513 | #endif |
| 1514 |
| 1515 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 1516 | dst_ptr += dst_stride; |
| 1517 | for (x = 0; x < src_height - 1; ++x) { |
| 1518 | Scale2RowUp(src_ptr, src_stride, dst_ptr, dst_stride, dst_width); |
| 1519 | src_ptr += src_stride; |
| 1520 | // TODO(fbarchard): Test performance of writing one row of destination at a |
| 1521 | // time. |
| 1522 | dst_ptr += 2 * dst_stride; |
| 1523 | } |
| 1524 | if (!(dst_height & 1)) { |
| 1525 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 1526 | } |
| 1527 | } |
| 1528 |
| 1529 | // Scale at most 14 bit plane, horizontally up by 2 times. |
| 1530 | // This is an optimized version for scaling up a plane to 2 times of |
| 1531 | // its original width, using linear interpolation. |
| 1532 | // stride is in count of uint16_t. |
| 1533 | // This is used to scale U and V planes of I210 to I410 and I212 to I412. |
| 1534 | void ScalePlaneUp2_12_Linear(int src_width, |
| 1535 | int src_height, |
| 1536 | int dst_width, |
| 1537 | int dst_height, |
| 1538 | int src_stride, |
| 1539 | int dst_stride, |
| 1540 | const uint16_t* src_ptr, |
| 1541 | uint16_t* dst_ptr) { |
| 1542 | void (*ScaleRowUp)(const uint16_t* src_ptr, uint16_t* dst_ptr, |
| 1543 | int dst_width) = ScaleRowUp2_Linear_16_Any_C; |
| 1544 | int i; |
| 1545 | int y; |
| 1546 | int dy; |
| 1547 |
| 1548 | // This function can only scale up by 2 times horizontally. |
| 1549 | assert(src_width == ((dst_width + 1) / 2)); |
| 1550 |
| 1551 | #ifdef HAS_SCALEROWUP2_LINEAR_12_SSSE3 |
| 1552 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1553 | ScaleRowUp = ScaleRowUp2_Linear_12_Any_SSSE3; |
| 1554 | } |
| 1555 | #endif |
| 1556 |
| 1557 | #ifdef HAS_SCALEROWUP2_LINEAR_12_AVX2 |
| 1558 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1559 | ScaleRowUp = ScaleRowUp2_Linear_12_Any_AVX2; |
| 1560 | } |
| 1561 | #endif |
| 1562 |
| 1563 | #ifdef HAS_SCALEROWUP2_LINEAR_12_NEON |
| 1564 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1565 | ScaleRowUp = ScaleRowUp2_Linear_12_Any_NEON; |
| 1566 | } |
| 1567 | #endif |
| 1568 |
| 1569 | if (dst_height == 1) { |
| 1570 | ScaleRowUp(src_ptr + ((src_height - 1) / 2) * (int64_t)src_stride, dst_ptr, |
| 1571 | dst_width); |
| 1572 | } else { |
| 1573 | dy = FixedDiv(src_height - 1, dst_height - 1); |
| 1574 | y = (1 << 15) - 1; |
| 1575 | for (i = 0; i < dst_height; ++i) { |
| 1576 | ScaleRowUp(src_ptr + (y >> 16) * (int64_t)src_stride, dst_ptr, dst_width); |
| 1577 | dst_ptr += dst_stride; |
| 1578 | y += dy; |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 |
| 1583 | // Scale at most 12 bit plane, up by 2 times. |
| 1584 | // This is an optimized version for scaling up a plane to 2 times of |
| 1585 | // its original size, using bilinear interpolation. |
| 1586 | // stride is in count of uint16_t. |
| 1587 | // This is used to scale U and V planes of I010 to I410 and I012 to I412. |
| 1588 | void ScalePlaneUp2_12_Bilinear(int src_width, |
| 1589 | int src_height, |
| 1590 | int dst_width, |
| 1591 | int dst_height, |
| 1592 | int src_stride, |
| 1593 | int dst_stride, |
| 1594 | const uint16_t* src_ptr, |
| 1595 | uint16_t* dst_ptr) { |
| 1596 | void (*Scale2RowUp)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 1597 | uint16_t* dst_ptr, ptrdiff_t dst_stride, int dst_width) = |
| 1598 | ScaleRowUp2_Bilinear_16_Any_C; |
| 1599 | int x; |
| 1600 |
| 1601 | // This function can only scale up by 2 times. |
| 1602 | assert(src_width == ((dst_width + 1) / 2)); |
| 1603 | assert(src_height == ((dst_height + 1) / 2)); |
| 1604 |
| 1605 | #ifdef HAS_SCALEROWUP2_BILINEAR_12_SSSE3 |
| 1606 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1607 | Scale2RowUp = ScaleRowUp2_Bilinear_12_Any_SSSE3; |
| 1608 | } |
| 1609 | #endif |
| 1610 |
| 1611 | #ifdef HAS_SCALEROWUP2_BILINEAR_12_AVX2 |
| 1612 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1613 | Scale2RowUp = ScaleRowUp2_Bilinear_12_Any_AVX2; |
| 1614 | } |
| 1615 | #endif |
| 1616 |
| 1617 | #ifdef HAS_SCALEROWUP2_BILINEAR_12_NEON |
| 1618 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1619 | Scale2RowUp = ScaleRowUp2_Bilinear_12_Any_NEON; |
| 1620 | } |
| 1621 | #endif |
| 1622 |
| 1623 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 1624 | dst_ptr += dst_stride; |
| 1625 | for (x = 0; x < src_height - 1; ++x) { |
| 1626 | Scale2RowUp(src_ptr, src_stride, dst_ptr, dst_stride, dst_width); |
| 1627 | src_ptr += src_stride; |
| 1628 | dst_ptr += 2 * dst_stride; |
| 1629 | } |
| 1630 | if (!(dst_height & 1)) { |
| 1631 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 1632 | } |
| 1633 | } |
| 1634 |
| 1635 | void ScalePlaneUp2_16_Linear(int src_width, |
| 1636 | int src_height, |
| 1637 | int dst_width, |
| 1638 | int dst_height, |
| 1639 | int src_stride, |
| 1640 | int dst_stride, |
| 1641 | const uint16_t* src_ptr, |
| 1642 | uint16_t* dst_ptr) { |
| 1643 | void (*ScaleRowUp)(const uint16_t* src_ptr, uint16_t* dst_ptr, |
| 1644 | int dst_width) = ScaleRowUp2_Linear_16_Any_C; |
| 1645 | int i; |
| 1646 | int y; |
| 1647 | int dy; |
| 1648 |
| 1649 | // This function can only scale up by 2 times horizontally. |
| 1650 | assert(src_width == ((dst_width + 1) / 2)); |
| 1651 |
| 1652 | #ifdef HAS_SCALEROWUP2_LINEAR_16_SSE2 |
| 1653 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1654 | ScaleRowUp = ScaleRowUp2_Linear_16_Any_SSE2; |
| 1655 | } |
| 1656 | #endif |
| 1657 |
| 1658 | #ifdef HAS_SCALEROWUP2_LINEAR_16_AVX2 |
| 1659 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1660 | ScaleRowUp = ScaleRowUp2_Linear_16_Any_AVX2; |
| 1661 | } |
| 1662 | #endif |
| 1663 |
| 1664 | #ifdef HAS_SCALEROWUP2_LINEAR_16_NEON |
| 1665 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1666 | ScaleRowUp = ScaleRowUp2_Linear_16_Any_NEON; |
| 1667 | } |
| 1668 | #endif |
| 1669 |
| 1670 | if (dst_height == 1) { |
| 1671 | ScaleRowUp(src_ptr + ((src_height - 1) / 2) * (int64_t)src_stride, dst_ptr, |
| 1672 | dst_width); |
| 1673 | } else { |
| 1674 | dy = FixedDiv(src_height - 1, dst_height - 1); |
| 1675 | y = (1 << 15) - 1; |
| 1676 | for (i = 0; i < dst_height; ++i) { |
| 1677 | ScaleRowUp(src_ptr + (y >> 16) * (int64_t)src_stride, dst_ptr, dst_width); |
| 1678 | dst_ptr += dst_stride; |
| 1679 | y += dy; |
| 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 |
| 1684 | void ScalePlaneUp2_16_Bilinear(int src_width, |
| 1685 | int src_height, |
| 1686 | int dst_width, |
| 1687 | int dst_height, |
| 1688 | int src_stride, |
| 1689 | int dst_stride, |
| 1690 | const uint16_t* src_ptr, |
| 1691 | uint16_t* dst_ptr) { |
| 1692 | void (*Scale2RowUp)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 1693 | uint16_t* dst_ptr, ptrdiff_t dst_stride, int dst_width) = |
| 1694 | ScaleRowUp2_Bilinear_16_Any_C; |
| 1695 | int x; |
| 1696 |
| 1697 | // This function can only scale up by 2 times. |
| 1698 | assert(src_width == ((dst_width + 1) / 2)); |
| 1699 | assert(src_height == ((dst_height + 1) / 2)); |
| 1700 |
| 1701 | #ifdef HAS_SCALEROWUP2_BILINEAR_16_SSE2 |
| 1702 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1703 | Scale2RowUp = ScaleRowUp2_Bilinear_16_Any_SSE2; |
| 1704 | } |
| 1705 | #endif |
| 1706 |
| 1707 | #ifdef HAS_SCALEROWUP2_BILINEAR_16_AVX2 |
| 1708 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1709 | Scale2RowUp = ScaleRowUp2_Bilinear_16_Any_AVX2; |
| 1710 | } |
| 1711 | #endif |
| 1712 |
| 1713 | #ifdef HAS_SCALEROWUP2_BILINEAR_16_NEON |
| 1714 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1715 | Scale2RowUp = ScaleRowUp2_Bilinear_16_Any_NEON; |
| 1716 | } |
| 1717 | #endif |
| 1718 |
| 1719 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 1720 | dst_ptr += dst_stride; |
| 1721 | for (x = 0; x < src_height - 1; ++x) { |
| 1722 | Scale2RowUp(src_ptr, src_stride, dst_ptr, dst_stride, dst_width); |
| 1723 | src_ptr += src_stride; |
| 1724 | dst_ptr += 2 * dst_stride; |
| 1725 | } |
| 1726 | if (!(dst_height & 1)) { |
| 1727 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 1728 | } |
| 1729 | } |
| 1730 |
| 1731 | void ScalePlaneBilinearUp_16(int src_width, |
| 1732 | int src_height, |
| 1733 | int dst_width, |
| 1734 | int dst_height, |
| 1735 | int src_stride, |
| 1736 | int dst_stride, |
| 1737 | const uint16_t* src_ptr, |
| 1738 | uint16_t* dst_ptr, |
| 1739 | enum FilterMode filtering) { |
| 1740 | int j; |
| 1741 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1742 | int x = 0; |
| 1743 | int y = 0; |
| 1744 | int dx = 0; |
| 1745 | int dy = 0; |
| 1746 | const int max_y = (src_height - 1) << 16; |
| 1747 | void (*InterpolateRow)(uint16_t * dst_ptr, const uint16_t* src_ptr, |
| 1748 | ptrdiff_t src_stride, int dst_width, |
| 1749 | int source_y_fraction) = InterpolateRow_16_C; |
| 1750 | void (*ScaleFilterCols)(uint16_t * dst_ptr, const uint16_t* src_ptr, |
| 1751 | int dst_width, int x, int dx) = |
| 1752 | filtering ? ScaleFilterCols_16_C : ScaleCols_16_C; |
| 1753 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, &x, &y, |
| 1754 | &dx, &dy); |
| 1755 | src_width = Abs(src_width); |
| 1756 |
| 1757 | #if defined(HAS_INTERPOLATEROW_16_SSE2) |
| 1758 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1759 | InterpolateRow = InterpolateRow_16_Any_SSE2; |
| 1760 | if (IS_ALIGNED(dst_width, 16)) { |
| 1761 | InterpolateRow = InterpolateRow_16_SSE2; |
| 1762 | } |
| 1763 | } |
| 1764 | #endif |
| 1765 | #if defined(HAS_INTERPOLATEROW_16_SSSE3) |
| 1766 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1767 | InterpolateRow = InterpolateRow_16_Any_SSSE3; |
| 1768 | if (IS_ALIGNED(dst_width, 16)) { |
| 1769 | InterpolateRow = InterpolateRow_16_SSSE3; |
| 1770 | } |
| 1771 | } |
| 1772 | #endif |
| 1773 | #if defined(HAS_INTERPOLATEROW_16_AVX2) |
| 1774 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1775 | InterpolateRow = InterpolateRow_16_Any_AVX2; |
| 1776 | if (IS_ALIGNED(dst_width, 32)) { |
| 1777 | InterpolateRow = InterpolateRow_16_AVX2; |
| 1778 | } |
| 1779 | } |
| 1780 | #endif |
| 1781 | #if defined(HAS_INTERPOLATEROW_16_NEON) |
| 1782 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1783 | InterpolateRow = InterpolateRow_16_Any_NEON; |
| 1784 | if (IS_ALIGNED(dst_width, 16)) { |
| 1785 | InterpolateRow = InterpolateRow_16_NEON; |
| 1786 | } |
| 1787 | } |
| 1788 | #endif |
| 1789 |
| 1790 | if (filtering && src_width >= 32768) { |
| 1791 | ScaleFilterCols = ScaleFilterCols64_16_C; |
| 1792 | } |
| 1793 | #if defined(HAS_SCALEFILTERCOLS_16_SSSE3) |
| 1794 | if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 1795 | ScaleFilterCols = ScaleFilterCols_16_SSSE3; |
| 1796 | } |
| 1797 | #endif |
| 1798 | if (!filtering && src_width * 2 == dst_width && x < 0x8000) { |
| 1799 | ScaleFilterCols = ScaleColsUp2_16_C; |
| 1800 | #if defined(HAS_SCALECOLS_16_SSE2) |
| 1801 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 1802 | ScaleFilterCols = ScaleColsUp2_16_SSE2; |
| 1803 | } |
| 1804 | #endif |
| 1805 | } |
| 1806 | if (y > max_y) { |
| 1807 | y = max_y; |
| 1808 | } |
| 1809 | { |
| 1810 | int yi = y >> 16; |
| 1811 | const uint16_t* src = src_ptr + yi * (int64_t)src_stride; |
| 1812 |
| 1813 | // Allocate 2 row buffers. |
| 1814 | const int row_size = (dst_width + 31) & ~31; |
| 1815 | align_buffer_64(row, row_size * 4); |
| 1816 |
| 1817 | uint16_t* rowptr = (uint16_t*)row; |
| 1818 | int rowstride = row_size; |
| 1819 | int lasty = yi; |
| 1820 |
| 1821 | ScaleFilterCols(rowptr, src, dst_width, x, dx); |
| 1822 | if (src_height > 1) { |
| 1823 | src += src_stride; |
| 1824 | } |
| 1825 | ScaleFilterCols(rowptr + rowstride, src, dst_width, x, dx); |
| 1826 | if (src_height > 2) { |
| 1827 | src += src_stride; |
| 1828 | } |
| 1829 |
| 1830 | for (j = 0; j < dst_height; ++j) { |
| 1831 | yi = y >> 16; |
| 1832 | if (yi != lasty) { |
| 1833 | if (y > max_y) { |
| 1834 | y = max_y; |
| 1835 | yi = y >> 16; |
| 1836 | src = src_ptr + yi * (int64_t)src_stride; |
| 1837 | } |
| 1838 | if (yi != lasty) { |
| 1839 | ScaleFilterCols(rowptr, src, dst_width, x, dx); |
| 1840 | rowptr += rowstride; |
| 1841 | rowstride = -rowstride; |
| 1842 | lasty = yi; |
| 1843 | if ((y + 65536) < max_y) { |
| 1844 | src += src_stride; |
| 1845 | } |
| 1846 | } |
| 1847 | } |
| 1848 | if (filtering == kFilterLinear) { |
| 1849 | InterpolateRow(dst_ptr, rowptr, 0, dst_width, 0); |
| 1850 | } else { |
| 1851 | int yf = (y >> 8) & 255; |
| 1852 | InterpolateRow(dst_ptr, rowptr, rowstride, dst_width, yf); |
| 1853 | } |
| 1854 | dst_ptr += dst_stride; |
| 1855 | y += dy; |
| 1856 | } |
| 1857 | free_aligned_buffer_64(row); |
| 1858 | } |
| 1859 | } |
| 1860 |
| 1861 | // Scale Plane to/from any dimensions, without interpolation. |
| 1862 | // Fixed point math is used for performance: The upper 16 bits |
| 1863 | // of x and dx is the integer part of the source position and |
| 1864 | // the lower 16 bits are the fixed decimal part. |
| 1865 |
| 1866 | static void ScalePlaneSimple(int src_width, |
| 1867 | int src_height, |
| 1868 | int dst_width, |
| 1869 | int dst_height, |
| 1870 | int src_stride, |
| 1871 | int dst_stride, |
| 1872 | const uint8_t* src_ptr, |
| 1873 | uint8_t* dst_ptr) { |
| 1874 | int i; |
| 1875 | void (*ScaleCols)(uint8_t * dst_ptr, const uint8_t* src_ptr, int dst_width, |
| 1876 | int x, int dx) = ScaleCols_C; |
| 1877 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1878 | int x = 0; |
| 1879 | int y = 0; |
| 1880 | int dx = 0; |
| 1881 | int dy = 0; |
| 1882 | ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterNone, &x, &y, |
| 1883 | &dx, &dy); |
| 1884 | src_width = Abs(src_width); |
| 1885 |
| 1886 | if (src_width * 2 == dst_width && x < 0x8000) { |
| 1887 | ScaleCols = ScaleColsUp2_C; |
| 1888 | #if defined(HAS_SCALECOLS_SSE2) |
| 1889 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 1890 | ScaleCols = ScaleColsUp2_SSE2; |
| 1891 | } |
| 1892 | #endif |
| 1893 | } |
| 1894 |
| 1895 | for (i = 0; i < dst_height; ++i) { |
| 1896 | ScaleCols(dst_ptr, src_ptr + (y >> 16) * (int64_t)src_stride, dst_width, x, |
| 1897 | dx); |
| 1898 | dst_ptr += dst_stride; |
| 1899 | y += dy; |
| 1900 | } |
| 1901 | } |
| 1902 |
| 1903 | static void ScalePlaneSimple_16(int src_width, |
| 1904 | int src_height, |
| 1905 | int dst_width, |
| 1906 | int dst_height, |
| 1907 | int src_stride, |
| 1908 | int dst_stride, |
| 1909 | const uint16_t* src_ptr, |
| 1910 | uint16_t* dst_ptr) { |
| 1911 | int i; |
| 1912 | void (*ScaleCols)(uint16_t * dst_ptr, const uint16_t* src_ptr, int dst_width, |
| 1913 | int x, int dx) = ScaleCols_16_C; |
| 1914 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1915 | int x = 0; |
| 1916 | int y = 0; |
| 1917 | int dx = 0; |
| 1918 | int dy = 0; |
| 1919 | ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterNone, &x, &y, |
| 1920 | &dx, &dy); |
| 1921 | src_width = Abs(src_width); |
| 1922 |
| 1923 | if (src_width * 2 == dst_width && x < 0x8000) { |
| 1924 | ScaleCols = ScaleColsUp2_16_C; |
| 1925 | #if defined(HAS_SCALECOLS_16_SSE2) |
| 1926 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 1927 | ScaleCols = ScaleColsUp2_16_SSE2; |
| 1928 | } |
| 1929 | #endif |
| 1930 | } |
| 1931 |
| 1932 | for (i = 0; i < dst_height; ++i) { |
| 1933 | ScaleCols(dst_ptr, src_ptr + (y >> 16) * (int64_t)src_stride, dst_width, x, |
| 1934 | dx); |
| 1935 | dst_ptr += dst_stride; |
| 1936 | y += dy; |
| 1937 | } |
| 1938 | } |
| 1939 |
| 1940 | // Scale a plane. |
| 1941 | // This function dispatches to a specialized scaler based on scale factor. |
| 1942 | LIBYUV_API |
| 1943 | void ScalePlane(const uint8_t* src, |
| 1944 | int src_stride, |
| 1945 | int src_width, |
| 1946 | int src_height, |
| 1947 | uint8_t* dst, |
| 1948 | int dst_stride, |
| 1949 | int dst_width, |
| 1950 | int dst_height, |
| 1951 | enum FilterMode filtering) { |
| 1952 | // Simplify filtering when possible. |
| 1953 | filtering = ScaleFilterReduce(src_width, src_height, dst_width, dst_height, |
| 1954 | filtering); |
| 1955 |
| 1956 | // Negative height means invert the image. |
| 1957 | if (src_height < 0) { |
| 1958 | src_height = -src_height; |
| 1959 | src = src + (src_height - 1) * (int64_t)src_stride; |
| 1960 | src_stride = -src_stride; |
| 1961 | } |
| 1962 | // Use specialized scales to improve performance for common resolutions. |
| 1963 | // For example, all the 1/2 scalings will use ScalePlaneDown2() |
| 1964 | if (dst_width == src_width && dst_height == src_height) { |
| 1965 | // Straight copy. |
| 1966 | CopyPlane(src, src_stride, dst, dst_stride, dst_width, dst_height); |
| 1967 | return; |
| 1968 | } |
| 1969 | if (dst_width == src_width && filtering != kFilterBox) { |
| 1970 | int dy = 0; |
| 1971 | int y = 0; |
| 1972 | // When scaling down, use the center 2 rows to filter. |
| 1973 | // When scaling up, last row of destination uses the last 2 source rows. |
| 1974 | if (dst_height <= src_height) { |
| 1975 | dy = FixedDiv(src_height, dst_height); |
| 1976 | y = CENTERSTART(dy, -32768); // Subtract 0.5 (32768) to center filter. |
| 1977 | } else if (src_height > 1 && dst_height > 1) { |
| 1978 | dy = FixedDiv1(src_height, dst_height); |
| 1979 | } |
| 1980 | // Arbitrary scale vertically, but unscaled horizontally. |
| 1981 | ScalePlaneVertical(src_height, dst_width, dst_height, src_stride, |
| 1982 | dst_stride, src, dst, 0, y, dy, /*bpp=*/1, filtering); |
| 1983 | return; |
| 1984 | } |
| 1985 | if (dst_width <= Abs(src_width) && dst_height <= src_height) { |
| 1986 | // Scale down. |
| 1987 | if (4 * dst_width == 3 * src_width && 4 * dst_height == 3 * src_height) { |
| 1988 | // optimized, 3/4 |
| 1989 | ScalePlaneDown34(src_width, src_height, dst_width, dst_height, src_stride, |
| 1990 | dst_stride, src, dst, filtering); |
| 1991 | return; |
| 1992 | } |
| 1993 | if (2 * dst_width == src_width && 2 * dst_height == src_height) { |
| 1994 | // optimized, 1/2 |
| 1995 | ScalePlaneDown2(src_width, src_height, dst_width, dst_height, src_stride, |
| 1996 | dst_stride, src, dst, filtering); |
| 1997 | return; |
| 1998 | } |
| 1999 | // 3/8 rounded up for odd sized chroma height. |
| 2000 | if (8 * dst_width == 3 * src_width && 8 * dst_height == 3 * src_height) { |
| 2001 | // optimized, 3/8 |
| 2002 | ScalePlaneDown38(src_width, src_height, dst_width, dst_height, src_stride, |
| 2003 | dst_stride, src, dst, filtering); |
| 2004 | return; |
| 2005 | } |
| 2006 | if (4 * dst_width == src_width && 4 * dst_height == src_height && |
| 2007 | (filtering == kFilterBox || filtering == kFilterNone)) { |
| 2008 | // optimized, 1/4 |
| 2009 | ScalePlaneDown4(src_width, src_height, dst_width, dst_height, src_stride, |
| 2010 | dst_stride, src, dst, filtering); |
| 2011 | return; |
| 2012 | } |
| 2013 | } |
| 2014 | if (filtering == kFilterBox && dst_height * 2 < src_height) { |
| 2015 | ScalePlaneBox(src_width, src_height, dst_width, dst_height, src_stride, |
| 2016 | dst_stride, src, dst); |
| 2017 | return; |
| 2018 | } |
| 2019 | if ((dst_width + 1) / 2 == src_width && filtering == kFilterLinear) { |
| 2020 | ScalePlaneUp2_Linear(src_width, src_height, dst_width, dst_height, |
| 2021 | src_stride, dst_stride, src, dst); |
| 2022 | return; |
| 2023 | } |
| 2024 | if ((dst_height + 1) / 2 == src_height && (dst_width + 1) / 2 == src_width && |
| 2025 | (filtering == kFilterBilinear || filtering == kFilterBox)) { |
| 2026 | ScalePlaneUp2_Bilinear(src_width, src_height, dst_width, dst_height, |
| 2027 | src_stride, dst_stride, src, dst); |
| 2028 | return; |
| 2029 | } |
| 2030 | if (filtering && dst_height > src_height) { |
| 2031 | ScalePlaneBilinearUp(src_width, src_height, dst_width, dst_height, |
| 2032 | src_stride, dst_stride, src, dst, filtering); |
| 2033 | return; |
| 2034 | } |
| 2035 | if (filtering) { |
| 2036 | ScalePlaneBilinearDown(src_width, src_height, dst_width, dst_height, |
| 2037 | src_stride, dst_stride, src, dst, filtering); |
| 2038 | return; |
| 2039 | } |
| 2040 | ScalePlaneSimple(src_width, src_height, dst_width, dst_height, src_stride, |
| 2041 | dst_stride, src, dst); |
| 2042 | } |
| 2043 |
| 2044 | LIBYUV_API |
| 2045 | void ScalePlane_16(const uint16_t* src, |
| 2046 | int src_stride, |
| 2047 | int src_width, |
| 2048 | int src_height, |
| 2049 | uint16_t* dst, |
| 2050 | int dst_stride, |
| 2051 | int dst_width, |
| 2052 | int dst_height, |
| 2053 | enum FilterMode filtering) { |
| 2054 | // Simplify filtering when possible. |
| 2055 | filtering = ScaleFilterReduce(src_width, src_height, dst_width, dst_height, |
| 2056 | filtering); |
| 2057 |
| 2058 | // Negative height means invert the image. |
| 2059 | if (src_height < 0) { |
| 2060 | src_height = -src_height; |
| 2061 | src = src + (src_height - 1) * (int64_t)src_stride; |
| 2062 | src_stride = -src_stride; |
| 2063 | } |
| 2064 | // Use specialized scales to improve performance for common resolutions. |
| 2065 | // For example, all the 1/2 scalings will use ScalePlaneDown2() |
| 2066 | if (dst_width == src_width && dst_height == src_height) { |
| 2067 | // Straight copy. |
| 2068 | CopyPlane_16(src, src_stride, dst, dst_stride, dst_width, dst_height); |
| 2069 | return; |
| 2070 | } |
| 2071 | if (dst_width == src_width && filtering != kFilterBox) { |
| 2072 | int dy = 0; |
| 2073 | int y = 0; |
| 2074 | // When scaling down, use the center 2 rows to filter. |
| 2075 | // When scaling up, last row of destination uses the last 2 source rows. |
| 2076 | if (dst_height <= src_height) { |
| 2077 | dy = FixedDiv(src_height, dst_height); |
| 2078 | y = CENTERSTART(dy, -32768); // Subtract 0.5 (32768) to center filter. |
| 2079 | // When scaling up, ensure the last row of destination uses the last |
| 2080 | // source. Avoid divide by zero for dst_height but will do no scaling |
| 2081 | // later. |
| 2082 | } else if (src_height > 1 && dst_height > 1) { |
| 2083 | dy = FixedDiv1(src_height, dst_height); |
| 2084 | } |
| 2085 | // Arbitrary scale vertically, but unscaled horizontally. |
| 2086 | ScalePlaneVertical_16(src_height, dst_width, dst_height, src_stride, |
| 2087 | dst_stride, src, dst, 0, y, dy, /*bpp=*/1, filtering); |
| 2088 | return; |
| 2089 | } |
| 2090 | if (dst_width <= Abs(src_width) && dst_height <= src_height) { |
| 2091 | // Scale down. |
| 2092 | if (4 * dst_width == 3 * src_width && 4 * dst_height == 3 * src_height) { |
| 2093 | // optimized, 3/4 |
| 2094 | ScalePlaneDown34_16(src_width, src_height, dst_width, dst_height, |
| 2095 | src_stride, dst_stride, src, dst, filtering); |
| 2096 | return; |
| 2097 | } |
| 2098 | if (2 * dst_width == src_width && 2 * dst_height == src_height) { |
| 2099 | // optimized, 1/2 |
| 2100 | ScalePlaneDown2_16(src_width, src_height, dst_width, dst_height, |
| 2101 | src_stride, dst_stride, src, dst, filtering); |
| 2102 | return; |
| 2103 | } |
| 2104 | // 3/8 rounded up for odd sized chroma height. |
| 2105 | if (8 * dst_width == 3 * src_width && 8 * dst_height == 3 * src_height) { |
| 2106 | // optimized, 3/8 |
| 2107 | ScalePlaneDown38_16(src_width, src_height, dst_width, dst_height, |
| 2108 | src_stride, dst_stride, src, dst, filtering); |
| 2109 | return; |
| 2110 | } |
| 2111 | if (4 * dst_width == src_width && 4 * dst_height == src_height && |
| 2112 | (filtering == kFilterBox || filtering == kFilterNone)) { |
| 2113 | // optimized, 1/4 |
| 2114 | ScalePlaneDown4_16(src_width, src_height, dst_width, dst_height, |
| 2115 | src_stride, dst_stride, src, dst, filtering); |
| 2116 | return; |
| 2117 | } |
| 2118 | } |
| 2119 | if (filtering == kFilterBox && dst_height * 2 < src_height) { |
| 2120 | ScalePlaneBox_16(src_width, src_height, dst_width, dst_height, src_stride, |
| 2121 | dst_stride, src, dst); |
| 2122 | return; |
| 2123 | } |
| 2124 | if ((dst_width + 1) / 2 == src_width && filtering == kFilterLinear) { |
| 2125 | ScalePlaneUp2_16_Linear(src_width, src_height, dst_width, dst_height, |
| 2126 | src_stride, dst_stride, src, dst); |
| 2127 | return; |
| 2128 | } |
| 2129 | if ((dst_height + 1) / 2 == src_height && (dst_width + 1) / 2 == src_width && |
| 2130 | (filtering == kFilterBilinear || filtering == kFilterBox)) { |
| 2131 | ScalePlaneUp2_16_Bilinear(src_width, src_height, dst_width, dst_height, |
| 2132 | src_stride, dst_stride, src, dst); |
| 2133 | return; |
| 2134 | } |
| 2135 | if (filtering && dst_height > src_height) { |
| 2136 | ScalePlaneBilinearUp_16(src_width, src_height, dst_width, dst_height, |
| 2137 | src_stride, dst_stride, src, dst, filtering); |
| 2138 | return; |
| 2139 | } |
| 2140 | if (filtering) { |
| 2141 | ScalePlaneBilinearDown_16(src_width, src_height, dst_width, dst_height, |
| 2142 | src_stride, dst_stride, src, dst, filtering); |
| 2143 | return; |
| 2144 | } |
| 2145 | ScalePlaneSimple_16(src_width, src_height, dst_width, dst_height, src_stride, |
| 2146 | dst_stride, src, dst); |
| 2147 | } |
| 2148 |
| 2149 | LIBYUV_API |
| 2150 | void ScalePlane_12(const uint16_t* src, |
| 2151 | int src_stride, |
| 2152 | int src_width, |
| 2153 | int src_height, |
| 2154 | uint16_t* dst, |
| 2155 | int dst_stride, |
| 2156 | int dst_width, |
| 2157 | int dst_height, |
| 2158 | enum FilterMode filtering) { |
| 2159 | // Simplify filtering when possible. |
| 2160 | filtering = ScaleFilterReduce(src_width, src_height, dst_width, dst_height, |
| 2161 | filtering); |
| 2162 |
| 2163 | // Negative height means invert the image. |
| 2164 | if (src_height < 0) { |
| 2165 | src_height = -src_height; |
| 2166 | src = src + (src_height - 1) * (int64_t)src_stride; |
| 2167 | src_stride = -src_stride; |
| 2168 | } |
| 2169 |
| 2170 | if ((dst_width + 1) / 2 == src_width && filtering == kFilterLinear) { |
| 2171 | ScalePlaneUp2_12_Linear(src_width, src_height, dst_width, dst_height, |
| 2172 | src_stride, dst_stride, src, dst); |
| 2173 | return; |
| 2174 | } |
| 2175 | if ((dst_height + 1) / 2 == src_height && (dst_width + 1) / 2 == src_width && |
| 2176 | (filtering == kFilterBilinear || filtering == kFilterBox)) { |
| 2177 | ScalePlaneUp2_12_Bilinear(src_width, src_height, dst_width, dst_height, |
| 2178 | src_stride, dst_stride, src, dst); |
| 2179 | return; |
| 2180 | } |
| 2181 |
| 2182 | ScalePlane_16(src, src_stride, src_width, src_height, dst, dst_stride, |
| 2183 | dst_width, dst_height, filtering); |
| 2184 | } |
| 2185 |
| 2186 | // Scale an I420 image. |
| 2187 | // This function in turn calls a scaling function for each plane. |
| 2188 |
| 2189 | LIBYUV_API |
| 2190 | int I420Scale(const uint8_t* src_y, |
| 2191 | int src_stride_y, |
| 2192 | const uint8_t* src_u, |
| 2193 | int src_stride_u, |
| 2194 | const uint8_t* src_v, |
| 2195 | int src_stride_v, |
| 2196 | int src_width, |
| 2197 | int src_height, |
| 2198 | uint8_t* dst_y, |
| 2199 | int dst_stride_y, |
| 2200 | uint8_t* dst_u, |
| 2201 | int dst_stride_u, |
| 2202 | uint8_t* dst_v, |
| 2203 | int dst_stride_v, |
| 2204 | int dst_width, |
| 2205 | int dst_height, |
| 2206 | enum FilterMode filtering) { |
| 2207 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 2208 | int src_halfheight = SUBSAMPLE(src_height, 1, 1); |
| 2209 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 2210 | int dst_halfheight = SUBSAMPLE(dst_height, 1, 1); |
| 2211 |
| 2212 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 2213 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 2214 | dst_width <= 0 || dst_height <= 0) { |
| 2215 | return -1; |
| 2216 | } |
| 2217 |
| 2218 | ScalePlane(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2219 | dst_width, dst_height, filtering); |
| 2220 | ScalePlane(src_u, src_stride_u, src_halfwidth, src_halfheight, dst_u, |
| 2221 | dst_stride_u, dst_halfwidth, dst_halfheight, filtering); |
| 2222 | ScalePlane(src_v, src_stride_v, src_halfwidth, src_halfheight, dst_v, |
| 2223 | dst_stride_v, dst_halfwidth, dst_halfheight, filtering); |
| 2224 | return 0; |
| 2225 | } |
| 2226 |
| 2227 | LIBYUV_API |
| 2228 | int I420Scale_16(const uint16_t* src_y, |
| 2229 | int src_stride_y, |
| 2230 | const uint16_t* src_u, |
| 2231 | int src_stride_u, |
| 2232 | const uint16_t* src_v, |
| 2233 | int src_stride_v, |
| 2234 | int src_width, |
| 2235 | int src_height, |
| 2236 | uint16_t* dst_y, |
| 2237 | int dst_stride_y, |
| 2238 | uint16_t* dst_u, |
| 2239 | int dst_stride_u, |
| 2240 | uint16_t* dst_v, |
| 2241 | int dst_stride_v, |
| 2242 | int dst_width, |
| 2243 | int dst_height, |
| 2244 | enum FilterMode filtering) { |
| 2245 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 2246 | int src_halfheight = SUBSAMPLE(src_height, 1, 1); |
| 2247 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 2248 | int dst_halfheight = SUBSAMPLE(dst_height, 1, 1); |
| 2249 |
| 2250 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 2251 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 2252 | dst_width <= 0 || dst_height <= 0) { |
| 2253 | return -1; |
| 2254 | } |
| 2255 |
| 2256 | ScalePlane_16(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2257 | dst_width, dst_height, filtering); |
| 2258 | ScalePlane_16(src_u, src_stride_u, src_halfwidth, src_halfheight, dst_u, |
| 2259 | dst_stride_u, dst_halfwidth, dst_halfheight, filtering); |
| 2260 | ScalePlane_16(src_v, src_stride_v, src_halfwidth, src_halfheight, dst_v, |
| 2261 | dst_stride_v, dst_halfwidth, dst_halfheight, filtering); |
| 2262 | return 0; |
| 2263 | } |
| 2264 |
| 2265 | LIBYUV_API |
| 2266 | int I420Scale_12(const uint16_t* src_y, |
| 2267 | int src_stride_y, |
| 2268 | const uint16_t* src_u, |
| 2269 | int src_stride_u, |
| 2270 | const uint16_t* src_v, |
| 2271 | int src_stride_v, |
| 2272 | int src_width, |
| 2273 | int src_height, |
| 2274 | uint16_t* dst_y, |
| 2275 | int dst_stride_y, |
| 2276 | uint16_t* dst_u, |
| 2277 | int dst_stride_u, |
| 2278 | uint16_t* dst_v, |
| 2279 | int dst_stride_v, |
| 2280 | int dst_width, |
| 2281 | int dst_height, |
| 2282 | enum FilterMode filtering) { |
| 2283 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 2284 | int src_halfheight = SUBSAMPLE(src_height, 1, 1); |
| 2285 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 2286 | int dst_halfheight = SUBSAMPLE(dst_height, 1, 1); |
| 2287 |
| 2288 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 2289 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 2290 | dst_width <= 0 || dst_height <= 0) { |
| 2291 | return -1; |
| 2292 | } |
| 2293 |
| 2294 | ScalePlane_12(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2295 | dst_width, dst_height, filtering); |
| 2296 | ScalePlane_12(src_u, src_stride_u, src_halfwidth, src_halfheight, dst_u, |
| 2297 | dst_stride_u, dst_halfwidth, dst_halfheight, filtering); |
| 2298 | ScalePlane_12(src_v, src_stride_v, src_halfwidth, src_halfheight, dst_v, |
| 2299 | dst_stride_v, dst_halfwidth, dst_halfheight, filtering); |
| 2300 | return 0; |
| 2301 | } |
| 2302 |
| 2303 | // Scale an I444 image. |
| 2304 | // This function in turn calls a scaling function for each plane. |
| 2305 |
| 2306 | LIBYUV_API |
| 2307 | int I444Scale(const uint8_t* src_y, |
| 2308 | int src_stride_y, |
| 2309 | const uint8_t* src_u, |
| 2310 | int src_stride_u, |
| 2311 | const uint8_t* src_v, |
| 2312 | int src_stride_v, |
| 2313 | int src_width, |
| 2314 | int src_height, |
| 2315 | uint8_t* dst_y, |
| 2316 | int dst_stride_y, |
| 2317 | uint8_t* dst_u, |
| 2318 | int dst_stride_u, |
| 2319 | uint8_t* dst_v, |
| 2320 | int dst_stride_v, |
| 2321 | int dst_width, |
| 2322 | int dst_height, |
| 2323 | enum FilterMode filtering) { |
| 2324 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 2325 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 2326 | dst_width <= 0 || dst_height <= 0) { |
| 2327 | return -1; |
| 2328 | } |
| 2329 |
| 2330 | ScalePlane(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2331 | dst_width, dst_height, filtering); |
| 2332 | ScalePlane(src_u, src_stride_u, src_width, src_height, dst_u, dst_stride_u, |
| 2333 | dst_width, dst_height, filtering); |
| 2334 | ScalePlane(src_v, src_stride_v, src_width, src_height, dst_v, dst_stride_v, |
| 2335 | dst_width, dst_height, filtering); |
| 2336 | return 0; |
| 2337 | } |
| 2338 |
| 2339 | LIBYUV_API |
| 2340 | int I444Scale_16(const uint16_t* src_y, |
| 2341 | int src_stride_y, |
| 2342 | const uint16_t* src_u, |
| 2343 | int src_stride_u, |
| 2344 | const uint16_t* src_v, |
| 2345 | int src_stride_v, |
| 2346 | int src_width, |
| 2347 | int src_height, |
| 2348 | uint16_t* dst_y, |
| 2349 | int dst_stride_y, |
| 2350 | uint16_t* dst_u, |
| 2351 | int dst_stride_u, |
| 2352 | uint16_t* dst_v, |
| 2353 | int dst_stride_v, |
| 2354 | int dst_width, |
| 2355 | int dst_height, |
| 2356 | enum FilterMode filtering) { |
| 2357 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 2358 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 2359 | dst_width <= 0 || dst_height <= 0) { |
| 2360 | return -1; |
| 2361 | } |
| 2362 |
| 2363 | ScalePlane_16(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2364 | dst_width, dst_height, filtering); |
| 2365 | ScalePlane_16(src_u, src_stride_u, src_width, src_height, dst_u, dst_stride_u, |
| 2366 | dst_width, dst_height, filtering); |
| 2367 | ScalePlane_16(src_v, src_stride_v, src_width, src_height, dst_v, dst_stride_v, |
| 2368 | dst_width, dst_height, filtering); |
| 2369 | return 0; |
| 2370 | } |
| 2371 |
| 2372 | LIBYUV_API |
| 2373 | int I444Scale_12(const uint16_t* src_y, |
| 2374 | int src_stride_y, |
| 2375 | const uint16_t* src_u, |
| 2376 | int src_stride_u, |
| 2377 | const uint16_t* src_v, |
| 2378 | int src_stride_v, |
| 2379 | int src_width, |
| 2380 | int src_height, |
| 2381 | uint16_t* dst_y, |
| 2382 | int dst_stride_y, |
| 2383 | uint16_t* dst_u, |
| 2384 | int dst_stride_u, |
| 2385 | uint16_t* dst_v, |
| 2386 | int dst_stride_v, |
| 2387 | int dst_width, |
| 2388 | int dst_height, |
| 2389 | enum FilterMode filtering) { |
| 2390 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 2391 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 2392 | dst_width <= 0 || dst_height <= 0) { |
| 2393 | return -1; |
| 2394 | } |
| 2395 |
| 2396 | ScalePlane_12(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2397 | dst_width, dst_height, filtering); |
| 2398 | ScalePlane_12(src_u, src_stride_u, src_width, src_height, dst_u, dst_stride_u, |
| 2399 | dst_width, dst_height, filtering); |
| 2400 | ScalePlane_12(src_v, src_stride_v, src_width, src_height, dst_v, dst_stride_v, |
| 2401 | dst_width, dst_height, filtering); |
| 2402 | return 0; |
| 2403 | } |
| 2404 |
| 2405 | // Scale an I422 image. |
| 2406 | // This function in turn calls a scaling function for each plane. |
| 2407 |
| 2408 | LIBYUV_API |
| 2409 | int I422Scale(const uint8_t* src_y, |
| 2410 | int src_stride_y, |
| 2411 | const uint8_t* src_u, |
| 2412 | int src_stride_u, |
| 2413 | const uint8_t* src_v, |
| 2414 | int src_stride_v, |
| 2415 | int src_width, |
| 2416 | int src_height, |
| 2417 | uint8_t* dst_y, |
| 2418 | int dst_stride_y, |
| 2419 | uint8_t* dst_u, |
| 2420 | int dst_stride_u, |
| 2421 | uint8_t* dst_v, |
| 2422 | int dst_stride_v, |
| 2423 | int dst_width, |
| 2424 | int dst_height, |
| 2425 | enum FilterMode filtering) { |
| 2426 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 2427 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 2428 |
| 2429 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 2430 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 2431 | dst_width <= 0 || dst_height <= 0) { |
| 2432 | return -1; |
| 2433 | } |
| 2434 |
| 2435 | ScalePlane(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2436 | dst_width, dst_height, filtering); |
| 2437 | ScalePlane(src_u, src_stride_u, src_halfwidth, src_height, dst_u, |
| 2438 | dst_stride_u, dst_halfwidth, dst_height, filtering); |
| 2439 | ScalePlane(src_v, src_stride_v, src_halfwidth, src_height, dst_v, |
| 2440 | dst_stride_v, dst_halfwidth, dst_height, filtering); |
| 2441 | return 0; |
| 2442 | } |
| 2443 |
| 2444 | LIBYUV_API |
| 2445 | int I422Scale_16(const uint16_t* src_y, |
| 2446 | int src_stride_y, |
| 2447 | const uint16_t* src_u, |
| 2448 | int src_stride_u, |
| 2449 | const uint16_t* src_v, |
| 2450 | int src_stride_v, |
| 2451 | int src_width, |
| 2452 | int src_height, |
| 2453 | uint16_t* dst_y, |
| 2454 | int dst_stride_y, |
| 2455 | uint16_t* dst_u, |
| 2456 | int dst_stride_u, |
| 2457 | uint16_t* dst_v, |
| 2458 | int dst_stride_v, |
| 2459 | int dst_width, |
| 2460 | int dst_height, |
| 2461 | enum FilterMode filtering) { |
| 2462 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 2463 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 2464 |
| 2465 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 2466 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 2467 | dst_width <= 0 || dst_height <= 0) { |
| 2468 | return -1; |
| 2469 | } |
| 2470 |
| 2471 | ScalePlane_16(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2472 | dst_width, dst_height, filtering); |
| 2473 | ScalePlane_16(src_u, src_stride_u, src_halfwidth, src_height, dst_u, |
| 2474 | dst_stride_u, dst_halfwidth, dst_height, filtering); |
| 2475 | ScalePlane_16(src_v, src_stride_v, src_halfwidth, src_height, dst_v, |
| 2476 | dst_stride_v, dst_halfwidth, dst_height, filtering); |
| 2477 | return 0; |
| 2478 | } |
| 2479 |
| 2480 | LIBYUV_API |
| 2481 | int I422Scale_12(const uint16_t* src_y, |
| 2482 | int src_stride_y, |
| 2483 | const uint16_t* src_u, |
| 2484 | int src_stride_u, |
| 2485 | const uint16_t* src_v, |
| 2486 | int src_stride_v, |
| 2487 | int src_width, |
| 2488 | int src_height, |
| 2489 | uint16_t* dst_y, |
| 2490 | int dst_stride_y, |
| 2491 | uint16_t* dst_u, |
| 2492 | int dst_stride_u, |
| 2493 | uint16_t* dst_v, |
| 2494 | int dst_stride_v, |
| 2495 | int dst_width, |
| 2496 | int dst_height, |
| 2497 | enum FilterMode filtering) { |
| 2498 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 2499 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 2500 |
| 2501 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 2502 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 2503 | dst_width <= 0 || dst_height <= 0) { |
| 2504 | return -1; |
| 2505 | } |
| 2506 |
| 2507 | ScalePlane_12(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2508 | dst_width, dst_height, filtering); |
| 2509 | ScalePlane_12(src_u, src_stride_u, src_halfwidth, src_height, dst_u, |
| 2510 | dst_stride_u, dst_halfwidth, dst_height, filtering); |
| 2511 | ScalePlane_12(src_v, src_stride_v, src_halfwidth, src_height, dst_v, |
| 2512 | dst_stride_v, dst_halfwidth, dst_height, filtering); |
| 2513 | return 0; |
| 2514 | } |
| 2515 |
| 2516 | // Scale an NV12 image. |
| 2517 | // This function in turn calls a scaling function for each plane. |
| 2518 |
| 2519 | LIBYUV_API |
| 2520 | int NV12Scale(const uint8_t* src_y, |
| 2521 | int src_stride_y, |
| 2522 | const uint8_t* src_uv, |
| 2523 | int src_stride_uv, |
| 2524 | int src_width, |
| 2525 | int src_height, |
| 2526 | uint8_t* dst_y, |
| 2527 | int dst_stride_y, |
| 2528 | uint8_t* dst_uv, |
| 2529 | int dst_stride_uv, |
| 2530 | int dst_width, |
| 2531 | int dst_height, |
| 2532 | enum FilterMode filtering) { |
| 2533 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 2534 | int src_halfheight = SUBSAMPLE(src_height, 1, 1); |
| 2535 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 2536 | int dst_halfheight = SUBSAMPLE(dst_height, 1, 1); |
| 2537 |
| 2538 | if (!src_y || !src_uv || src_width <= 0 || src_height == 0 || |
| 2539 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_uv || |
| 2540 | dst_width <= 0 || dst_height <= 0) { |
| 2541 | return -1; |
| 2542 | } |
| 2543 |
| 2544 | ScalePlane(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y, |
| 2545 | dst_width, dst_height, filtering); |
| 2546 | UVScale(src_uv, src_stride_uv, src_halfwidth, src_halfheight, dst_uv, |
| 2547 | dst_stride_uv, dst_halfwidth, dst_halfheight, filtering); |
| 2548 | return 0; |
| 2549 | } |
| 2550 |
| 2551 | // Deprecated api |
| 2552 | LIBYUV_API |
| 2553 | int Scale(const uint8_t* src_y, |
| 2554 | const uint8_t* src_u, |
| 2555 | const uint8_t* src_v, |
| 2556 | int src_stride_y, |
| 2557 | int src_stride_u, |
| 2558 | int src_stride_v, |
| 2559 | int src_width, |
| 2560 | int src_height, |
| 2561 | uint8_t* dst_y, |
| 2562 | uint8_t* dst_u, |
| 2563 | uint8_t* dst_v, |
| 2564 | int dst_stride_y, |
| 2565 | int dst_stride_u, |
| 2566 | int dst_stride_v, |
| 2567 | int dst_width, |
| 2568 | int dst_height, |
| 2569 | LIBYUV_BOOL interpolate) { |
| 2570 | return I420Scale(src_y, src_stride_y, src_u, src_stride_u, src_v, |
| 2571 | src_stride_v, src_width, src_height, dst_y, dst_stride_y, |
| 2572 | dst_u, dst_stride_u, dst_v, dst_stride_v, dst_width, |
| 2573 | dst_height, interpolate ? kFilterBox : kFilterNone); |
| 2574 | } |
| 2575 |
| 2576 | #ifdef __cplusplus |
| 2577 | } // extern "C" |
| 2578 | } // namespace libyuv |
| 2579 | #endif |
| 2580 |