| 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 |
| 21 | #ifdef __cplusplus |
| 22 | namespace libyuv { |
| 23 | extern "C" { |
| 24 | #endif |
| 25 |
| 26 | static __inline int Abs(int v) { |
| 27 | return v >= 0 ? v : -v; |
| 28 | } |
| 29 |
| 30 | #define SUBSAMPLE(v, a, s) (v < 0) ? (-((-v + a) >> s)) : ((v + a) >> s) |
| 31 |
| 32 | // Scale plane, 1/2 |
| 33 | // This is an optimized version for scaling down a plane to 1/2 of |
| 34 | // its original size. |
| 35 |
| 36 | static void ScalePlaneDown2(int src_width, int src_height, |
| 37 | int dst_width, int dst_height, |
| 38 | int src_stride, int dst_stride, |
| 39 | const uint8* src_ptr, uint8* dst_ptr, |
| 40 | enum FilterMode filtering) { |
| 41 | int y; |
| 42 | void (*ScaleRowDown2)(const uint8* src_ptr, ptrdiff_t src_stride, |
| 43 | uint8* dst_ptr, int dst_width) = |
| 44 | filtering == kFilterNone ? ScaleRowDown2_C : |
| 45 | (filtering == kFilterLinear ? ScaleRowDown2Linear_C : ScaleRowDown2Box_C); |
| 46 | int row_stride = src_stride << 1; |
| 47 | if (!filtering) { |
| 48 | src_ptr += src_stride; // Point to odd rows. |
| 49 | src_stride = 0; |
| 50 | } |
| 51 |
| 52 | #if defined(HAS_SCALEROWDOWN2_NEON) |
| 53 | if (TestCpuFlag(kCpuHasNEON)) { |
| 54 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_Any_NEON : |
| 55 | (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_NEON : |
| 56 | ScaleRowDown2Box_Any_NEON); |
| 57 | if (IS_ALIGNED(dst_width, 16)) { |
| 58 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_NEON : |
| 59 | (filtering == kFilterLinear ? ScaleRowDown2Linear_NEON : |
| 60 | ScaleRowDown2Box_NEON); |
| 61 | } |
| 62 | } |
| 63 | #endif |
| 64 | #if defined(HAS_SCALEROWDOWN2_SSSE3) |
| 65 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 66 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_Any_SSSE3 : |
| 67 | (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_SSSE3 : |
| 68 | ScaleRowDown2Box_Any_SSSE3); |
| 69 | if (IS_ALIGNED(dst_width, 16)) { |
| 70 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_SSSE3 : |
| 71 | (filtering == kFilterLinear ? ScaleRowDown2Linear_SSSE3 : |
| 72 | ScaleRowDown2Box_SSSE3); |
| 73 | } |
| 74 | } |
| 75 | #endif |
| 76 | #if defined(HAS_SCALEROWDOWN2_AVX2) |
| 77 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 78 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_Any_AVX2 : |
| 79 | (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_AVX2 : |
| 80 | ScaleRowDown2Box_Any_AVX2); |
| 81 | if (IS_ALIGNED(dst_width, 32)) { |
| 82 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_AVX2 : |
| 83 | (filtering == kFilterLinear ? ScaleRowDown2Linear_AVX2 : |
| 84 | ScaleRowDown2Box_AVX2); |
| 85 | } |
| 86 | } |
| 87 | #endif |
| 88 | #if defined(HAS_SCALEROWDOWN2_DSPR2) |
| 89 | if (TestCpuFlag(kCpuHasDSPR2) && IS_ALIGNED(src_ptr, 4) && |
| 90 | IS_ALIGNED(src_stride, 4) && IS_ALIGNED(row_stride, 4) && |
| 91 | IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { |
| 92 | ScaleRowDown2 = filtering ? |
| 93 | ScaleRowDown2Box_DSPR2 : ScaleRowDown2_DSPR2; |
| 94 | } |
| 95 | #endif |
| 96 |
| 97 | if (filtering == kFilterLinear) { |
| 98 | src_stride = 0; |
| 99 | } |
| 100 | // TODO(fbarchard): Loop through source height to allow odd height. |
| 101 | for (y = 0; y < dst_height; ++y) { |
| 102 | ScaleRowDown2(src_ptr, src_stride, dst_ptr, dst_width); |
| 103 | src_ptr += row_stride; |
| 104 | dst_ptr += dst_stride; |
| 105 | } |
| 106 | } |
| 107 |
| 108 | static void ScalePlaneDown2_16(int src_width, int src_height, |
| 109 | int dst_width, int dst_height, |
| 110 | int src_stride, int dst_stride, |
| 111 | const uint16* src_ptr, uint16* dst_ptr, |
| 112 | enum FilterMode filtering) { |
| 113 | int y; |
| 114 | void (*ScaleRowDown2)(const uint16* src_ptr, ptrdiff_t src_stride, |
| 115 | uint16* dst_ptr, int dst_width) = |
| 116 | filtering == kFilterNone ? ScaleRowDown2_16_C : |
| 117 | (filtering == kFilterLinear ? ScaleRowDown2Linear_16_C : |
| 118 | ScaleRowDown2Box_16_C); |
| 119 | int row_stride = src_stride << 1; |
| 120 | if (!filtering) { |
| 121 | src_ptr += src_stride; // Point to odd rows. |
| 122 | src_stride = 0; |
| 123 | } |
| 124 |
| 125 | #if defined(HAS_SCALEROWDOWN2_16_NEON) |
| 126 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 16)) { |
| 127 | ScaleRowDown2 = filtering ? ScaleRowDown2Box_16_NEON : |
| 128 | ScaleRowDown2_16_NEON; |
| 129 | } |
| 130 | #endif |
| 131 | #if defined(HAS_SCALEROWDOWN2_16_SSE2) |
| 132 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 16)) { |
| 133 | ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_16_SSE2 : |
| 134 | (filtering == kFilterLinear ? ScaleRowDown2Linear_16_SSE2 : |
| 135 | ScaleRowDown2Box_16_SSE2); |
| 136 | } |
| 137 | #endif |
| 138 | #if defined(HAS_SCALEROWDOWN2_16_DSPR2) |
| 139 | if (TestCpuFlag(kCpuHasDSPR2) && IS_ALIGNED(src_ptr, 4) && |
| 140 | IS_ALIGNED(src_stride, 4) && IS_ALIGNED(row_stride, 4) && |
| 141 | IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { |
| 142 | ScaleRowDown2 = filtering ? |
| 143 | ScaleRowDown2Box_16_DSPR2 : ScaleRowDown2_16_DSPR2; |
| 144 | } |
| 145 | #endif |
| 146 |
| 147 | if (filtering == kFilterLinear) { |
| 148 | src_stride = 0; |
| 149 | } |
| 150 | // TODO(fbarchard): Loop through source height to allow odd height. |
| 151 | for (y = 0; y < dst_height; ++y) { |
| 152 | ScaleRowDown2(src_ptr, src_stride, dst_ptr, dst_width); |
| 153 | src_ptr += row_stride; |
| 154 | dst_ptr += dst_stride; |
| 155 | } |
| 156 | } |
| 157 |
| 158 | // Scale plane, 1/4 |
| 159 | // This is an optimized version for scaling down a plane to 1/4 of |
| 160 | // its original size. |
| 161 |
| 162 | static void ScalePlaneDown4(int src_width, int src_height, |
| 163 | int dst_width, int dst_height, |
| 164 | int src_stride, int dst_stride, |
| 165 | const uint8* src_ptr, uint8* dst_ptr, |
| 166 | enum FilterMode filtering) { |
| 167 | int y; |
| 168 | void (*ScaleRowDown4)(const uint8* src_ptr, ptrdiff_t src_stride, |
| 169 | uint8* dst_ptr, int dst_width) = |
| 170 | filtering ? ScaleRowDown4Box_C : ScaleRowDown4_C; |
| 171 | int row_stride = src_stride << 2; |
| 172 | if (!filtering) { |
| 173 | src_ptr += src_stride * 2; // Point to row 2. |
| 174 | src_stride = 0; |
| 175 | } |
| 176 | #if defined(HAS_SCALEROWDOWN4_NEON) |
| 177 | if (TestCpuFlag(kCpuHasNEON)) { |
| 178 | ScaleRowDown4 = filtering ? |
| 179 | ScaleRowDown4Box_Any_NEON : ScaleRowDown4_Any_NEON; |
| 180 | if (IS_ALIGNED(dst_width, 8)) { |
| 181 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_NEON : ScaleRowDown4_NEON; |
| 182 | } |
| 183 | } |
| 184 | #endif |
| 185 | #if defined(HAS_SCALEROWDOWN4_SSSE3) |
| 186 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 187 | ScaleRowDown4 = filtering ? |
| 188 | ScaleRowDown4Box_Any_SSSE3 : ScaleRowDown4_Any_SSSE3; |
| 189 | if (IS_ALIGNED(dst_width, 8)) { |
| 190 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_SSSE3 : ScaleRowDown4_SSSE3; |
| 191 | } |
| 192 | } |
| 193 | #endif |
| 194 | #if defined(HAS_SCALEROWDOWN4_AVX2) |
| 195 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 196 | ScaleRowDown4 = filtering ? |
| 197 | ScaleRowDown4Box_Any_AVX2 : ScaleRowDown4_Any_AVX2; |
| 198 | if (IS_ALIGNED(dst_width, 16)) { |
| 199 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_AVX2 : ScaleRowDown4_AVX2; |
| 200 | } |
| 201 | } |
| 202 | #endif |
| 203 | #if defined(HAS_SCALEROWDOWN4_DSPR2) |
| 204 | if (TestCpuFlag(kCpuHasDSPR2) && IS_ALIGNED(row_stride, 4) && |
| 205 | IS_ALIGNED(src_ptr, 4) && IS_ALIGNED(src_stride, 4) && |
| 206 | IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { |
| 207 | ScaleRowDown4 = filtering ? |
| 208 | ScaleRowDown4Box_DSPR2 : ScaleRowDown4_DSPR2; |
| 209 | } |
| 210 | #endif |
| 211 |
| 212 | if (filtering == kFilterLinear) { |
| 213 | src_stride = 0; |
| 214 | } |
| 215 | for (y = 0; y < dst_height; ++y) { |
| 216 | ScaleRowDown4(src_ptr, src_stride, dst_ptr, dst_width); |
| 217 | src_ptr += row_stride; |
| 218 | dst_ptr += dst_stride; |
| 219 | } |
| 220 | } |
| 221 |
| 222 | static void ScalePlaneDown4_16(int src_width, int src_height, |
| 223 | int dst_width, int dst_height, |
| 224 | int src_stride, int dst_stride, |
| 225 | const uint16* src_ptr, uint16* dst_ptr, |
| 226 | enum FilterMode filtering) { |
| 227 | int y; |
| 228 | void (*ScaleRowDown4)(const uint16* src_ptr, ptrdiff_t src_stride, |
| 229 | uint16* dst_ptr, int dst_width) = |
| 230 | filtering ? ScaleRowDown4Box_16_C : ScaleRowDown4_16_C; |
| 231 | int row_stride = src_stride << 2; |
| 232 | if (!filtering) { |
| 233 | src_ptr += src_stride * 2; // Point to row 2. |
| 234 | src_stride = 0; |
| 235 | } |
| 236 | #if defined(HAS_SCALEROWDOWN4_16_NEON) |
| 237 | if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 8)) { |
| 238 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_16_NEON : |
| 239 | ScaleRowDown4_16_NEON; |
| 240 | } |
| 241 | #endif |
| 242 | #if defined(HAS_SCALEROWDOWN4_16_SSE2) |
| 243 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 244 | ScaleRowDown4 = filtering ? ScaleRowDown4Box_16_SSE2 : |
| 245 | ScaleRowDown4_16_SSE2; |
| 246 | } |
| 247 | #endif |
| 248 | #if defined(HAS_SCALEROWDOWN4_16_DSPR2) |
| 249 | if (TestCpuFlag(kCpuHasDSPR2) && IS_ALIGNED(row_stride, 4) && |
| 250 | IS_ALIGNED(src_ptr, 4) && IS_ALIGNED(src_stride, 4) && |
| 251 | IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { |
| 252 | ScaleRowDown4 = filtering ? |
| 253 | ScaleRowDown4Box_16_DSPR2 : ScaleRowDown4_16_DSPR2; |
| 254 | } |
| 255 | #endif |
| 256 |
| 257 | if (filtering == kFilterLinear) { |
| 258 | src_stride = 0; |
| 259 | } |
| 260 | for (y = 0; y < dst_height; ++y) { |
| 261 | ScaleRowDown4(src_ptr, src_stride, dst_ptr, dst_width); |
| 262 | src_ptr += row_stride; |
| 263 | dst_ptr += dst_stride; |
| 264 | } |
| 265 | } |
| 266 |
| 267 | // Scale plane down, 3/4 |
| 268 |
| 269 | static void ScalePlaneDown34(int src_width, int src_height, |
| 270 | int dst_width, int dst_height, |
| 271 | int src_stride, int dst_stride, |
| 272 | const uint8* src_ptr, uint8* dst_ptr, |
| 273 | enum FilterMode filtering) { |
| 274 | int y; |
| 275 | void (*ScaleRowDown34_0)(const uint8* src_ptr, ptrdiff_t src_stride, |
| 276 | uint8* dst_ptr, int dst_width); |
| 277 | void (*ScaleRowDown34_1)(const uint8* src_ptr, ptrdiff_t src_stride, |
| 278 | uint8* dst_ptr, int dst_width); |
| 279 | const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; |
| 280 | assert(dst_width % 3 == 0); |
| 281 | if (!filtering) { |
| 282 | ScaleRowDown34_0 = ScaleRowDown34_C; |
| 283 | ScaleRowDown34_1 = ScaleRowDown34_C; |
| 284 | } else { |
| 285 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_C; |
| 286 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_C; |
| 287 | } |
| 288 | #if defined(HAS_SCALEROWDOWN34_NEON) |
| 289 | if (TestCpuFlag(kCpuHasNEON)) { |
| 290 | if (!filtering) { |
| 291 | ScaleRowDown34_0 = ScaleRowDown34_Any_NEON; |
| 292 | ScaleRowDown34_1 = ScaleRowDown34_Any_NEON; |
| 293 | } else { |
| 294 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_Any_NEON; |
| 295 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_Any_NEON; |
| 296 | } |
| 297 | if (dst_width % 24 == 0) { |
| 298 | if (!filtering) { |
| 299 | ScaleRowDown34_0 = ScaleRowDown34_NEON; |
| 300 | ScaleRowDown34_1 = ScaleRowDown34_NEON; |
| 301 | } else { |
| 302 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_NEON; |
| 303 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_NEON; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | #endif |
| 308 | #if defined(HAS_SCALEROWDOWN34_SSSE3) |
| 309 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 310 | if (!filtering) { |
| 311 | ScaleRowDown34_0 = ScaleRowDown34_Any_SSSE3; |
| 312 | ScaleRowDown34_1 = ScaleRowDown34_Any_SSSE3; |
| 313 | } else { |
| 314 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_Any_SSSE3; |
| 315 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_Any_SSSE3; |
| 316 | } |
| 317 | if (dst_width % 24 == 0) { |
| 318 | if (!filtering) { |
| 319 | ScaleRowDown34_0 = ScaleRowDown34_SSSE3; |
| 320 | ScaleRowDown34_1 = ScaleRowDown34_SSSE3; |
| 321 | } else { |
| 322 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_SSSE3; |
| 323 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_SSSE3; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | #endif |
| 328 | #if defined(HAS_SCALEROWDOWN34_DSPR2) |
| 329 | if (TestCpuFlag(kCpuHasDSPR2) && (dst_width % 24 == 0) && |
| 330 | IS_ALIGNED(src_ptr, 4) && IS_ALIGNED(src_stride, 4) && |
| 331 | IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { |
| 332 | if (!filtering) { |
| 333 | ScaleRowDown34_0 = ScaleRowDown34_DSPR2; |
| 334 | ScaleRowDown34_1 = ScaleRowDown34_DSPR2; |
| 335 | } else { |
| 336 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_DSPR2; |
| 337 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_DSPR2; |
| 338 | } |
| 339 | } |
| 340 | #endif |
| 341 |
| 342 | for (y = 0; y < dst_height - 2; y += 3) { |
| 343 | ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); |
| 344 | src_ptr += src_stride; |
| 345 | dst_ptr += dst_stride; |
| 346 | ScaleRowDown34_1(src_ptr, filter_stride, dst_ptr, dst_width); |
| 347 | src_ptr += src_stride; |
| 348 | dst_ptr += dst_stride; |
| 349 | ScaleRowDown34_0(src_ptr + src_stride, -filter_stride, |
| 350 | dst_ptr, dst_width); |
| 351 | src_ptr += src_stride * 2; |
| 352 | dst_ptr += dst_stride; |
| 353 | } |
| 354 |
| 355 | // Remainder 1 or 2 rows with last row vertically unfiltered |
| 356 | if ((dst_height % 3) == 2) { |
| 357 | ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); |
| 358 | src_ptr += src_stride; |
| 359 | dst_ptr += dst_stride; |
| 360 | ScaleRowDown34_1(src_ptr, 0, dst_ptr, dst_width); |
| 361 | } else if ((dst_height % 3) == 1) { |
| 362 | ScaleRowDown34_0(src_ptr, 0, dst_ptr, dst_width); |
| 363 | } |
| 364 | } |
| 365 |
| 366 | static void ScalePlaneDown34_16(int src_width, int src_height, |
| 367 | int dst_width, int dst_height, |
| 368 | int src_stride, int dst_stride, |
| 369 | const uint16* src_ptr, uint16* dst_ptr, |
| 370 | enum FilterMode filtering) { |
| 371 | int y; |
| 372 | void (*ScaleRowDown34_0)(const uint16* src_ptr, ptrdiff_t src_stride, |
| 373 | uint16* dst_ptr, int dst_width); |
| 374 | void (*ScaleRowDown34_1)(const uint16* src_ptr, ptrdiff_t src_stride, |
| 375 | uint16* dst_ptr, int dst_width); |
| 376 | const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; |
| 377 | assert(dst_width % 3 == 0); |
| 378 | if (!filtering) { |
| 379 | ScaleRowDown34_0 = ScaleRowDown34_16_C; |
| 380 | ScaleRowDown34_1 = ScaleRowDown34_16_C; |
| 381 | } else { |
| 382 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_C; |
| 383 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_C; |
| 384 | } |
| 385 | #if defined(HAS_SCALEROWDOWN34_16_NEON) |
| 386 | if (TestCpuFlag(kCpuHasNEON) && (dst_width % 24 == 0)) { |
| 387 | if (!filtering) { |
| 388 | ScaleRowDown34_0 = ScaleRowDown34_16_NEON; |
| 389 | ScaleRowDown34_1 = ScaleRowDown34_16_NEON; |
| 390 | } else { |
| 391 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_NEON; |
| 392 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_NEON; |
| 393 | } |
| 394 | } |
| 395 | #endif |
| 396 | #if defined(HAS_SCALEROWDOWN34_16_SSSE3) |
| 397 | if (TestCpuFlag(kCpuHasSSSE3) && (dst_width % 24 == 0)) { |
| 398 | if (!filtering) { |
| 399 | ScaleRowDown34_0 = ScaleRowDown34_16_SSSE3; |
| 400 | ScaleRowDown34_1 = ScaleRowDown34_16_SSSE3; |
| 401 | } else { |
| 402 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_SSSE3; |
| 403 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_SSSE3; |
| 404 | } |
| 405 | } |
| 406 | #endif |
| 407 | #if defined(HAS_SCALEROWDOWN34_16_DSPR2) |
| 408 | if (TestCpuFlag(kCpuHasDSPR2) && (dst_width % 24 == 0) && |
| 409 | IS_ALIGNED(src_ptr, 4) && IS_ALIGNED(src_stride, 4) && |
| 410 | IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { |
| 411 | if (!filtering) { |
| 412 | ScaleRowDown34_0 = ScaleRowDown34_16_DSPR2; |
| 413 | ScaleRowDown34_1 = ScaleRowDown34_16_DSPR2; |
| 414 | } else { |
| 415 | ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_DSPR2; |
| 416 | ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_DSPR2; |
| 417 | } |
| 418 | } |
| 419 | #endif |
| 420 |
| 421 | for (y = 0; y < dst_height - 2; y += 3) { |
| 422 | ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); |
| 423 | src_ptr += src_stride; |
| 424 | dst_ptr += dst_stride; |
| 425 | ScaleRowDown34_1(src_ptr, filter_stride, dst_ptr, dst_width); |
| 426 | src_ptr += src_stride; |
| 427 | dst_ptr += dst_stride; |
| 428 | ScaleRowDown34_0(src_ptr + src_stride, -filter_stride, |
| 429 | dst_ptr, dst_width); |
| 430 | src_ptr += src_stride * 2; |
| 431 | dst_ptr += dst_stride; |
| 432 | } |
| 433 |
| 434 | // Remainder 1 or 2 rows with last row vertically unfiltered |
| 435 | if ((dst_height % 3) == 2) { |
| 436 | ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width); |
| 437 | src_ptr += src_stride; |
| 438 | dst_ptr += dst_stride; |
| 439 | ScaleRowDown34_1(src_ptr, 0, dst_ptr, dst_width); |
| 440 | } else if ((dst_height % 3) == 1) { |
| 441 | ScaleRowDown34_0(src_ptr, 0, dst_ptr, dst_width); |
| 442 | } |
| 443 | } |
| 444 |
| 445 |
| 446 | // Scale plane, 3/8 |
| 447 | // This is an optimized version for scaling down a plane to 3/8 |
| 448 | // of its original size. |
| 449 | // |
| 450 | // Uses box filter arranges like this |
| 451 | // aaabbbcc -> abc |
| 452 | // aaabbbcc def |
| 453 | // aaabbbcc ghi |
| 454 | // dddeeeff |
| 455 | // dddeeeff |
| 456 | // dddeeeff |
| 457 | // ggghhhii |
| 458 | // ggghhhii |
| 459 | // Boxes are 3x3, 2x3, 3x2 and 2x2 |
| 460 |
| 461 | static void ScalePlaneDown38(int src_width, int src_height, |
| 462 | int dst_width, int dst_height, |
| 463 | int src_stride, int dst_stride, |
| 464 | const uint8* src_ptr, uint8* dst_ptr, |
| 465 | enum FilterMode filtering) { |
| 466 | int y; |
| 467 | void (*ScaleRowDown38_3)(const uint8* src_ptr, ptrdiff_t src_stride, |
| 468 | uint8* dst_ptr, int dst_width); |
| 469 | void (*ScaleRowDown38_2)(const uint8* src_ptr, ptrdiff_t src_stride, |
| 470 | uint8* dst_ptr, int dst_width); |
| 471 | const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; |
| 472 | assert(dst_width % 3 == 0); |
| 473 | if (!filtering) { |
| 474 | ScaleRowDown38_3 = ScaleRowDown38_C; |
| 475 | ScaleRowDown38_2 = ScaleRowDown38_C; |
| 476 | } else { |
| 477 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_C; |
| 478 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_C; |
| 479 | } |
| 480 |
| 481 | #if defined(HAS_SCALEROWDOWN38_NEON) |
| 482 | if (TestCpuFlag(kCpuHasNEON)) { |
| 483 | if (!filtering) { |
| 484 | ScaleRowDown38_3 = ScaleRowDown38_Any_NEON; |
| 485 | ScaleRowDown38_2 = ScaleRowDown38_Any_NEON; |
| 486 | } else { |
| 487 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_Any_NEON; |
| 488 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_Any_NEON; |
| 489 | } |
| 490 | if (dst_width % 12 == 0) { |
| 491 | if (!filtering) { |
| 492 | ScaleRowDown38_3 = ScaleRowDown38_NEON; |
| 493 | ScaleRowDown38_2 = ScaleRowDown38_NEON; |
| 494 | } else { |
| 495 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_NEON; |
| 496 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_NEON; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | #endif |
| 501 | #if defined(HAS_SCALEROWDOWN38_SSSE3) |
| 502 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 503 | if (!filtering) { |
| 504 | ScaleRowDown38_3 = ScaleRowDown38_Any_SSSE3; |
| 505 | ScaleRowDown38_2 = ScaleRowDown38_Any_SSSE3; |
| 506 | } else { |
| 507 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_Any_SSSE3; |
| 508 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_Any_SSSE3; |
| 509 | } |
| 510 | if (dst_width % 12 == 0 && !filtering) { |
| 511 | ScaleRowDown38_3 = ScaleRowDown38_SSSE3; |
| 512 | ScaleRowDown38_2 = ScaleRowDown38_SSSE3; |
| 513 | } |
| 514 | if (dst_width % 6 == 0 && filtering) { |
| 515 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_SSSE3; |
| 516 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_SSSE3; |
| 517 | } |
| 518 | } |
| 519 | #endif |
| 520 | #if defined(HAS_SCALEROWDOWN38_DSPR2) |
| 521 | if (TestCpuFlag(kCpuHasDSPR2) && (dst_width % 12 == 0) && |
| 522 | IS_ALIGNED(src_ptr, 4) && IS_ALIGNED(src_stride, 4) && |
| 523 | IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { |
| 524 | if (!filtering) { |
| 525 | ScaleRowDown38_3 = ScaleRowDown38_DSPR2; |
| 526 | ScaleRowDown38_2 = ScaleRowDown38_DSPR2; |
| 527 | } else { |
| 528 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_DSPR2; |
| 529 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_DSPR2; |
| 530 | } |
| 531 | } |
| 532 | #endif |
| 533 |
| 534 | for (y = 0; y < dst_height - 2; y += 3) { |
| 535 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 536 | src_ptr += src_stride * 3; |
| 537 | dst_ptr += dst_stride; |
| 538 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 539 | src_ptr += src_stride * 3; |
| 540 | dst_ptr += dst_stride; |
| 541 | ScaleRowDown38_2(src_ptr, filter_stride, dst_ptr, dst_width); |
| 542 | src_ptr += src_stride * 2; |
| 543 | dst_ptr += dst_stride; |
| 544 | } |
| 545 |
| 546 | // Remainder 1 or 2 rows with last row vertically unfiltered |
| 547 | if ((dst_height % 3) == 2) { |
| 548 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 549 | src_ptr += src_stride * 3; |
| 550 | dst_ptr += dst_stride; |
| 551 | ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); |
| 552 | } else if ((dst_height % 3) == 1) { |
| 553 | ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); |
| 554 | } |
| 555 | } |
| 556 |
| 557 | static void ScalePlaneDown38_16(int src_width, int src_height, |
| 558 | int dst_width, int dst_height, |
| 559 | int src_stride, int dst_stride, |
| 560 | const uint16* src_ptr, uint16* dst_ptr, |
| 561 | enum FilterMode filtering) { |
| 562 | int y; |
| 563 | void (*ScaleRowDown38_3)(const uint16* src_ptr, ptrdiff_t src_stride, |
| 564 | uint16* dst_ptr, int dst_width); |
| 565 | void (*ScaleRowDown38_2)(const uint16* src_ptr, ptrdiff_t src_stride, |
| 566 | uint16* dst_ptr, int dst_width); |
| 567 | const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride; |
| 568 | assert(dst_width % 3 == 0); |
| 569 | if (!filtering) { |
| 570 | ScaleRowDown38_3 = ScaleRowDown38_16_C; |
| 571 | ScaleRowDown38_2 = ScaleRowDown38_16_C; |
| 572 | } else { |
| 573 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_C; |
| 574 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_C; |
| 575 | } |
| 576 | #if defined(HAS_SCALEROWDOWN38_16_NEON) |
| 577 | if (TestCpuFlag(kCpuHasNEON) && (dst_width % 12 == 0)) { |
| 578 | if (!filtering) { |
| 579 | ScaleRowDown38_3 = ScaleRowDown38_16_NEON; |
| 580 | ScaleRowDown38_2 = ScaleRowDown38_16_NEON; |
| 581 | } else { |
| 582 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_NEON; |
| 583 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_NEON; |
| 584 | } |
| 585 | } |
| 586 | #endif |
| 587 | #if defined(HAS_SCALEROWDOWN38_16_SSSE3) |
| 588 | if (TestCpuFlag(kCpuHasSSSE3) && (dst_width % 24 == 0)) { |
| 589 | if (!filtering) { |
| 590 | ScaleRowDown38_3 = ScaleRowDown38_16_SSSE3; |
| 591 | ScaleRowDown38_2 = ScaleRowDown38_16_SSSE3; |
| 592 | } else { |
| 593 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_SSSE3; |
| 594 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_SSSE3; |
| 595 | } |
| 596 | } |
| 597 | #endif |
| 598 | #if defined(HAS_SCALEROWDOWN38_16_DSPR2) |
| 599 | if (TestCpuFlag(kCpuHasDSPR2) && (dst_width % 12 == 0) && |
| 600 | IS_ALIGNED(src_ptr, 4) && IS_ALIGNED(src_stride, 4) && |
| 601 | IS_ALIGNED(dst_ptr, 4) && IS_ALIGNED(dst_stride, 4)) { |
| 602 | if (!filtering) { |
| 603 | ScaleRowDown38_3 = ScaleRowDown38_16_DSPR2; |
| 604 | ScaleRowDown38_2 = ScaleRowDown38_16_DSPR2; |
| 605 | } else { |
| 606 | ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_DSPR2; |
| 607 | ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_DSPR2; |
| 608 | } |
| 609 | } |
| 610 | #endif |
| 611 |
| 612 | for (y = 0; y < dst_height - 2; y += 3) { |
| 613 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 614 | src_ptr += src_stride * 3; |
| 615 | dst_ptr += dst_stride; |
| 616 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 617 | src_ptr += src_stride * 3; |
| 618 | dst_ptr += dst_stride; |
| 619 | ScaleRowDown38_2(src_ptr, filter_stride, dst_ptr, dst_width); |
| 620 | src_ptr += src_stride * 2; |
| 621 | dst_ptr += dst_stride; |
| 622 | } |
| 623 |
| 624 | // Remainder 1 or 2 rows with last row vertically unfiltered |
| 625 | if ((dst_height % 3) == 2) { |
| 626 | ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width); |
| 627 | src_ptr += src_stride * 3; |
| 628 | dst_ptr += dst_stride; |
| 629 | ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); |
| 630 | } else if ((dst_height % 3) == 1) { |
| 631 | ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width); |
| 632 | } |
| 633 | } |
| 634 |
| 635 | #define MIN1(x) ((x) < 1 ? 1 : (x)) |
| 636 |
| 637 | static __inline uint32 SumPixels(int iboxwidth, const uint16* src_ptr) { |
| 638 | uint32 sum = 0u; |
| 639 | int x; |
| 640 | assert(iboxwidth > 0); |
| 641 | for (x = 0; x < iboxwidth; ++x) { |
| 642 | sum += src_ptr[x]; |
| 643 | } |
| 644 | return sum; |
| 645 | } |
| 646 |
| 647 | static __inline uint32 SumPixels_16(int iboxwidth, const uint32* src_ptr) { |
| 648 | uint32 sum = 0u; |
| 649 | int x; |
| 650 | assert(iboxwidth > 0); |
| 651 | for (x = 0; x < iboxwidth; ++x) { |
| 652 | sum += src_ptr[x]; |
| 653 | } |
| 654 | return sum; |
| 655 | } |
| 656 |
| 657 | static void ScaleAddCols2_C(int dst_width, int boxheight, int x, int dx, |
| 658 | const uint16* src_ptr, uint8* dst_ptr) { |
| 659 | int i; |
| 660 | int scaletbl[2]; |
| 661 | int minboxwidth = dx >> 16; |
| 662 | int boxwidth; |
| 663 | scaletbl[0] = 65536 / (MIN1(minboxwidth) * boxheight); |
| 664 | scaletbl[1] = 65536 / (MIN1(minboxwidth + 1) * boxheight); |
| 665 | for (i = 0; i < dst_width; ++i) { |
| 666 | int ix = x >> 16; |
| 667 | x += dx; |
| 668 | boxwidth = MIN1((x >> 16) - ix); |
| 669 | *dst_ptr++ = SumPixels(boxwidth, src_ptr + ix) * |
| 670 | scaletbl[boxwidth - minboxwidth] >> 16; |
| 671 | } |
| 672 | } |
| 673 |
| 674 | static void ScaleAddCols2_16_C(int dst_width, int boxheight, int x, int dx, |
| 675 | const uint32* src_ptr, uint16* dst_ptr) { |
| 676 | int i; |
| 677 | int scaletbl[2]; |
| 678 | int minboxwidth = dx >> 16; |
| 679 | int boxwidth; |
| 680 | scaletbl[0] = 65536 / (MIN1(minboxwidth) * boxheight); |
| 681 | scaletbl[1] = 65536 / (MIN1(minboxwidth + 1) * boxheight); |
| 682 | for (i = 0; i < dst_width; ++i) { |
| 683 | int ix = x >> 16; |
| 684 | x += dx; |
| 685 | boxwidth = MIN1((x >> 16) - ix); |
| 686 | *dst_ptr++ = SumPixels_16(boxwidth, src_ptr + ix) * |
| 687 | scaletbl[boxwidth - minboxwidth] >> 16; |
| 688 | } |
| 689 | } |
| 690 |
| 691 | static void ScaleAddCols0_C(int dst_width, int boxheight, int x, int, |
| 692 | const uint16* src_ptr, uint8* dst_ptr) { |
| 693 | int scaleval = 65536 / boxheight; |
| 694 | int i; |
| 695 | src_ptr += (x >> 16); |
| 696 | for (i = 0; i < dst_width; ++i) { |
| 697 | *dst_ptr++ = src_ptr[i] * scaleval >> 16; |
| 698 | } |
| 699 | } |
| 700 |
| 701 | static void ScaleAddCols1_C(int dst_width, int boxheight, int x, int dx, |
| 702 | const uint16* src_ptr, uint8* dst_ptr) { |
| 703 | int boxwidth = MIN1(dx >> 16); |
| 704 | int scaleval = 65536 / (boxwidth * boxheight); |
| 705 | int i; |
| 706 | x >>= 16; |
| 707 | for (i = 0; i < dst_width; ++i) { |
| 708 | *dst_ptr++ = SumPixels(boxwidth, src_ptr + x) * scaleval >> 16; |
| 709 | x += boxwidth; |
| 710 | } |
| 711 | } |
| 712 |
| 713 | static void ScaleAddCols1_16_C(int dst_width, int boxheight, int x, int dx, |
| 714 | const uint32* src_ptr, uint16* dst_ptr) { |
| 715 | int boxwidth = MIN1(dx >> 16); |
| 716 | int scaleval = 65536 / (boxwidth * boxheight); |
| 717 | int i; |
| 718 | for (i = 0; i < dst_width; ++i) { |
| 719 | *dst_ptr++ = SumPixels_16(boxwidth, src_ptr + x) * scaleval >> 16; |
| 720 | x += boxwidth; |
| 721 | } |
| 722 | } |
| 723 |
| 724 | // Scale plane down to any dimensions, with interpolation. |
| 725 | // (boxfilter). |
| 726 | // |
| 727 | // Same method as SimpleScale, which is fixed point, outputting |
| 728 | // one pixel of destination using fixed point (16.16) to step |
| 729 | // through source, sampling a box of pixel with simple |
| 730 | // averaging. |
| 731 | static void ScalePlaneBox(int src_width, int src_height, |
| 732 | int dst_width, int dst_height, |
| 733 | int src_stride, int dst_stride, |
| 734 | const uint8* src_ptr, uint8* dst_ptr) { |
| 735 | int j, k; |
| 736 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 737 | int x = 0; |
| 738 | int y = 0; |
| 739 | int dx = 0; |
| 740 | int dy = 0; |
| 741 | const int max_y = (src_height << 16); |
| 742 | ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterBox, |
| 743 | &x, &y, &dx, &dy); |
| 744 | src_width = Abs(src_width); |
| 745 | { |
| 746 | // Allocate a row buffer of uint16. |
| 747 | align_buffer_64(row16, src_width * 2); |
| 748 | void (*ScaleAddCols)(int dst_width, int boxheight, int x, int dx, |
| 749 | const uint16* src_ptr, uint8* dst_ptr) = |
| 750 | (dx & 0xffff) ? ScaleAddCols2_C: |
| 751 | ((dx != 0x10000) ? ScaleAddCols1_C : ScaleAddCols0_C); |
| 752 | void (*ScaleAddRow)(const uint8* src_ptr, uint16* dst_ptr, int src_width) = |
| 753 | ScaleAddRow_C; |
| 754 | #if defined(HAS_SCALEADDROW_SSE2) |
| 755 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 756 | ScaleAddRow = ScaleAddRow_Any_SSE2; |
| 757 | if (IS_ALIGNED(src_width, 16)) { |
| 758 | ScaleAddRow = ScaleAddRow_SSE2; |
| 759 | } |
| 760 | } |
| 761 | #endif |
| 762 | #if defined(HAS_SCALEADDROW_AVX2) |
| 763 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 764 | ScaleAddRow = ScaleAddRow_Any_AVX2; |
| 765 | if (IS_ALIGNED(src_width, 32)) { |
| 766 | ScaleAddRow = ScaleAddRow_AVX2; |
| 767 | } |
| 768 | } |
| 769 | #endif |
| 770 | #if defined(HAS_SCALEADDROW_NEON) |
| 771 | if (TestCpuFlag(kCpuHasNEON)) { |
| 772 | ScaleAddRow = ScaleAddRow_Any_NEON; |
| 773 | if (IS_ALIGNED(src_width, 16)) { |
| 774 | ScaleAddRow = ScaleAddRow_NEON; |
| 775 | } |
| 776 | } |
| 777 | #endif |
| 778 |
| 779 | for (j = 0; j < dst_height; ++j) { |
| 780 | int boxheight; |
| 781 | int iy = y >> 16; |
| 782 | const uint8* src = src_ptr + iy * src_stride; |
| 783 | y += dy; |
| 784 | if (y > max_y) { |
| 785 | y = max_y; |
| 786 | } |
| 787 | boxheight = MIN1((y >> 16) - iy); |
| 788 | memset(row16, 0, src_width * 2); |
| 789 | for (k = 0; k < boxheight; ++k) { |
| 790 | ScaleAddRow(src, (uint16 *)(row16), src_width); |
| 791 | src += src_stride; |
| 792 | } |
| 793 | ScaleAddCols(dst_width, boxheight, x, dx, (uint16*)(row16), dst_ptr); |
| 794 | dst_ptr += dst_stride; |
| 795 | } |
| 796 | free_aligned_buffer_64(row16); |
| 797 | } |
| 798 | } |
| 799 |
| 800 | static void ScalePlaneBox_16(int src_width, int src_height, |
| 801 | int dst_width, int dst_height, |
| 802 | int src_stride, int dst_stride, |
| 803 | const uint16* src_ptr, uint16* dst_ptr) { |
| 804 | int j, k; |
| 805 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 806 | int x = 0; |
| 807 | int y = 0; |
| 808 | int dx = 0; |
| 809 | int dy = 0; |
| 810 | const int max_y = (src_height << 16); |
| 811 | ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterBox, |
| 812 | &x, &y, &dx, &dy); |
| 813 | src_width = Abs(src_width); |
| 814 | { |
| 815 | // Allocate a row buffer of uint32. |
| 816 | align_buffer_64(row32, src_width * 4); |
| 817 | void (*ScaleAddCols)(int dst_width, int boxheight, int x, int dx, |
| 818 | const uint32* src_ptr, uint16* dst_ptr) = |
| 819 | (dx & 0xffff) ? ScaleAddCols2_16_C: ScaleAddCols1_16_C; |
| 820 | void (*ScaleAddRow)(const uint16* src_ptr, uint32* dst_ptr, int src_width) = |
| 821 | ScaleAddRow_16_C; |
| 822 |
| 823 | #if defined(HAS_SCALEADDROW_16_SSE2) |
| 824 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(src_width, 16)) { |
| 825 | ScaleAddRow = ScaleAddRow_16_SSE2; |
| 826 | } |
| 827 | #endif |
| 828 |
| 829 | for (j = 0; j < dst_height; ++j) { |
| 830 | int boxheight; |
| 831 | int iy = y >> 16; |
| 832 | const uint16* src = src_ptr + iy * src_stride; |
| 833 | y += dy; |
| 834 | if (y > max_y) { |
| 835 | y = max_y; |
| 836 | } |
| 837 | boxheight = MIN1((y >> 16) - iy); |
| 838 | memset(row32, 0, src_width * 4); |
| 839 | for (k = 0; k < boxheight; ++k) { |
| 840 | ScaleAddRow(src, (uint32 *)(row32), src_width); |
| 841 | src += src_stride; |
| 842 | } |
| 843 | ScaleAddCols(dst_width, boxheight, x, dx, (uint32*)(row32), dst_ptr); |
| 844 | dst_ptr += dst_stride; |
| 845 | } |
| 846 | free_aligned_buffer_64(row32); |
| 847 | } |
| 848 | } |
| 849 |
| 850 | // Scale plane down with bilinear interpolation. |
| 851 | void ScalePlaneBilinearDown(int src_width, int src_height, |
| 852 | int dst_width, int dst_height, |
| 853 | int src_stride, int dst_stride, |
| 854 | const uint8* src_ptr, uint8* dst_ptr, |
| 855 | enum FilterMode filtering) { |
| 856 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 857 | int x = 0; |
| 858 | int y = 0; |
| 859 | int dx = 0; |
| 860 | int dy = 0; |
| 861 | // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear. |
| 862 | // Allocate a row buffer. |
| 863 | align_buffer_64(row, src_width); |
| 864 |
| 865 | const int max_y = (src_height - 1) << 16; |
| 866 | int j; |
| 867 | void (*ScaleFilterCols)(uint8* dst_ptr, const uint8* src_ptr, |
| 868 | int dst_width, int x, int dx) = |
| 869 | (src_width >= 32768) ? ScaleFilterCols64_C : ScaleFilterCols_C; |
| 870 | void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr, |
| 871 | ptrdiff_t src_stride, int dst_width, int source_y_fraction) = |
| 872 | InterpolateRow_C; |
| 873 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, |
| 874 | &x, &y, &dx, &dy); |
| 875 | src_width = Abs(src_width); |
| 876 |
| 877 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 878 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 879 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 880 | if (IS_ALIGNED(src_width, 16)) { |
| 881 | InterpolateRow = InterpolateRow_SSSE3; |
| 882 | } |
| 883 | } |
| 884 | #endif |
| 885 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 886 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 887 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 888 | if (IS_ALIGNED(src_width, 32)) { |
| 889 | InterpolateRow = InterpolateRow_AVX2; |
| 890 | } |
| 891 | } |
| 892 | #endif |
| 893 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 894 | if (TestCpuFlag(kCpuHasNEON)) { |
| 895 | InterpolateRow = InterpolateRow_Any_NEON; |
| 896 | if (IS_ALIGNED(src_width, 16)) { |
| 897 | InterpolateRow = InterpolateRow_NEON; |
| 898 | } |
| 899 | } |
| 900 | #endif |
| 901 | #if defined(HAS_INTERPOLATEROW_DSPR2) |
| 902 | if (TestCpuFlag(kCpuHasDSPR2)) { |
| 903 | InterpolateRow = InterpolateRow_Any_DSPR2; |
| 904 | if (IS_ALIGNED(src_width, 4)) { |
| 905 | InterpolateRow = InterpolateRow_DSPR2; |
| 906 | } |
| 907 | } |
| 908 | #endif |
| 909 |
| 910 |
| 911 | #if defined(HAS_SCALEFILTERCOLS_SSSE3) |
| 912 | if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 913 | ScaleFilterCols = ScaleFilterCols_SSSE3; |
| 914 | } |
| 915 | #endif |
| 916 | #if defined(HAS_SCALEFILTERCOLS_NEON) |
| 917 | if (TestCpuFlag(kCpuHasNEON) && src_width < 32768) { |
| 918 | ScaleFilterCols = ScaleFilterCols_Any_NEON; |
| 919 | if (IS_ALIGNED(dst_width, 8)) { |
| 920 | ScaleFilterCols = ScaleFilterCols_NEON; |
| 921 | } |
| 922 | } |
| 923 | #endif |
| 924 | if (y > max_y) { |
| 925 | y = max_y; |
| 926 | } |
| 927 |
| 928 | for (j = 0; j < dst_height; ++j) { |
| 929 | int yi = y >> 16; |
| 930 | const uint8* src = src_ptr + yi * src_stride; |
| 931 | if (filtering == kFilterLinear) { |
| 932 | ScaleFilterCols(dst_ptr, src, dst_width, x, dx); |
| 933 | } else { |
| 934 | int yf = (y >> 8) & 255; |
| 935 | InterpolateRow(row, src, src_stride, src_width, yf); |
| 936 | ScaleFilterCols(dst_ptr, row, dst_width, x, dx); |
| 937 | } |
| 938 | dst_ptr += dst_stride; |
| 939 | y += dy; |
| 940 | if (y > max_y) { |
| 941 | y = max_y; |
| 942 | } |
| 943 | } |
| 944 | free_aligned_buffer_64(row); |
| 945 | } |
| 946 |
| 947 | void ScalePlaneBilinearDown_16(int src_width, int src_height, |
| 948 | int dst_width, int dst_height, |
| 949 | int src_stride, int dst_stride, |
| 950 | const uint16* src_ptr, uint16* dst_ptr, |
| 951 | enum FilterMode filtering) { |
| 952 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 953 | int x = 0; |
| 954 | int y = 0; |
| 955 | int dx = 0; |
| 956 | int dy = 0; |
| 957 | // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear. |
| 958 | // Allocate a row buffer. |
| 959 | align_buffer_64(row, src_width * 2); |
| 960 |
| 961 | const int max_y = (src_height - 1) << 16; |
| 962 | int j; |
| 963 | void (*ScaleFilterCols)(uint16* dst_ptr, const uint16* src_ptr, |
| 964 | int dst_width, int x, int dx) = |
| 965 | (src_width >= 32768) ? ScaleFilterCols64_16_C : ScaleFilterCols_16_C; |
| 966 | void (*InterpolateRow)(uint16* dst_ptr, const uint16* src_ptr, |
| 967 | ptrdiff_t src_stride, int dst_width, int source_y_fraction) = |
| 968 | InterpolateRow_16_C; |
| 969 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, |
| 970 | &x, &y, &dx, &dy); |
| 971 | src_width = Abs(src_width); |
| 972 |
| 973 | #if defined(HAS_INTERPOLATEROW_16_SSE2) |
| 974 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 975 | InterpolateRow = InterpolateRow_Any_16_SSE2; |
| 976 | if (IS_ALIGNED(src_width, 16)) { |
| 977 | InterpolateRow = InterpolateRow_16_SSE2; |
| 978 | } |
| 979 | } |
| 980 | #endif |
| 981 | #if defined(HAS_INTERPOLATEROW_16_SSSE3) |
| 982 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 983 | InterpolateRow = InterpolateRow_Any_16_SSSE3; |
| 984 | if (IS_ALIGNED(src_width, 16)) { |
| 985 | InterpolateRow = InterpolateRow_16_SSSE3; |
| 986 | } |
| 987 | } |
| 988 | #endif |
| 989 | #if defined(HAS_INTERPOLATEROW_16_AVX2) |
| 990 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 991 | InterpolateRow = InterpolateRow_Any_16_AVX2; |
| 992 | if (IS_ALIGNED(src_width, 32)) { |
| 993 | InterpolateRow = InterpolateRow_16_AVX2; |
| 994 | } |
| 995 | } |
| 996 | #endif |
| 997 | #if defined(HAS_INTERPOLATEROW_16_NEON) |
| 998 | if (TestCpuFlag(kCpuHasNEON)) { |
| 999 | InterpolateRow = InterpolateRow_Any_16_NEON; |
| 1000 | if (IS_ALIGNED(src_width, 16)) { |
| 1001 | InterpolateRow = InterpolateRow_16_NEON; |
| 1002 | } |
| 1003 | } |
| 1004 | #endif |
| 1005 | #if defined(HAS_INTERPOLATEROW_16_DSPR2) |
| 1006 | if (TestCpuFlag(kCpuHasDSPR2)) { |
| 1007 | InterpolateRow = InterpolateRow_Any_16_DSPR2; |
| 1008 | if (IS_ALIGNED(src_width, 4)) { |
| 1009 | InterpolateRow = InterpolateRow_16_DSPR2; |
| 1010 | } |
| 1011 | } |
| 1012 | #endif |
| 1013 |
| 1014 |
| 1015 | #if defined(HAS_SCALEFILTERCOLS_16_SSSE3) |
| 1016 | if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 1017 | ScaleFilterCols = ScaleFilterCols_16_SSSE3; |
| 1018 | } |
| 1019 | #endif |
| 1020 | if (y > max_y) { |
| 1021 | y = max_y; |
| 1022 | } |
| 1023 |
| 1024 | for (j = 0; j < dst_height; ++j) { |
| 1025 | int yi = y >> 16; |
| 1026 | const uint16* src = src_ptr + yi * src_stride; |
| 1027 | if (filtering == kFilterLinear) { |
| 1028 | ScaleFilterCols(dst_ptr, src, dst_width, x, dx); |
| 1029 | } else { |
| 1030 | int yf = (y >> 8) & 255; |
| 1031 | InterpolateRow((uint16*)row, src, src_stride, src_width, yf); |
| 1032 | ScaleFilterCols(dst_ptr, (uint16*)row, dst_width, x, dx); |
| 1033 | } |
| 1034 | dst_ptr += dst_stride; |
| 1035 | y += dy; |
| 1036 | if (y > max_y) { |
| 1037 | y = max_y; |
| 1038 | } |
| 1039 | } |
| 1040 | free_aligned_buffer_64(row); |
| 1041 | } |
| 1042 |
| 1043 | // Scale up down with bilinear interpolation. |
| 1044 | void ScalePlaneBilinearUp(int src_width, int src_height, |
| 1045 | int dst_width, int dst_height, |
| 1046 | int src_stride, int dst_stride, |
| 1047 | const uint8* src_ptr, uint8* dst_ptr, |
| 1048 | enum FilterMode filtering) { |
| 1049 | int j; |
| 1050 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1051 | int x = 0; |
| 1052 | int y = 0; |
| 1053 | int dx = 0; |
| 1054 | int dy = 0; |
| 1055 | const int max_y = (src_height - 1) << 16; |
| 1056 | void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr, |
| 1057 | ptrdiff_t src_stride, int dst_width, int source_y_fraction) = |
| 1058 | InterpolateRow_C; |
| 1059 | void (*ScaleFilterCols)(uint8* dst_ptr, const uint8* src_ptr, |
| 1060 | int dst_width, int x, int dx) = |
| 1061 | filtering ? ScaleFilterCols_C : ScaleCols_C; |
| 1062 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, |
| 1063 | &x, &y, &dx, &dy); |
| 1064 | src_width = Abs(src_width); |
| 1065 |
| 1066 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 1067 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1068 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 1069 | if (IS_ALIGNED(dst_width, 16)) { |
| 1070 | InterpolateRow = InterpolateRow_SSSE3; |
| 1071 | } |
| 1072 | } |
| 1073 | #endif |
| 1074 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 1075 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1076 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 1077 | if (IS_ALIGNED(dst_width, 32)) { |
| 1078 | InterpolateRow = InterpolateRow_AVX2; |
| 1079 | } |
| 1080 | } |
| 1081 | #endif |
| 1082 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 1083 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1084 | InterpolateRow = InterpolateRow_Any_NEON; |
| 1085 | if (IS_ALIGNED(dst_width, 16)) { |
| 1086 | InterpolateRow = InterpolateRow_NEON; |
| 1087 | } |
| 1088 | } |
| 1089 | #endif |
| 1090 | #if defined(HAS_INTERPOLATEROW_DSPR2) |
| 1091 | if (TestCpuFlag(kCpuHasDSPR2)) { |
| 1092 | InterpolateRow = InterpolateRow_Any_DSPR2; |
| 1093 | if (IS_ALIGNED(dst_width, 4)) { |
| 1094 | InterpolateRow = InterpolateRow_DSPR2; |
| 1095 | } |
| 1096 | } |
| 1097 | #endif |
| 1098 |
| 1099 | if (filtering && src_width >= 32768) { |
| 1100 | ScaleFilterCols = ScaleFilterCols64_C; |
| 1101 | } |
| 1102 | #if defined(HAS_SCALEFILTERCOLS_SSSE3) |
| 1103 | if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 1104 | ScaleFilterCols = ScaleFilterCols_SSSE3; |
| 1105 | } |
| 1106 | #endif |
| 1107 | #if defined(HAS_SCALEFILTERCOLS_NEON) |
| 1108 | if (filtering && TestCpuFlag(kCpuHasNEON) && src_width < 32768) { |
| 1109 | ScaleFilterCols = ScaleFilterCols_Any_NEON; |
| 1110 | if (IS_ALIGNED(dst_width, 8)) { |
| 1111 | ScaleFilterCols = ScaleFilterCols_NEON; |
| 1112 | } |
| 1113 | } |
| 1114 | #endif |
| 1115 | if (!filtering && src_width * 2 == dst_width && x < 0x8000) { |
| 1116 | ScaleFilterCols = ScaleColsUp2_C; |
| 1117 | #if defined(HAS_SCALECOLS_SSE2) |
| 1118 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 1119 | ScaleFilterCols = ScaleColsUp2_SSE2; |
| 1120 | } |
| 1121 | #endif |
| 1122 | } |
| 1123 |
| 1124 | if (y > max_y) { |
| 1125 | y = max_y; |
| 1126 | } |
| 1127 | { |
| 1128 | int yi = y >> 16; |
| 1129 | const uint8* src = src_ptr + yi * src_stride; |
| 1130 |
| 1131 | // Allocate 2 row buffers. |
| 1132 | const int kRowSize = (dst_width + 31) & ~31; |
| 1133 | align_buffer_64(row, kRowSize * 2); |
| 1134 |
| 1135 | uint8* rowptr = row; |
| 1136 | int rowstride = kRowSize; |
| 1137 | int lasty = yi; |
| 1138 |
| 1139 | ScaleFilterCols(rowptr, src, dst_width, x, dx); |
| 1140 | if (src_height > 1) { |
| 1141 | src += src_stride; |
| 1142 | } |
| 1143 | ScaleFilterCols(rowptr + rowstride, src, dst_width, x, dx); |
| 1144 | src += src_stride; |
| 1145 |
| 1146 | for (j = 0; j < dst_height; ++j) { |
| 1147 | yi = y >> 16; |
| 1148 | if (yi != lasty) { |
| 1149 | if (y > max_y) { |
| 1150 | y = max_y; |
| 1151 | yi = y >> 16; |
| 1152 | src = src_ptr + yi * src_stride; |
| 1153 | } |
| 1154 | if (yi != lasty) { |
| 1155 | ScaleFilterCols(rowptr, src, dst_width, x, dx); |
| 1156 | rowptr += rowstride; |
| 1157 | rowstride = -rowstride; |
| 1158 | lasty = yi; |
| 1159 | src += src_stride; |
| 1160 | } |
| 1161 | } |
| 1162 | if (filtering == kFilterLinear) { |
| 1163 | InterpolateRow(dst_ptr, rowptr, 0, dst_width, 0); |
| 1164 | } else { |
| 1165 | int yf = (y >> 8) & 255; |
| 1166 | InterpolateRow(dst_ptr, rowptr, rowstride, dst_width, yf); |
| 1167 | } |
| 1168 | dst_ptr += dst_stride; |
| 1169 | y += dy; |
| 1170 | } |
| 1171 | free_aligned_buffer_64(row); |
| 1172 | } |
| 1173 | } |
| 1174 |
| 1175 | void ScalePlaneBilinearUp_16(int src_width, int src_height, |
| 1176 | int dst_width, int dst_height, |
| 1177 | int src_stride, int dst_stride, |
| 1178 | const uint16* src_ptr, uint16* dst_ptr, |
| 1179 | enum FilterMode filtering) { |
| 1180 | int j; |
| 1181 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1182 | int x = 0; |
| 1183 | int y = 0; |
| 1184 | int dx = 0; |
| 1185 | int dy = 0; |
| 1186 | const int max_y = (src_height - 1) << 16; |
| 1187 | void (*InterpolateRow)(uint16* dst_ptr, const uint16* src_ptr, |
| 1188 | ptrdiff_t src_stride, int dst_width, int source_y_fraction) = |
| 1189 | InterpolateRow_16_C; |
| 1190 | void (*ScaleFilterCols)(uint16* dst_ptr, const uint16* src_ptr, |
| 1191 | int dst_width, int x, int dx) = |
| 1192 | filtering ? ScaleFilterCols_16_C : ScaleCols_16_C; |
| 1193 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, |
| 1194 | &x, &y, &dx, &dy); |
| 1195 | src_width = Abs(src_width); |
| 1196 |
| 1197 | #if defined(HAS_INTERPOLATEROW_16_SSE2) |
| 1198 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1199 | InterpolateRow = InterpolateRow_Any_16_SSE2; |
| 1200 | if (IS_ALIGNED(dst_width, 16)) { |
| 1201 | InterpolateRow = InterpolateRow_16_SSE2; |
| 1202 | } |
| 1203 | } |
| 1204 | #endif |
| 1205 | #if defined(HAS_INTERPOLATEROW_16_SSSE3) |
| 1206 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 1207 | InterpolateRow = InterpolateRow_Any_16_SSSE3; |
| 1208 | if (IS_ALIGNED(dst_width, 16)) { |
| 1209 | InterpolateRow = InterpolateRow_16_SSSE3; |
| 1210 | } |
| 1211 | } |
| 1212 | #endif |
| 1213 | #if defined(HAS_INTERPOLATEROW_16_AVX2) |
| 1214 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 1215 | InterpolateRow = InterpolateRow_Any_16_AVX2; |
| 1216 | if (IS_ALIGNED(dst_width, 32)) { |
| 1217 | InterpolateRow = InterpolateRow_16_AVX2; |
| 1218 | } |
| 1219 | } |
| 1220 | #endif |
| 1221 | #if defined(HAS_INTERPOLATEROW_16_NEON) |
| 1222 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1223 | InterpolateRow = InterpolateRow_Any_16_NEON; |
| 1224 | if (IS_ALIGNED(dst_width, 16)) { |
| 1225 | InterpolateRow = InterpolateRow_16_NEON; |
| 1226 | } |
| 1227 | } |
| 1228 | #endif |
| 1229 | #if defined(HAS_INTERPOLATEROW_16_DSPR2) |
| 1230 | if (TestCpuFlag(kCpuHasDSPR2)) { |
| 1231 | InterpolateRow = InterpolateRow_Any_16_DSPR2; |
| 1232 | if (IS_ALIGNED(dst_width, 4)) { |
| 1233 | InterpolateRow = InterpolateRow_16_DSPR2; |
| 1234 | } |
| 1235 | } |
| 1236 | #endif |
| 1237 |
| 1238 | if (filtering && src_width >= 32768) { |
| 1239 | ScaleFilterCols = ScaleFilterCols64_16_C; |
| 1240 | } |
| 1241 | #if defined(HAS_SCALEFILTERCOLS_16_SSSE3) |
| 1242 | if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 1243 | ScaleFilterCols = ScaleFilterCols_16_SSSE3; |
| 1244 | } |
| 1245 | #endif |
| 1246 | if (!filtering && src_width * 2 == dst_width && x < 0x8000) { |
| 1247 | ScaleFilterCols = ScaleColsUp2_16_C; |
| 1248 | #if defined(HAS_SCALECOLS_16_SSE2) |
| 1249 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 1250 | ScaleFilterCols = ScaleColsUp2_16_SSE2; |
| 1251 | } |
| 1252 | #endif |
| 1253 | } |
| 1254 |
| 1255 | if (y > max_y) { |
| 1256 | y = max_y; |
| 1257 | } |
| 1258 | { |
| 1259 | int yi = y >> 16; |
| 1260 | const uint16* src = src_ptr + yi * src_stride; |
| 1261 |
| 1262 | // Allocate 2 row buffers. |
| 1263 | const int kRowSize = (dst_width + 31) & ~31; |
| 1264 | align_buffer_64(row, kRowSize * 4); |
| 1265 |
| 1266 | uint16* rowptr = (uint16*)row; |
| 1267 | int rowstride = kRowSize; |
| 1268 | int lasty = yi; |
| 1269 |
| 1270 | ScaleFilterCols(rowptr, src, dst_width, x, dx); |
| 1271 | if (src_height > 1) { |
| 1272 | src += src_stride; |
| 1273 | } |
| 1274 | ScaleFilterCols(rowptr + rowstride, src, dst_width, x, dx); |
| 1275 | src += src_stride; |
| 1276 |
| 1277 | for (j = 0; j < dst_height; ++j) { |
| 1278 | yi = y >> 16; |
| 1279 | if (yi != lasty) { |
| 1280 | if (y > max_y) { |
| 1281 | y = max_y; |
| 1282 | yi = y >> 16; |
| 1283 | src = src_ptr + yi * src_stride; |
| 1284 | } |
| 1285 | if (yi != lasty) { |
| 1286 | ScaleFilterCols(rowptr, src, dst_width, x, dx); |
| 1287 | rowptr += rowstride; |
| 1288 | rowstride = -rowstride; |
| 1289 | lasty = yi; |
| 1290 | src += src_stride; |
| 1291 | } |
| 1292 | } |
| 1293 | if (filtering == kFilterLinear) { |
| 1294 | InterpolateRow(dst_ptr, rowptr, 0, dst_width, 0); |
| 1295 | } else { |
| 1296 | int yf = (y >> 8) & 255; |
| 1297 | InterpolateRow(dst_ptr, rowptr, rowstride, dst_width, yf); |
| 1298 | } |
| 1299 | dst_ptr += dst_stride; |
| 1300 | y += dy; |
| 1301 | } |
| 1302 | free_aligned_buffer_64(row); |
| 1303 | } |
| 1304 | } |
| 1305 |
| 1306 | // Scale Plane to/from any dimensions, without interpolation. |
| 1307 | // Fixed point math is used for performance: The upper 16 bits |
| 1308 | // of x and dx is the integer part of the source position and |
| 1309 | // the lower 16 bits are the fixed decimal part. |
| 1310 |
| 1311 | static void ScalePlaneSimple(int src_width, int src_height, |
| 1312 | int dst_width, int dst_height, |
| 1313 | int src_stride, int dst_stride, |
| 1314 | const uint8* src_ptr, uint8* dst_ptr) { |
| 1315 | int i; |
| 1316 | void (*ScaleCols)(uint8* dst_ptr, const uint8* src_ptr, |
| 1317 | int dst_width, int x, int dx) = ScaleCols_C; |
| 1318 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1319 | int x = 0; |
| 1320 | int y = 0; |
| 1321 | int dx = 0; |
| 1322 | int dy = 0; |
| 1323 | ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterNone, |
| 1324 | &x, &y, &dx, &dy); |
| 1325 | src_width = Abs(src_width); |
| 1326 |
| 1327 | if (src_width * 2 == dst_width && x < 0x8000) { |
| 1328 | ScaleCols = ScaleColsUp2_C; |
| 1329 | #if defined(HAS_SCALECOLS_SSE2) |
| 1330 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 1331 | ScaleCols = ScaleColsUp2_SSE2; |
| 1332 | } |
| 1333 | #endif |
| 1334 | } |
| 1335 |
| 1336 | for (i = 0; i < dst_height; ++i) { |
| 1337 | ScaleCols(dst_ptr, src_ptr + (y >> 16) * src_stride, dst_width, x, dx); |
| 1338 | dst_ptr += dst_stride; |
| 1339 | y += dy; |
| 1340 | } |
| 1341 | } |
| 1342 |
| 1343 | static void ScalePlaneSimple_16(int src_width, int src_height, |
| 1344 | int dst_width, int dst_height, |
| 1345 | int src_stride, int dst_stride, |
| 1346 | const uint16* src_ptr, uint16* dst_ptr) { |
| 1347 | int i; |
| 1348 | void (*ScaleCols)(uint16* dst_ptr, const uint16* src_ptr, |
| 1349 | int dst_width, int x, int dx) = ScaleCols_16_C; |
| 1350 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 1351 | int x = 0; |
| 1352 | int y = 0; |
| 1353 | int dx = 0; |
| 1354 | int dy = 0; |
| 1355 | ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterNone, |
| 1356 | &x, &y, &dx, &dy); |
| 1357 | src_width = Abs(src_width); |
| 1358 |
| 1359 | if (src_width * 2 == dst_width && x < 0x8000) { |
| 1360 | ScaleCols = ScaleColsUp2_16_C; |
| 1361 | #if defined(HAS_SCALECOLS_16_SSE2) |
| 1362 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 1363 | ScaleCols = ScaleColsUp2_16_SSE2; |
| 1364 | } |
| 1365 | #endif |
| 1366 | } |
| 1367 |
| 1368 | for (i = 0; i < dst_height; ++i) { |
| 1369 | ScaleCols(dst_ptr, src_ptr + (y >> 16) * src_stride, |
| 1370 | dst_width, x, dx); |
| 1371 | dst_ptr += dst_stride; |
| 1372 | y += dy; |
| 1373 | } |
| 1374 | } |
| 1375 |
| 1376 | // Scale a plane. |
| 1377 | // This function dispatches to a specialized scaler based on scale factor. |
| 1378 |
| 1379 | LIBYUV_API |
| 1380 | void ScalePlane(const uint8* src, int src_stride, |
| 1381 | int src_width, int src_height, |
| 1382 | uint8* dst, int dst_stride, |
| 1383 | int dst_width, int dst_height, |
| 1384 | enum FilterMode filtering) { |
| 1385 | // Simplify filtering when possible. |
| 1386 | filtering = ScaleFilterReduce(src_width, src_height, |
| 1387 | dst_width, dst_height, filtering); |
| 1388 |
| 1389 | // Negative height means invert the image. |
| 1390 | if (src_height < 0) { |
| 1391 | src_height = -src_height; |
| 1392 | src = src + (src_height - 1) * src_stride; |
| 1393 | src_stride = -src_stride; |
| 1394 | } |
| 1395 |
| 1396 | // Use specialized scales to improve performance for common resolutions. |
| 1397 | // For example, all the 1/2 scalings will use ScalePlaneDown2() |
| 1398 | if (dst_width == src_width && dst_height == src_height) { |
| 1399 | // Straight copy. |
| 1400 | CopyPlane(src, src_stride, dst, dst_stride, dst_width, dst_height); |
| 1401 | return; |
| 1402 | } |
| 1403 | if (dst_width == src_width && filtering != kFilterBox) { |
| 1404 | int dy = FixedDiv(src_height, dst_height); |
| 1405 | // Arbitrary scale vertically, but unscaled horizontally. |
| 1406 | ScalePlaneVertical(src_height, |
| 1407 | dst_width, dst_height, |
| 1408 | src_stride, dst_stride, src, dst, |
| 1409 | 0, 0, dy, 1, filtering); |
| 1410 | return; |
| 1411 | } |
| 1412 | if (dst_width <= Abs(src_width) && dst_height <= src_height) { |
| 1413 | // Scale down. |
| 1414 | if (4 * dst_width == 3 * src_width && |
| 1415 | 4 * dst_height == 3 * src_height) { |
| 1416 | // optimized, 3/4 |
| 1417 | ScalePlaneDown34(src_width, src_height, dst_width, dst_height, |
| 1418 | src_stride, dst_stride, src, dst, filtering); |
| 1419 | return; |
| 1420 | } |
| 1421 | if (2 * dst_width == src_width && 2 * dst_height == src_height) { |
| 1422 | // optimized, 1/2 |
| 1423 | ScalePlaneDown2(src_width, src_height, dst_width, dst_height, |
| 1424 | src_stride, dst_stride, src, dst, filtering); |
| 1425 | return; |
| 1426 | } |
| 1427 | // 3/8 rounded up for odd sized chroma height. |
| 1428 | if (8 * dst_width == 3 * src_width && |
| 1429 | dst_height == ((src_height * 3 + 7) / 8)) { |
| 1430 | // optimized, 3/8 |
| 1431 | ScalePlaneDown38(src_width, src_height, dst_width, dst_height, |
| 1432 | src_stride, dst_stride, src, dst, filtering); |
| 1433 | return; |
| 1434 | } |
| 1435 | if (4 * dst_width == src_width && 4 * dst_height == src_height && |
| 1436 | (filtering == kFilterBox || filtering == kFilterNone)) { |
| 1437 | // optimized, 1/4 |
| 1438 | ScalePlaneDown4(src_width, src_height, dst_width, dst_height, |
| 1439 | src_stride, dst_stride, src, dst, filtering); |
| 1440 | return; |
| 1441 | } |
| 1442 | } |
| 1443 | if (filtering == kFilterBox && dst_height * 2 < src_height) { |
| 1444 | ScalePlaneBox(src_width, src_height, dst_width, dst_height, |
| 1445 | src_stride, dst_stride, src, dst); |
| 1446 | return; |
| 1447 | } |
| 1448 | if (filtering && dst_height > src_height) { |
| 1449 | ScalePlaneBilinearUp(src_width, src_height, dst_width, dst_height, |
| 1450 | src_stride, dst_stride, src, dst, filtering); |
| 1451 | return; |
| 1452 | } |
| 1453 | if (filtering) { |
| 1454 | ScalePlaneBilinearDown(src_width, src_height, dst_width, dst_height, |
| 1455 | src_stride, dst_stride, src, dst, filtering); |
| 1456 | return; |
| 1457 | } |
| 1458 | ScalePlaneSimple(src_width, src_height, dst_width, dst_height, |
| 1459 | src_stride, dst_stride, src, dst); |
| 1460 | } |
| 1461 |
| 1462 | LIBYUV_API |
| 1463 | void ScalePlane_16(const uint16* src, int src_stride, |
| 1464 | int src_width, int src_height, |
| 1465 | uint16* dst, int dst_stride, |
| 1466 | int dst_width, int dst_height, |
| 1467 | enum FilterMode filtering) { |
| 1468 | // Simplify filtering when possible. |
| 1469 | filtering = ScaleFilterReduce(src_width, src_height, |
| 1470 | dst_width, dst_height, filtering); |
| 1471 |
| 1472 | // Negative height means invert the image. |
| 1473 | if (src_height < 0) { |
| 1474 | src_height = -src_height; |
| 1475 | src = src + (src_height - 1) * src_stride; |
| 1476 | src_stride = -src_stride; |
| 1477 | } |
| 1478 |
| 1479 | // Use specialized scales to improve performance for common resolutions. |
| 1480 | // For example, all the 1/2 scalings will use ScalePlaneDown2() |
| 1481 | if (dst_width == src_width && dst_height == src_height) { |
| 1482 | // Straight copy. |
| 1483 | CopyPlane_16(src, src_stride, dst, dst_stride, dst_width, dst_height); |
| 1484 | return; |
| 1485 | } |
| 1486 | if (dst_width == src_width) { |
| 1487 | int dy = FixedDiv(src_height, dst_height); |
| 1488 | // Arbitrary scale vertically, but unscaled vertically. |
| 1489 | ScalePlaneVertical_16(src_height, |
| 1490 | dst_width, dst_height, |
| 1491 | src_stride, dst_stride, src, dst, |
| 1492 | 0, 0, dy, 1, filtering); |
| 1493 | return; |
| 1494 | } |
| 1495 | if (dst_width <= Abs(src_width) && dst_height <= src_height) { |
| 1496 | // Scale down. |
| 1497 | if (4 * dst_width == 3 * src_width && |
| 1498 | 4 * dst_height == 3 * src_height) { |
| 1499 | // optimized, 3/4 |
| 1500 | ScalePlaneDown34_16(src_width, src_height, dst_width, dst_height, |
| 1501 | src_stride, dst_stride, src, dst, filtering); |
| 1502 | return; |
| 1503 | } |
| 1504 | if (2 * dst_width == src_width && 2 * dst_height == src_height) { |
| 1505 | // optimized, 1/2 |
| 1506 | ScalePlaneDown2_16(src_width, src_height, dst_width, dst_height, |
| 1507 | src_stride, dst_stride, src, dst, filtering); |
| 1508 | return; |
| 1509 | } |
| 1510 | // 3/8 rounded up for odd sized chroma height. |
| 1511 | if (8 * dst_width == 3 * src_width && |
| 1512 | dst_height == ((src_height * 3 + 7) / 8)) { |
| 1513 | // optimized, 3/8 |
| 1514 | ScalePlaneDown38_16(src_width, src_height, dst_width, dst_height, |
| 1515 | src_stride, dst_stride, src, dst, filtering); |
| 1516 | return; |
| 1517 | } |
| 1518 | if (4 * dst_width == src_width && 4 * dst_height == src_height && |
| 1519 | filtering != kFilterBilinear) { |
| 1520 | // optimized, 1/4 |
| 1521 | ScalePlaneDown4_16(src_width, src_height, dst_width, dst_height, |
| 1522 | src_stride, dst_stride, src, dst, filtering); |
| 1523 | return; |
| 1524 | } |
| 1525 | } |
| 1526 | if (filtering == kFilterBox && dst_height * 2 < src_height) { |
| 1527 | ScalePlaneBox_16(src_width, src_height, dst_width, dst_height, |
| 1528 | src_stride, dst_stride, src, dst); |
| 1529 | return; |
| 1530 | } |
| 1531 | if (filtering && dst_height > src_height) { |
| 1532 | ScalePlaneBilinearUp_16(src_width, src_height, dst_width, dst_height, |
| 1533 | src_stride, dst_stride, src, dst, filtering); |
| 1534 | return; |
| 1535 | } |
| 1536 | if (filtering) { |
| 1537 | ScalePlaneBilinearDown_16(src_width, src_height, dst_width, dst_height, |
| 1538 | src_stride, dst_stride, src, dst, filtering); |
| 1539 | return; |
| 1540 | } |
| 1541 | ScalePlaneSimple_16(src_width, src_height, dst_width, dst_height, |
| 1542 | src_stride, dst_stride, src, dst); |
| 1543 | } |
| 1544 |
| 1545 | // Scale an I420 image. |
| 1546 | // This function in turn calls a scaling function for each plane. |
| 1547 |
| 1548 | LIBYUV_API |
| 1549 | int I420Scale(const uint8* src_y, int src_stride_y, |
| 1550 | const uint8* src_u, int src_stride_u, |
| 1551 | const uint8* src_v, int src_stride_v, |
| 1552 | int src_width, int src_height, |
| 1553 | uint8* dst_y, int dst_stride_y, |
| 1554 | uint8* dst_u, int dst_stride_u, |
| 1555 | uint8* dst_v, int dst_stride_v, |
| 1556 | int dst_width, int dst_height, |
| 1557 | enum FilterMode filtering) { |
| 1558 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 1559 | int src_halfheight = SUBSAMPLE(src_height, 1, 1); |
| 1560 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 1561 | int dst_halfheight = SUBSAMPLE(dst_height, 1, 1); |
| 1562 | if (!src_y || !src_u || !src_v || src_width == 0 || src_height == 0 || |
| 1563 | src_width > 32768 || src_height > 32768 || |
| 1564 | !dst_y || !dst_u || !dst_v || dst_width <= 0 || dst_height <= 0) { |
| 1565 | return -1; |
| 1566 | } |
| 1567 |
| 1568 | ScalePlane(src_y, src_stride_y, src_width, src_height, |
| 1569 | dst_y, dst_stride_y, dst_width, dst_height, |
| 1570 | filtering); |
| 1571 | ScalePlane(src_u, src_stride_u, src_halfwidth, src_halfheight, |
| 1572 | dst_u, dst_stride_u, dst_halfwidth, dst_halfheight, |
| 1573 | filtering); |
| 1574 | ScalePlane(src_v, src_stride_v, src_halfwidth, src_halfheight, |
| 1575 | dst_v, dst_stride_v, dst_halfwidth, dst_halfheight, |
| 1576 | filtering); |
| 1577 | return 0; |
| 1578 | } |
| 1579 |
| 1580 | LIBYUV_API |
| 1581 | int I420Scale_16(const uint16* src_y, int src_stride_y, |
| 1582 | const uint16* src_u, int src_stride_u, |
| 1583 | const uint16* src_v, int src_stride_v, |
| 1584 | int src_width, int src_height, |
| 1585 | uint16* dst_y, int dst_stride_y, |
| 1586 | uint16* dst_u, int dst_stride_u, |
| 1587 | uint16* dst_v, int dst_stride_v, |
| 1588 | int dst_width, int dst_height, |
| 1589 | enum FilterMode filtering) { |
| 1590 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 1591 | int src_halfheight = SUBSAMPLE(src_height, 1, 1); |
| 1592 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 1593 | int dst_halfheight = SUBSAMPLE(dst_height, 1, 1); |
| 1594 | if (!src_y || !src_u || !src_v || src_width == 0 || src_height == 0 || |
| 1595 | src_width > 32768 || src_height > 32768 || |
| 1596 | !dst_y || !dst_u || !dst_v || dst_width <= 0 || dst_height <= 0) { |
| 1597 | return -1; |
| 1598 | } |
| 1599 |
| 1600 | ScalePlane_16(src_y, src_stride_y, src_width, src_height, |
| 1601 | dst_y, dst_stride_y, dst_width, dst_height, |
| 1602 | filtering); |
| 1603 | ScalePlane_16(src_u, src_stride_u, src_halfwidth, src_halfheight, |
| 1604 | dst_u, dst_stride_u, dst_halfwidth, dst_halfheight, |
| 1605 | filtering); |
| 1606 | ScalePlane_16(src_v, src_stride_v, src_halfwidth, src_halfheight, |
| 1607 | dst_v, dst_stride_v, dst_halfwidth, dst_halfheight, |
| 1608 | filtering); |
| 1609 | return 0; |
| 1610 | } |
| 1611 |
| 1612 | // Deprecated api |
| 1613 | LIBYUV_API |
| 1614 | int Scale(const uint8* src_y, const uint8* src_u, const uint8* src_v, |
| 1615 | int src_stride_y, int src_stride_u, int src_stride_v, |
| 1616 | int src_width, int src_height, |
| 1617 | uint8* dst_y, uint8* dst_u, uint8* dst_v, |
| 1618 | int dst_stride_y, int dst_stride_u, int dst_stride_v, |
| 1619 | int dst_width, int dst_height, |
| 1620 | LIBYUV_BOOL interpolate) { |
| 1621 | return I420Scale(src_y, src_stride_y, |
| 1622 | src_u, src_stride_u, |
| 1623 | src_v, src_stride_v, |
| 1624 | src_width, src_height, |
| 1625 | dst_y, dst_stride_y, |
| 1626 | dst_u, dst_stride_u, |
| 1627 | dst_v, dst_stride_v, |
| 1628 | dst_width, dst_height, |
| 1629 | interpolate ? kFilterBox : kFilterNone); |
| 1630 | } |
| 1631 |
| 1632 | // Deprecated api |
| 1633 | LIBYUV_API |
| 1634 | int ScaleOffset(const uint8* src, int src_width, int src_height, |
| 1635 | uint8* dst, int dst_width, int dst_height, int dst_yoffset, |
| 1636 | LIBYUV_BOOL interpolate) { |
| 1637 | // Chroma requires offset to multiple of 2. |
| 1638 | int dst_yoffset_even = dst_yoffset & ~1; |
| 1639 | int src_halfwidth = SUBSAMPLE(src_width, 1, 1); |
| 1640 | int src_halfheight = SUBSAMPLE(src_height, 1, 1); |
| 1641 | int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1); |
| 1642 | int dst_halfheight = SUBSAMPLE(dst_height, 1, 1); |
| 1643 | int aheight = dst_height - dst_yoffset_even * 2; // actual output height |
| 1644 | const uint8* src_y = src; |
| 1645 | const uint8* src_u = src + src_width * src_height; |
| 1646 | const uint8* src_v = src + src_width * src_height + |
| 1647 | src_halfwidth * src_halfheight; |
| 1648 | uint8* dst_y = dst + dst_yoffset_even * dst_width; |
| 1649 | uint8* dst_u = dst + dst_width * dst_height + |
| 1650 | (dst_yoffset_even >> 1) * dst_halfwidth; |
| 1651 | uint8* dst_v = dst + dst_width * dst_height + dst_halfwidth * dst_halfheight + |
| 1652 | (dst_yoffset_even >> 1) * dst_halfwidth; |
| 1653 | if (!src || src_width <= 0 || src_height <= 0 || |
| 1654 | !dst || dst_width <= 0 || dst_height <= 0 || dst_yoffset_even < 0 || |
| 1655 | dst_yoffset_even >= dst_height) { |
| 1656 | return -1; |
| 1657 | } |
| 1658 | return I420Scale(src_y, src_width, |
| 1659 | src_u, src_halfwidth, |
| 1660 | src_v, src_halfwidth, |
| 1661 | src_width, src_height, |
| 1662 | dst_y, dst_width, |
| 1663 | dst_u, dst_halfwidth, |
| 1664 | dst_v, dst_halfwidth, |
| 1665 | dst_width, aheight, |
| 1666 | interpolate ? kFilterBox : kFilterNone); |
| 1667 | } |
| 1668 |
| 1669 | #ifdef __cplusplus |
| 1670 | } // extern "C" |
| 1671 | } // namespace libyuv |
| 1672 | #endif |
| 1673 |