| 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 CopyUV |
| 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 | // Macros to enable specialized scalers |
| 27 |
| 28 | #ifndef HAS_SCALEUVDOWN2 |
| 29 | #define HAS_SCALEUVDOWN2 1 |
| 30 | #endif |
| 31 | #ifndef HAS_SCALEUVDOWN4BOX |
| 32 | #define HAS_SCALEUVDOWN4BOX 1 |
| 33 | #endif |
| 34 | #ifndef HAS_SCALEUVDOWNEVEN |
| 35 | #define HAS_SCALEUVDOWNEVEN 1 |
| 36 | #endif |
| 37 | #ifndef HAS_SCALEUVBILINEARDOWN |
| 38 | #define HAS_SCALEUVBILINEARDOWN 1 |
| 39 | #endif |
| 40 | #ifndef HAS_SCALEUVBILINEARUP |
| 41 | #define HAS_SCALEUVBILINEARUP 1 |
| 42 | #endif |
| 43 | #ifndef HAS_UVCOPY |
| 44 | #define HAS_UVCOPY 1 |
| 45 | #endif |
| 46 | #ifndef HAS_SCALEPLANEVERTICAL |
| 47 | #define HAS_SCALEPLANEVERTICAL 1 |
| 48 | #endif |
| 49 |
| 50 | static __inline int Abs(int v) { |
| 51 | return v >= 0 ? v : -v; |
| 52 | } |
| 53 |
| 54 | // ScaleUV, 1/2 |
| 55 | // This is an optimized version for scaling down a UV to 1/2 of |
| 56 | // its original size. |
| 57 | #if HAS_SCALEUVDOWN2 |
| 58 | static void ScaleUVDown2(int src_width, |
| 59 | int src_height, |
| 60 | int dst_width, |
| 61 | int dst_height, |
| 62 | int src_stride, |
| 63 | int dst_stride, |
| 64 | const uint8_t* src_uv, |
| 65 | uint8_t* dst_uv, |
| 66 | int x, |
| 67 | int dx, |
| 68 | int y, |
| 69 | int dy, |
| 70 | enum FilterMode filtering) { |
| 71 | int j; |
| 72 | int row_stride = src_stride * (dy >> 16); |
| 73 | void (*ScaleUVRowDown2)(const uint8_t* src_uv, ptrdiff_t src_stride, |
| 74 | uint8_t* dst_uv, int dst_width) = |
| 75 | filtering == kFilterNone |
| 76 | ? ScaleUVRowDown2_C |
| 77 | : (filtering == kFilterLinear ? ScaleUVRowDown2Linear_C |
| 78 | : ScaleUVRowDown2Box_C); |
| 79 | (void)src_width; |
| 80 | (void)src_height; |
| 81 | (void)dx; |
| 82 | assert(dx == 65536 * 2); // Test scale factor of 2. |
| 83 | assert((dy & 0x1ffff) == 0); // Test vertical scale is multiple of 2. |
| 84 | // Advance to odd row, even column. |
| 85 | if (filtering == kFilterBilinear) { |
| 86 | src_uv += (y >> 16) * (intptr_t)src_stride + (x >> 16) * 2; |
| 87 | } else { |
| 88 | src_uv += (y >> 16) * (intptr_t)src_stride + ((x >> 16) - 1) * 2; |
| 89 | } |
| 90 |
| 91 | #if defined(HAS_SCALEUVROWDOWN2BOX_SSSE3) |
| 92 | if (TestCpuFlag(kCpuHasSSSE3) && filtering) { |
| 93 | ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_SSSE3; |
| 94 | if (IS_ALIGNED(dst_width, 4)) { |
| 95 | ScaleUVRowDown2 = ScaleUVRowDown2Box_SSSE3; |
| 96 | } |
| 97 | } |
| 98 | #endif |
| 99 | #if defined(HAS_SCALEUVROWDOWN2BOX_AVX2) |
| 100 | if (TestCpuFlag(kCpuHasAVX2) && filtering) { |
| 101 | ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_AVX2; |
| 102 | if (IS_ALIGNED(dst_width, 8)) { |
| 103 | ScaleUVRowDown2 = ScaleUVRowDown2Box_AVX2; |
| 104 | } |
| 105 | } |
| 106 | #endif |
| 107 | #if defined(HAS_SCALEUVROWDOWN2BOX_NEON) |
| 108 | if (TestCpuFlag(kCpuHasNEON) && filtering) { |
| 109 | ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_NEON; |
| 110 | if (IS_ALIGNED(dst_width, 8)) { |
| 111 | ScaleUVRowDown2 = ScaleUVRowDown2Box_NEON; |
| 112 | } |
| 113 | } |
| 114 | #endif |
| 115 |
| 116 | // This code is not enabled. Only box filter is available at this time. |
| 117 | #if defined(HAS_SCALEUVROWDOWN2_SSSE3) |
| 118 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 119 | ScaleUVRowDown2 = |
| 120 | filtering == kFilterNone |
| 121 | ? ScaleUVRowDown2_Any_SSSE3 |
| 122 | : (filtering == kFilterLinear ? ScaleUVRowDown2Linear_Any_SSSE3 |
| 123 | : ScaleUVRowDown2Box_Any_SSSE3); |
| 124 | if (IS_ALIGNED(dst_width, 2)) { |
| 125 | ScaleUVRowDown2 = |
| 126 | filtering == kFilterNone |
| 127 | ? ScaleUVRowDown2_SSSE3 |
| 128 | : (filtering == kFilterLinear ? ScaleUVRowDown2Linear_SSSE3 |
| 129 | : ScaleUVRowDown2Box_SSSE3); |
| 130 | } |
| 131 | } |
| 132 | #endif |
| 133 | // This code is not enabled. Only box filter is available at this time. |
| 134 | #if defined(HAS_SCALEUVROWDOWN2_NEON) |
| 135 | if (TestCpuFlag(kCpuHasNEON)) { |
| 136 | ScaleUVRowDown2 = |
| 137 | filtering == kFilterNone |
| 138 | ? ScaleUVRowDown2_Any_NEON |
| 139 | : (filtering == kFilterLinear ? ScaleUVRowDown2Linear_Any_NEON |
| 140 | : ScaleUVRowDown2Box_Any_NEON); |
| 141 | if (IS_ALIGNED(dst_width, 8)) { |
| 142 | ScaleUVRowDown2 = |
| 143 | filtering == kFilterNone |
| 144 | ? ScaleUVRowDown2_NEON |
| 145 | : (filtering == kFilterLinear ? ScaleUVRowDown2Linear_NEON |
| 146 | : ScaleUVRowDown2Box_NEON); |
| 147 | } |
| 148 | } |
| 149 | #endif |
| 150 | #if defined(HAS_SCALEUVROWDOWN2_MSA) |
| 151 | if (TestCpuFlag(kCpuHasMSA)) { |
| 152 | ScaleUVRowDown2 = |
| 153 | filtering == kFilterNone |
| 154 | ? ScaleUVRowDown2_Any_MSA |
| 155 | : (filtering == kFilterLinear ? ScaleUVRowDown2Linear_Any_MSA |
| 156 | : ScaleUVRowDown2Box_Any_MSA); |
| 157 | if (IS_ALIGNED(dst_width, 2)) { |
| 158 | ScaleUVRowDown2 = |
| 159 | filtering == kFilterNone |
| 160 | ? ScaleUVRowDown2_MSA |
| 161 | : (filtering == kFilterLinear ? ScaleUVRowDown2Linear_MSA |
| 162 | : ScaleUVRowDown2Box_MSA); |
| 163 | } |
| 164 | } |
| 165 | #endif |
| 166 |
| 167 | if (filtering == kFilterLinear) { |
| 168 | src_stride = 0; |
| 169 | } |
| 170 | for (j = 0; j < dst_height; ++j) { |
| 171 | ScaleUVRowDown2(src_uv, src_stride, dst_uv, dst_width); |
| 172 | src_uv += row_stride; |
| 173 | dst_uv += dst_stride; |
| 174 | } |
| 175 | } |
| 176 | #endif // HAS_SCALEUVDOWN2 |
| 177 |
| 178 | // ScaleUV, 1/4 |
| 179 | // This is an optimized version for scaling down a UV to 1/4 of |
| 180 | // its original size. |
| 181 | #if HAS_SCALEUVDOWN4BOX |
| 182 | static void ScaleUVDown4Box(int src_width, |
| 183 | int src_height, |
| 184 | int dst_width, |
| 185 | int dst_height, |
| 186 | int src_stride, |
| 187 | int dst_stride, |
| 188 | const uint8_t* src_uv, |
| 189 | uint8_t* dst_uv, |
| 190 | int x, |
| 191 | int dx, |
| 192 | int y, |
| 193 | int dy) { |
| 194 | int j; |
| 195 | // Allocate 2 rows of UV. |
| 196 | const int row_size = (dst_width * 2 * 2 + 15) & ~15; |
| 197 | align_buffer_64(row, row_size * 2); |
| 198 | int row_stride = src_stride * (dy >> 16); |
| 199 | void (*ScaleUVRowDown2)(const uint8_t* src_uv, ptrdiff_t src_stride, |
| 200 | uint8_t* dst_uv, int dst_width) = |
| 201 | ScaleUVRowDown2Box_C; |
| 202 | // Advance to odd row, even column. |
| 203 | src_uv += (y >> 16) * (intptr_t)src_stride + (x >> 16) * 2; |
| 204 | (void)src_width; |
| 205 | (void)src_height; |
| 206 | (void)dx; |
| 207 | assert(dx == 65536 * 4); // Test scale factor of 4. |
| 208 | assert((dy & 0x3ffff) == 0); // Test vertical scale is multiple of 4. |
| 209 |
| 210 | #if defined(HAS_SCALEUVROWDOWN2BOX_SSSE3) |
| 211 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 212 | ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_SSSE3; |
| 213 | if (IS_ALIGNED(dst_width, 4)) { |
| 214 | ScaleUVRowDown2 = ScaleUVRowDown2Box_SSSE3; |
| 215 | } |
| 216 | } |
| 217 | #endif |
| 218 | #if defined(HAS_SCALEUVROWDOWN2BOX_AVX2) |
| 219 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 220 | ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_AVX2; |
| 221 | if (IS_ALIGNED(dst_width, 8)) { |
| 222 | ScaleUVRowDown2 = ScaleUVRowDown2Box_AVX2; |
| 223 | } |
| 224 | } |
| 225 | #endif |
| 226 | #if defined(HAS_SCALEUVROWDOWN2BOX_NEON) |
| 227 | if (TestCpuFlag(kCpuHasNEON)) { |
| 228 | ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_NEON; |
| 229 | if (IS_ALIGNED(dst_width, 8)) { |
| 230 | ScaleUVRowDown2 = ScaleUVRowDown2Box_NEON; |
| 231 | } |
| 232 | } |
| 233 | #endif |
| 234 |
| 235 | for (j = 0; j < dst_height; ++j) { |
| 236 | ScaleUVRowDown2(src_uv, src_stride, row, dst_width * 2); |
| 237 | ScaleUVRowDown2(src_uv + src_stride * 2, src_stride, row + row_size, |
| 238 | dst_width * 2); |
| 239 | ScaleUVRowDown2(row, row_size, dst_uv, dst_width); |
| 240 | src_uv += row_stride; |
| 241 | dst_uv += dst_stride; |
| 242 | } |
| 243 | free_aligned_buffer_64(row); |
| 244 | } |
| 245 | #endif // HAS_SCALEUVDOWN4BOX |
| 246 |
| 247 | // ScaleUV Even |
| 248 | // This is an optimized version for scaling down a UV to even |
| 249 | // multiple of its original size. |
| 250 | #if HAS_SCALEUVDOWNEVEN |
| 251 | static void ScaleUVDownEven(int src_width, |
| 252 | int src_height, |
| 253 | int dst_width, |
| 254 | int dst_height, |
| 255 | int src_stride, |
| 256 | int dst_stride, |
| 257 | const uint8_t* src_uv, |
| 258 | uint8_t* dst_uv, |
| 259 | int x, |
| 260 | int dx, |
| 261 | int y, |
| 262 | int dy, |
| 263 | enum FilterMode filtering) { |
| 264 | int j; |
| 265 | int col_step = dx >> 16; |
| 266 | ptrdiff_t row_stride = (ptrdiff_t)((dy >> 16) * (intptr_t)src_stride); |
| 267 | void (*ScaleUVRowDownEven)(const uint8_t* src_uv, ptrdiff_t src_stride, |
| 268 | int src_step, uint8_t* dst_uv, int dst_width) = |
| 269 | filtering ? ScaleUVRowDownEvenBox_C : ScaleUVRowDownEven_C; |
| 270 | (void)src_width; |
| 271 | (void)src_height; |
| 272 | assert(IS_ALIGNED(src_width, 2)); |
| 273 | assert(IS_ALIGNED(src_height, 2)); |
| 274 | src_uv += (y >> 16) * (intptr_t)src_stride + (x >> 16) * 2; |
| 275 | #if defined(HAS_SCALEUVROWDOWNEVEN_SSSE3) |
| 276 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 277 | ScaleUVRowDownEven = filtering ? ScaleUVRowDownEvenBox_Any_SSSE3 |
| 278 | : ScaleUVRowDownEven_Any_SSSE3; |
| 279 | if (IS_ALIGNED(dst_width, 4)) { |
| 280 | ScaleUVRowDownEven = |
| 281 | filtering ? ScaleUVRowDownEvenBox_SSE2 : ScaleUVRowDownEven_SSSE3; |
| 282 | } |
| 283 | } |
| 284 | #endif |
| 285 | #if defined(HAS_SCALEUVROWDOWNEVEN_NEON) |
| 286 | if (TestCpuFlag(kCpuHasNEON) && !filtering) { |
| 287 | ScaleUVRowDownEven = ScaleUVRowDownEven_Any_NEON; |
| 288 | if (IS_ALIGNED(dst_width, 4)) { |
| 289 | ScaleUVRowDownEven = ScaleUVRowDownEven_NEON; |
| 290 | } |
| 291 | } |
| 292 | #endif // TODO(fbarchard): Enable Box filter |
| 293 | #if defined(HAS_SCALEUVROWDOWNEVENBOX_NEON) |
| 294 | if (TestCpuFlag(kCpuHasNEON)) { |
| 295 | ScaleUVRowDownEven = filtering ? ScaleUVRowDownEvenBox_Any_NEON |
| 296 | : ScaleUVRowDownEven_Any_NEON; |
| 297 | if (IS_ALIGNED(dst_width, 4)) { |
| 298 | ScaleUVRowDownEven = |
| 299 | filtering ? ScaleUVRowDownEvenBox_NEON : ScaleUVRowDownEven_NEON; |
| 300 | } |
| 301 | } |
| 302 | #endif |
| 303 | #if defined(HAS_SCALEUVROWDOWNEVEN_MSA) |
| 304 | if (TestCpuFlag(kCpuHasMSA)) { |
| 305 | ScaleUVRowDownEven = |
| 306 | filtering ? ScaleUVRowDownEvenBox_Any_MSA : ScaleUVRowDownEven_Any_MSA; |
| 307 | if (IS_ALIGNED(dst_width, 4)) { |
| 308 | ScaleUVRowDownEven = |
| 309 | filtering ? ScaleUVRowDownEvenBox_MSA : ScaleUVRowDownEven_MSA; |
| 310 | } |
| 311 | } |
| 312 | #endif |
| 313 |
| 314 | if (filtering == kFilterLinear) { |
| 315 | src_stride = 0; |
| 316 | } |
| 317 | for (j = 0; j < dst_height; ++j) { |
| 318 | ScaleUVRowDownEven(src_uv, src_stride, col_step, dst_uv, dst_width); |
| 319 | src_uv += row_stride; |
| 320 | dst_uv += dst_stride; |
| 321 | } |
| 322 | } |
| 323 | #endif |
| 324 |
| 325 | // Scale UV down with bilinear interpolation. |
| 326 | #if HAS_SCALEUVBILINEARDOWN |
| 327 | static void ScaleUVBilinearDown(int src_width, |
| 328 | int src_height, |
| 329 | int dst_width, |
| 330 | int dst_height, |
| 331 | int src_stride, |
| 332 | int dst_stride, |
| 333 | const uint8_t* src_uv, |
| 334 | uint8_t* dst_uv, |
| 335 | int x, |
| 336 | int dx, |
| 337 | int y, |
| 338 | int dy, |
| 339 | enum FilterMode filtering) { |
| 340 | int j; |
| 341 | void (*InterpolateRow)(uint8_t* dst_uv, const uint8_t* src_uv, |
| 342 | ptrdiff_t src_stride, int dst_width, |
| 343 | int source_y_fraction) = InterpolateRow_C; |
| 344 | void (*ScaleUVFilterCols)(uint8_t* dst_uv, const uint8_t* src_uv, |
| 345 | int dst_width, int x, int dx) = |
| 346 | (src_width >= 32768) ? ScaleUVFilterCols64_C : ScaleUVFilterCols_C; |
| 347 | int64_t xlast = x + (int64_t)(dst_width - 1) * dx; |
| 348 | int64_t xl = (dx >= 0) ? x : xlast; |
| 349 | int64_t xr = (dx >= 0) ? xlast : x; |
| 350 | int clip_src_width; |
| 351 | xl = (xl >> 16) & ~3; // Left edge aligned. |
| 352 | xr = (xr >> 16) + 1; // Right most pixel used. Bilinear uses 2 pixels. |
| 353 | xr = (xr + 1 + 3) & ~3; // 1 beyond 4 pixel aligned right most pixel. |
| 354 | if (xr > src_width) { |
| 355 | xr = src_width; |
| 356 | } |
| 357 | clip_src_width = (int)(xr - xl) * 2; // Width aligned to 2. |
| 358 | src_uv += xl * 2; |
| 359 | x -= (int)(xl << 16); |
| 360 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 361 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 362 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 363 | if (IS_ALIGNED(clip_src_width, 16)) { |
| 364 | InterpolateRow = InterpolateRow_SSSE3; |
| 365 | } |
| 366 | } |
| 367 | #endif |
| 368 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 369 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 370 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 371 | if (IS_ALIGNED(clip_src_width, 32)) { |
| 372 | InterpolateRow = InterpolateRow_AVX2; |
| 373 | } |
| 374 | } |
| 375 | #endif |
| 376 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 377 | if (TestCpuFlag(kCpuHasNEON)) { |
| 378 | InterpolateRow = InterpolateRow_Any_NEON; |
| 379 | if (IS_ALIGNED(clip_src_width, 16)) { |
| 380 | InterpolateRow = InterpolateRow_NEON; |
| 381 | } |
| 382 | } |
| 383 | #endif |
| 384 | #if defined(HAS_INTERPOLATEROW_MSA) |
| 385 | if (TestCpuFlag(kCpuHasMSA)) { |
| 386 | InterpolateRow = InterpolateRow_Any_MSA; |
| 387 | if (IS_ALIGNED(clip_src_width, 32)) { |
| 388 | InterpolateRow = InterpolateRow_MSA; |
| 389 | } |
| 390 | } |
| 391 | #endif |
| 392 | #if defined(HAS_INTERPOLATEROW_LSX) |
| 393 | if (TestCpuFlag(kCpuHasLSX)) { |
| 394 | InterpolateRow = InterpolateRow_Any_LSX; |
| 395 | if (IS_ALIGNED(clip_src_width, 32)) { |
| 396 | InterpolateRow = InterpolateRow_LSX; |
| 397 | } |
| 398 | } |
| 399 | #endif |
| 400 | #if defined(HAS_SCALEUVFILTERCOLS_SSSE3) |
| 401 | if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 402 | ScaleUVFilterCols = ScaleUVFilterCols_SSSE3; |
| 403 | } |
| 404 | #endif |
| 405 | #if defined(HAS_SCALEUVFILTERCOLS_NEON) |
| 406 | if (TestCpuFlag(kCpuHasNEON)) { |
| 407 | ScaleUVFilterCols = ScaleUVFilterCols_Any_NEON; |
| 408 | if (IS_ALIGNED(dst_width, 4)) { |
| 409 | ScaleUVFilterCols = ScaleUVFilterCols_NEON; |
| 410 | } |
| 411 | } |
| 412 | #endif |
| 413 | #if defined(HAS_SCALEUVFILTERCOLS_MSA) |
| 414 | if (TestCpuFlag(kCpuHasMSA)) { |
| 415 | ScaleUVFilterCols = ScaleUVFilterCols_Any_MSA; |
| 416 | if (IS_ALIGNED(dst_width, 8)) { |
| 417 | ScaleUVFilterCols = ScaleUVFilterCols_MSA; |
| 418 | } |
| 419 | } |
| 420 | #endif |
| 421 | // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear. |
| 422 | // Allocate a row of UV. |
| 423 | { |
| 424 | align_buffer_64(row, clip_src_width * 2); |
| 425 |
| 426 | const int max_y = (src_height - 1) << 16; |
| 427 | if (y > max_y) { |
| 428 | y = max_y; |
| 429 | } |
| 430 | for (j = 0; j < dst_height; ++j) { |
| 431 | int yi = y >> 16; |
| 432 | const uint8_t* src = src_uv + yi * (intptr_t)src_stride; |
| 433 | if (filtering == kFilterLinear) { |
| 434 | ScaleUVFilterCols(dst_uv, src, dst_width, x, dx); |
| 435 | } else { |
| 436 | int yf = (y >> 8) & 255; |
| 437 | InterpolateRow(row, src, src_stride, clip_src_width, yf); |
| 438 | ScaleUVFilterCols(dst_uv, row, dst_width, x, dx); |
| 439 | } |
| 440 | dst_uv += dst_stride; |
| 441 | y += dy; |
| 442 | if (y > max_y) { |
| 443 | y = max_y; |
| 444 | } |
| 445 | } |
| 446 | free_aligned_buffer_64(row); |
| 447 | } |
| 448 | } |
| 449 | #endif |
| 450 |
| 451 | // Scale UV up with bilinear interpolation. |
| 452 | #if HAS_SCALEUVBILINEARUP |
| 453 | static void ScaleUVBilinearUp(int src_width, |
| 454 | int src_height, |
| 455 | int dst_width, |
| 456 | int dst_height, |
| 457 | int src_stride, |
| 458 | int dst_stride, |
| 459 | const uint8_t* src_uv, |
| 460 | uint8_t* dst_uv, |
| 461 | int x, |
| 462 | int dx, |
| 463 | int y, |
| 464 | int dy, |
| 465 | enum FilterMode filtering) { |
| 466 | int j; |
| 467 | void (*InterpolateRow)(uint8_t* dst_uv, const uint8_t* src_uv, |
| 468 | ptrdiff_t src_stride, int dst_width, |
| 469 | int source_y_fraction) = InterpolateRow_C; |
| 470 | void (*ScaleUVFilterCols)(uint8_t* dst_uv, const uint8_t* src_uv, |
| 471 | int dst_width, int x, int dx) = |
| 472 | filtering ? ScaleUVFilterCols_C : ScaleUVCols_C; |
| 473 | const int max_y = (src_height - 1) << 16; |
| 474 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 475 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 476 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 477 | if (IS_ALIGNED(dst_width, 8)) { |
| 478 | InterpolateRow = InterpolateRow_SSSE3; |
| 479 | } |
| 480 | } |
| 481 | #endif |
| 482 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 483 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 484 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 485 | if (IS_ALIGNED(dst_width, 16)) { |
| 486 | InterpolateRow = InterpolateRow_AVX2; |
| 487 | } |
| 488 | } |
| 489 | #endif |
| 490 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 491 | if (TestCpuFlag(kCpuHasNEON)) { |
| 492 | InterpolateRow = InterpolateRow_Any_NEON; |
| 493 | if (IS_ALIGNED(dst_width, 8)) { |
| 494 | InterpolateRow = InterpolateRow_NEON; |
| 495 | } |
| 496 | } |
| 497 | #endif |
| 498 | #if defined(HAS_INTERPOLATEROW_MSA) |
| 499 | if (TestCpuFlag(kCpuHasMSA)) { |
| 500 | InterpolateRow = InterpolateRow_Any_MSA; |
| 501 | if (IS_ALIGNED(dst_width, 16)) { |
| 502 | InterpolateRow = InterpolateRow_MSA; |
| 503 | } |
| 504 | } |
| 505 | #endif |
| 506 | #if defined(HAS_INTERPOLATEROW_LSX) |
| 507 | if (TestCpuFlag(kCpuHasLSX)) { |
| 508 | InterpolateRow = InterpolateRow_Any_LSX; |
| 509 | if (IS_ALIGNED(dst_width, 16)) { |
| 510 | InterpolateRow = InterpolateRow_LSX; |
| 511 | } |
| 512 | } |
| 513 | #endif |
| 514 | if (src_width >= 32768) { |
| 515 | ScaleUVFilterCols = filtering ? ScaleUVFilterCols64_C : ScaleUVCols64_C; |
| 516 | } |
| 517 | #if defined(HAS_SCALEUVFILTERCOLS_SSSE3) |
| 518 | if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 519 | ScaleUVFilterCols = ScaleUVFilterCols_SSSE3; |
| 520 | } |
| 521 | #endif |
| 522 | #if defined(HAS_SCALEUVFILTERCOLS_NEON) |
| 523 | if (filtering && TestCpuFlag(kCpuHasNEON)) { |
| 524 | ScaleUVFilterCols = ScaleUVFilterCols_Any_NEON; |
| 525 | if (IS_ALIGNED(dst_width, 8)) { |
| 526 | ScaleUVFilterCols = ScaleUVFilterCols_NEON; |
| 527 | } |
| 528 | } |
| 529 | #endif |
| 530 | #if defined(HAS_SCALEUVFILTERCOLS_MSA) |
| 531 | if (filtering && TestCpuFlag(kCpuHasMSA)) { |
| 532 | ScaleUVFilterCols = ScaleUVFilterCols_Any_MSA; |
| 533 | if (IS_ALIGNED(dst_width, 16)) { |
| 534 | ScaleUVFilterCols = ScaleUVFilterCols_MSA; |
| 535 | } |
| 536 | } |
| 537 | #endif |
| 538 | #if defined(HAS_SCALEUVCOLS_SSSE3) |
| 539 | if (!filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 540 | ScaleUVFilterCols = ScaleUVCols_SSSE3; |
| 541 | } |
| 542 | #endif |
| 543 | #if defined(HAS_SCALEUVCOLS_NEON) |
| 544 | if (!filtering && TestCpuFlag(kCpuHasNEON)) { |
| 545 | ScaleUVFilterCols = ScaleUVCols_Any_NEON; |
| 546 | if (IS_ALIGNED(dst_width, 16)) { |
| 547 | ScaleUVFilterCols = ScaleUVCols_NEON; |
| 548 | } |
| 549 | } |
| 550 | #endif |
| 551 | #if defined(HAS_SCALEUVCOLS_MSA) |
| 552 | if (!filtering && TestCpuFlag(kCpuHasMSA)) { |
| 553 | ScaleUVFilterCols = ScaleUVCols_Any_MSA; |
| 554 | if (IS_ALIGNED(dst_width, 8)) { |
| 555 | ScaleUVFilterCols = ScaleUVCols_MSA; |
| 556 | } |
| 557 | } |
| 558 | #endif |
| 559 | if (!filtering && src_width * 2 == dst_width && x < 0x8000) { |
| 560 | ScaleUVFilterCols = ScaleUVColsUp2_C; |
| 561 | #if defined(HAS_SCALEUVCOLSUP2_SSSE3) |
| 562 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(dst_width, 8)) { |
| 563 | ScaleUVFilterCols = ScaleUVColsUp2_SSSE3; |
| 564 | } |
| 565 | #endif |
| 566 | } |
| 567 |
| 568 | if (y > max_y) { |
| 569 | y = max_y; |
| 570 | } |
| 571 |
| 572 | { |
| 573 | int yi = y >> 16; |
| 574 | const uint8_t* src = src_uv + yi * (intptr_t)src_stride; |
| 575 |
| 576 | // Allocate 2 rows of UV. |
| 577 | const int row_size = (dst_width * 2 + 15) & ~15; |
| 578 | align_buffer_64(row, row_size * 2); |
| 579 |
| 580 | uint8_t* rowptr = row; |
| 581 | int rowstride = row_size; |
| 582 | int lasty = yi; |
| 583 |
| 584 | ScaleUVFilterCols(rowptr, src, dst_width, x, dx); |
| 585 | if (src_height > 1) { |
| 586 | src += src_stride; |
| 587 | } |
| 588 | ScaleUVFilterCols(rowptr + rowstride, src, dst_width, x, dx); |
| 589 | if (src_height > 2) { |
| 590 | src += src_stride; |
| 591 | } |
| 592 |
| 593 | for (j = 0; j < dst_height; ++j) { |
| 594 | yi = y >> 16; |
| 595 | if (yi != lasty) { |
| 596 | if (y > max_y) { |
| 597 | y = max_y; |
| 598 | yi = y >> 16; |
| 599 | src = src_uv + yi * (intptr_t)src_stride; |
| 600 | } |
| 601 | if (yi != lasty) { |
| 602 | ScaleUVFilterCols(rowptr, src, dst_width, x, dx); |
| 603 | rowptr += rowstride; |
| 604 | rowstride = -rowstride; |
| 605 | lasty = yi; |
| 606 | if ((y + 65536) < max_y) { |
| 607 | src += src_stride; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | if (filtering == kFilterLinear) { |
| 612 | InterpolateRow(dst_uv, rowptr, 0, dst_width * 2, 0); |
| 613 | } else { |
| 614 | int yf = (y >> 8) & 255; |
| 615 | InterpolateRow(dst_uv, rowptr, rowstride, dst_width * 2, yf); |
| 616 | } |
| 617 | dst_uv += dst_stride; |
| 618 | y += dy; |
| 619 | } |
| 620 | free_aligned_buffer_64(row); |
| 621 | } |
| 622 | } |
| 623 | #endif // HAS_SCALEUVBILINEARUP |
| 624 |
| 625 | // Scale UV, horizontally up by 2 times. |
| 626 | // Uses linear filter horizontally, nearest vertically. |
| 627 | // This is an optimized version for scaling up a plane to 2 times of |
| 628 | // its original width, using linear interpolation. |
| 629 | // This is used to scale U and V planes of NV16 to NV24. |
| 630 | void ScaleUVLinearUp2(int src_width, |
| 631 | int src_height, |
| 632 | int dst_width, |
| 633 | int dst_height, |
| 634 | int src_stride, |
| 635 | int dst_stride, |
| 636 | const uint8_t* src_uv, |
| 637 | uint8_t* dst_uv) { |
| 638 | void (*ScaleRowUp)(const uint8_t* src_uv, uint8_t* dst_uv, int dst_width) = |
| 639 | ScaleUVRowUp2_Linear_Any_C; |
| 640 | int i; |
| 641 | int y; |
| 642 | int dy; |
| 643 |
| 644 | // This function can only scale up by 2 times horizontally. |
| 645 | assert(src_width == ((dst_width + 1) / 2)); |
| 646 |
| 647 | #ifdef HAS_SCALEUVROWUP2LINEAR_SSSE3 |
| 648 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 649 | ScaleRowUp = ScaleUVRowUp2_Linear_Any_SSSE3; |
| 650 | } |
| 651 | #endif |
| 652 |
| 653 | #ifdef HAS_SCALEUVROWUP2LINEAR_AVX2 |
| 654 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 655 | ScaleRowUp = ScaleUVRowUp2_Linear_Any_AVX2; |
| 656 | } |
| 657 | #endif |
| 658 |
| 659 | #ifdef HAS_SCALEUVROWUP2LINEAR_NEON |
| 660 | if (TestCpuFlag(kCpuHasNEON)) { |
| 661 | ScaleRowUp = ScaleUVRowUp2_Linear_Any_NEON; |
| 662 | } |
| 663 | #endif |
| 664 |
| 665 | if (dst_height == 1) { |
| 666 | ScaleRowUp(src_uv + ((src_height - 1) / 2) * (intptr_t)src_stride, dst_uv, |
| 667 | dst_width); |
| 668 | } else { |
| 669 | dy = FixedDiv(src_height - 1, dst_height - 1); |
| 670 | y = (1 << 15) - 1; |
| 671 | for (i = 0; i < dst_height; ++i) { |
| 672 | ScaleRowUp(src_uv + (y >> 16) * (intptr_t)src_stride, dst_uv, dst_width); |
| 673 | dst_uv += dst_stride; |
| 674 | y += dy; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 |
| 679 | // Scale plane, up by 2 times. |
| 680 | // This is an optimized version for scaling up a plane to 2 times of |
| 681 | // its original size, using bilinear interpolation. |
| 682 | // This is used to scale U and V planes of NV12 to NV24. |
| 683 | void ScaleUVBilinearUp2(int src_width, |
| 684 | int src_height, |
| 685 | int dst_width, |
| 686 | int dst_height, |
| 687 | int src_stride, |
| 688 | int dst_stride, |
| 689 | const uint8_t* src_ptr, |
| 690 | uint8_t* dst_ptr) { |
| 691 | void (*Scale2RowUp)(const uint8_t* src_ptr, ptrdiff_t src_stride, |
| 692 | uint8_t* dst_ptr, ptrdiff_t dst_stride, int dst_width) = |
| 693 | ScaleUVRowUp2_Bilinear_Any_C; |
| 694 | int x; |
| 695 |
| 696 | // This function can only scale up by 2 times. |
| 697 | assert(src_width == ((dst_width + 1) / 2)); |
| 698 | assert(src_height == ((dst_height + 1) / 2)); |
| 699 |
| 700 | #ifdef HAS_SCALEUVROWUP2BILINEAR_SSSE3 |
| 701 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 702 | Scale2RowUp = ScaleUVRowUp2_Bilinear_Any_SSSE3; |
| 703 | } |
| 704 | #endif |
| 705 |
| 706 | #ifdef HAS_SCALEUVROWUP2BILINEAR_AVX2 |
| 707 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 708 | Scale2RowUp = ScaleUVRowUp2_Bilinear_Any_AVX2; |
| 709 | } |
| 710 | #endif |
| 711 |
| 712 | #ifdef HAS_SCALEUVROWUP2BILINEAR_NEON |
| 713 | if (TestCpuFlag(kCpuHasNEON)) { |
| 714 | Scale2RowUp = ScaleUVRowUp2_Bilinear_Any_NEON; |
| 715 | } |
| 716 | #endif |
| 717 |
| 718 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 719 | dst_ptr += dst_stride; |
| 720 | for (x = 0; x < src_height - 1; ++x) { |
| 721 | Scale2RowUp(src_ptr, src_stride, dst_ptr, dst_stride, dst_width); |
| 722 | src_ptr += src_stride; |
| 723 | // TODO(fbarchard): Test performance of writing one row of destination at a |
| 724 | // time. |
| 725 | dst_ptr += 2 * dst_stride; |
| 726 | } |
| 727 | if (!(dst_height & 1)) { |
| 728 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 729 | } |
| 730 | } |
| 731 |
| 732 | // Scale 16 bit UV, horizontally up by 2 times. |
| 733 | // Uses linear filter horizontally, nearest vertically. |
| 734 | // This is an optimized version for scaling up a plane to 2 times of |
| 735 | // its original width, using linear interpolation. |
| 736 | // This is used to scale U and V planes of P210 to P410. |
| 737 | void ScaleUVLinearUp2_16(int src_width, |
| 738 | int src_height, |
| 739 | int dst_width, |
| 740 | int dst_height, |
| 741 | int src_stride, |
| 742 | int dst_stride, |
| 743 | const uint16_t* src_uv, |
| 744 | uint16_t* dst_uv) { |
| 745 | void (*ScaleRowUp)(const uint16_t* src_uv, uint16_t* dst_uv, int dst_width) = |
| 746 | ScaleUVRowUp2_Linear_16_Any_C; |
| 747 | int i; |
| 748 | int y; |
| 749 | int dy; |
| 750 |
| 751 | // This function can only scale up by 2 times horizontally. |
| 752 | assert(src_width == ((dst_width + 1) / 2)); |
| 753 |
| 754 | #ifdef HAS_SCALEUVROWUP2LINEAR_16_SSE41 |
| 755 | if (TestCpuFlag(kCpuHasSSE41)) { |
| 756 | ScaleRowUp = ScaleUVRowUp2_Linear_16_Any_SSE41; |
| 757 | } |
| 758 | #endif |
| 759 |
| 760 | #ifdef HAS_SCALEUVROWUP2LINEAR_16_AVX2 |
| 761 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 762 | ScaleRowUp = ScaleUVRowUp2_Linear_16_Any_AVX2; |
| 763 | } |
| 764 | #endif |
| 765 |
| 766 | #ifdef HAS_SCALEUVROWUP2LINEAR_16_NEON |
| 767 | if (TestCpuFlag(kCpuHasNEON)) { |
| 768 | ScaleRowUp = ScaleUVRowUp2_Linear_16_Any_NEON; |
| 769 | } |
| 770 | #endif |
| 771 |
| 772 | if (dst_height == 1) { |
| 773 | ScaleRowUp(src_uv + ((src_height - 1) / 2) * (intptr_t)src_stride, dst_uv, |
| 774 | dst_width); |
| 775 | } else { |
| 776 | dy = FixedDiv(src_height - 1, dst_height - 1); |
| 777 | y = (1 << 15) - 1; |
| 778 | for (i = 0; i < dst_height; ++i) { |
| 779 | ScaleRowUp(src_uv + (y >> 16) * (intptr_t)src_stride, dst_uv, dst_width); |
| 780 | dst_uv += dst_stride; |
| 781 | y += dy; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 |
| 786 | // Scale 16 bit UV, up by 2 times. |
| 787 | // This is an optimized version for scaling up a plane to 2 times of |
| 788 | // its original size, using bilinear interpolation. |
| 789 | // This is used to scale U and V planes of P010 to P410. |
| 790 | void ScaleUVBilinearUp2_16(int src_width, |
| 791 | int src_height, |
| 792 | int dst_width, |
| 793 | int dst_height, |
| 794 | int src_stride, |
| 795 | int dst_stride, |
| 796 | const uint16_t* src_ptr, |
| 797 | uint16_t* dst_ptr) { |
| 798 | void (*Scale2RowUp)(const uint16_t* src_ptr, ptrdiff_t src_stride, |
| 799 | uint16_t* dst_ptr, ptrdiff_t dst_stride, int dst_width) = |
| 800 | ScaleUVRowUp2_Bilinear_16_Any_C; |
| 801 | int x; |
| 802 |
| 803 | // This function can only scale up by 2 times. |
| 804 | assert(src_width == ((dst_width + 1) / 2)); |
| 805 | assert(src_height == ((dst_height + 1) / 2)); |
| 806 |
| 807 | #ifdef HAS_SCALEUVROWUP2BILINEAR_16_SSE41 |
| 808 | if (TestCpuFlag(kCpuHasSSE41)) { |
| 809 | Scale2RowUp = ScaleUVRowUp2_Bilinear_16_Any_SSE41; |
| 810 | } |
| 811 | #endif |
| 812 |
| 813 | #ifdef HAS_SCALEUVROWUP2BILINEAR_16_AVX2 |
| 814 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 815 | Scale2RowUp = ScaleUVRowUp2_Bilinear_16_Any_AVX2; |
| 816 | } |
| 817 | #endif |
| 818 |
| 819 | #ifdef HAS_SCALEUVROWUP2BILINEAR_16_NEON |
| 820 | if (TestCpuFlag(kCpuHasNEON)) { |
| 821 | Scale2RowUp = ScaleUVRowUp2_Bilinear_16_Any_NEON; |
| 822 | } |
| 823 | #endif |
| 824 |
| 825 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 826 | dst_ptr += dst_stride; |
| 827 | for (x = 0; x < src_height - 1; ++x) { |
| 828 | Scale2RowUp(src_ptr, src_stride, dst_ptr, dst_stride, dst_width); |
| 829 | src_ptr += src_stride; |
| 830 | // TODO(fbarchard): Test performance of writing one row of destination at a |
| 831 | // time. |
| 832 | dst_ptr += 2 * dst_stride; |
| 833 | } |
| 834 | if (!(dst_height & 1)) { |
| 835 | Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width); |
| 836 | } |
| 837 | } |
| 838 |
| 839 | // Scale UV to/from any dimensions, without interpolation. |
| 840 | // Fixed point math is used for performance: The upper 16 bits |
| 841 | // of x and dx is the integer part of the source position and |
| 842 | // the lower 16 bits are the fixed decimal part. |
| 843 |
| 844 | static void ScaleUVSimple(int src_width, |
| 845 | int src_height, |
| 846 | int dst_width, |
| 847 | int dst_height, |
| 848 | int src_stride, |
| 849 | int dst_stride, |
| 850 | const uint8_t* src_uv, |
| 851 | uint8_t* dst_uv, |
| 852 | int x, |
| 853 | int dx, |
| 854 | int y, |
| 855 | int dy) { |
| 856 | int j; |
| 857 | void (*ScaleUVCols)(uint8_t* dst_uv, const uint8_t* src_uv, int dst_width, |
| 858 | int x, int dx) = |
| 859 | (src_width >= 32768) ? ScaleUVCols64_C : ScaleUVCols_C; |
| 860 | (void)src_height; |
| 861 | #if defined(HAS_SCALEUVCOLS_SSSE3) |
| 862 | if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 863 | ScaleUVCols = ScaleUVCols_SSSE3; |
| 864 | } |
| 865 | #endif |
| 866 | #if defined(HAS_SCALEUVCOLS_NEON) |
| 867 | if (TestCpuFlag(kCpuHasNEON)) { |
| 868 | ScaleUVCols = ScaleUVCols_Any_NEON; |
| 869 | if (IS_ALIGNED(dst_width, 8)) { |
| 870 | ScaleUVCols = ScaleUVCols_NEON; |
| 871 | } |
| 872 | } |
| 873 | #endif |
| 874 | #if defined(HAS_SCALEUVCOLS_MSA) |
| 875 | if (TestCpuFlag(kCpuHasMSA)) { |
| 876 | ScaleUVCols = ScaleUVCols_Any_MSA; |
| 877 | if (IS_ALIGNED(dst_width, 4)) { |
| 878 | ScaleUVCols = ScaleUVCols_MSA; |
| 879 | } |
| 880 | } |
| 881 | #endif |
| 882 | if (src_width * 2 == dst_width && x < 0x8000) { |
| 883 | ScaleUVCols = ScaleUVColsUp2_C; |
| 884 | #if defined(HAS_SCALEUVCOLSUP2_SSSE3) |
| 885 | if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(dst_width, 8)) { |
| 886 | ScaleUVCols = ScaleUVColsUp2_SSSE3; |
| 887 | } |
| 888 | #endif |
| 889 | } |
| 890 |
| 891 | for (j = 0; j < dst_height; ++j) { |
| 892 | ScaleUVCols(dst_uv, src_uv + (y >> 16) * (intptr_t)src_stride, dst_width, x, |
| 893 | dx); |
| 894 | dst_uv += dst_stride; |
| 895 | y += dy; |
| 896 | } |
| 897 | } |
| 898 |
| 899 | // Copy UV with optional flipping |
| 900 | #if HAS_UVCOPY |
| 901 | static int UVCopy(const uint8_t* src_uv, |
| 902 | int src_stride_uv, |
| 903 | uint8_t* dst_uv, |
| 904 | int dst_stride_uv, |
| 905 | int width, |
| 906 | int height) { |
| 907 | if (!src_uv || !dst_uv || width <= 0 || height == 0) { |
| 908 | return -1; |
| 909 | } |
| 910 | // Negative height means invert the image. |
| 911 | if (height < 0) { |
| 912 | height = -height; |
| 913 | src_uv = src_uv + (height - 1) * (intptr_t)src_stride_uv; |
| 914 | src_stride_uv = -src_stride_uv; |
| 915 | } |
| 916 |
| 917 | CopyPlane(src_uv, src_stride_uv, dst_uv, dst_stride_uv, width * 2, height); |
| 918 | return 0; |
| 919 | } |
| 920 |
| 921 | static int UVCopy_16(const uint16_t* src_uv, |
| 922 | int src_stride_uv, |
| 923 | uint16_t* dst_uv, |
| 924 | int dst_stride_uv, |
| 925 | int width, |
| 926 | int height) { |
| 927 | if (!src_uv || !dst_uv || width <= 0 || height == 0) { |
| 928 | return -1; |
| 929 | } |
| 930 | // Negative height means invert the image. |
| 931 | if (height < 0) { |
| 932 | height = -height; |
| 933 | src_uv = src_uv + (height - 1) * (intptr_t)src_stride_uv; |
| 934 | src_stride_uv = -src_stride_uv; |
| 935 | } |
| 936 |
| 937 | CopyPlane_16(src_uv, src_stride_uv, dst_uv, dst_stride_uv, width * 2, height); |
| 938 | return 0; |
| 939 | } |
| 940 | #endif // HAS_UVCOPY |
| 941 |
| 942 | // Scale a UV plane (from NV12) |
| 943 | // This function in turn calls a scaling function |
| 944 | // suitable for handling the desired resolutions. |
| 945 | static void ScaleUV(const uint8_t* src, |
| 946 | int src_stride, |
| 947 | int src_width, |
| 948 | int src_height, |
| 949 | uint8_t* dst, |
| 950 | int dst_stride, |
| 951 | int dst_width, |
| 952 | int dst_height, |
| 953 | int clip_x, |
| 954 | int clip_y, |
| 955 | int clip_width, |
| 956 | int clip_height, |
| 957 | enum FilterMode filtering) { |
| 958 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 959 | int x = 0; |
| 960 | int y = 0; |
| 961 | int dx = 0; |
| 962 | int dy = 0; |
| 963 | // UV does not support box filter yet, but allow the user to pass it. |
| 964 | // Simplify filtering when possible. |
| 965 | filtering = ScaleFilterReduce(src_width, src_height, dst_width, dst_height, |
| 966 | filtering); |
| 967 |
| 968 | // Negative src_height means invert the image. |
| 969 | if (src_height < 0) { |
| 970 | src_height = -src_height; |
| 971 | src = src + (src_height - 1) * (intptr_t)src_stride; |
| 972 | src_stride = -src_stride; |
| 973 | } |
| 974 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, &x, &y, |
| 975 | &dx, &dy); |
| 976 | src_width = Abs(src_width); |
| 977 | if (clip_x) { |
| 978 | int64_t clipf = (int64_t)(clip_x)*dx; |
| 979 | x += (clipf & 0xffff); |
| 980 | src += (clipf >> 16) * 2; |
| 981 | dst += clip_x * 2; |
| 982 | } |
| 983 | if (clip_y) { |
| 984 | int64_t clipf = (int64_t)(clip_y)*dy; |
| 985 | y += (clipf & 0xffff); |
| 986 | src += (clipf >> 16) * (intptr_t)src_stride; |
| 987 | dst += clip_y * dst_stride; |
| 988 | } |
| 989 |
| 990 | // Special case for integer step values. |
| 991 | if (((dx | dy) & 0xffff) == 0) { |
| 992 | if (!dx || !dy) { // 1 pixel wide and/or tall. |
| 993 | filtering = kFilterNone; |
| 994 | } else { |
| 995 | // Optimized even scale down. ie 2, 4, 6, 8, 10x. |
| 996 | if (!(dx & 0x10000) && !(dy & 0x10000)) { |
| 997 | #if HAS_SCALEUVDOWN2 |
| 998 | if (dx == 0x20000) { |
| 999 | // Optimized 1/2 downsample. |
| 1000 | ScaleUVDown2(src_width, src_height, clip_width, clip_height, |
| 1001 | src_stride, dst_stride, src, dst, x, dx, y, dy, |
| 1002 | filtering); |
| 1003 | return; |
| 1004 | } |
| 1005 | #endif |
| 1006 | #if HAS_SCALEUVDOWN4BOX |
| 1007 | if (dx == 0x40000 && filtering == kFilterBox) { |
| 1008 | // Optimized 1/4 box downsample. |
| 1009 | ScaleUVDown4Box(src_width, src_height, clip_width, clip_height, |
| 1010 | src_stride, dst_stride, src, dst, x, dx, y, dy); |
| 1011 | return; |
| 1012 | } |
| 1013 | #endif |
| 1014 | #if HAS_SCALEUVDOWNEVEN |
| 1015 | ScaleUVDownEven(src_width, src_height, clip_width, clip_height, |
| 1016 | src_stride, dst_stride, src, dst, x, dx, y, dy, |
| 1017 | filtering); |
| 1018 | return; |
| 1019 | #endif |
| 1020 | } |
| 1021 | // Optimized odd scale down. ie 3, 5, 7, 9x. |
| 1022 | if ((dx & 0x10000) && (dy & 0x10000)) { |
| 1023 | filtering = kFilterNone; |
| 1024 | #ifdef HAS_UVCOPY |
| 1025 | if (dx == 0x10000 && dy == 0x10000) { |
| 1026 | // Straight copy. |
| 1027 | UVCopy(src + (y >> 16) * (intptr_t)src_stride + (x >> 16) * 2, |
| 1028 | src_stride, dst, dst_stride, clip_width, clip_height); |
| 1029 | return; |
| 1030 | } |
| 1031 | #endif |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | // HAS_SCALEPLANEVERTICAL |
| 1036 | if (dx == 0x10000 && (x & 0xffff) == 0) { |
| 1037 | // Arbitrary scale vertically, but unscaled horizontally. |
| 1038 | ScalePlaneVertical(src_height, clip_width, clip_height, src_stride, |
| 1039 | dst_stride, src, dst, x, y, dy, /*bpp=*/2, filtering); |
| 1040 | return; |
| 1041 | } |
| 1042 | if (filtering && (dst_width + 1) / 2 == src_width) { |
| 1043 | ScaleUVLinearUp2(src_width, src_height, clip_width, clip_height, src_stride, |
| 1044 | dst_stride, src, dst); |
| 1045 | return; |
| 1046 | } |
| 1047 | if ((clip_height + 1) / 2 == src_height && |
| 1048 | (clip_width + 1) / 2 == src_width && |
| 1049 | (filtering == kFilterBilinear || filtering == kFilterBox)) { |
| 1050 | ScaleUVBilinearUp2(src_width, src_height, clip_width, clip_height, |
| 1051 | src_stride, dst_stride, src, dst); |
| 1052 | return; |
| 1053 | } |
| 1054 | #if HAS_SCALEUVBILINEARUP |
| 1055 | if (filtering && dy < 65536) { |
| 1056 | ScaleUVBilinearUp(src_width, src_height, clip_width, clip_height, |
| 1057 | src_stride, dst_stride, src, dst, x, dx, y, dy, |
| 1058 | filtering); |
| 1059 | return; |
| 1060 | } |
| 1061 | #endif |
| 1062 | #if HAS_SCALEUVBILINEARDOWN |
| 1063 | if (filtering) { |
| 1064 | ScaleUVBilinearDown(src_width, src_height, clip_width, clip_height, |
| 1065 | src_stride, dst_stride, src, dst, x, dx, y, dy, |
| 1066 | filtering); |
| 1067 | return; |
| 1068 | } |
| 1069 | #endif |
| 1070 | ScaleUVSimple(src_width, src_height, clip_width, clip_height, src_stride, |
| 1071 | dst_stride, src, dst, x, dx, y, dy); |
| 1072 | } |
| 1073 |
| 1074 | // Scale an UV image. |
| 1075 | LIBYUV_API |
| 1076 | int UVScale(const uint8_t* src_uv, |
| 1077 | int src_stride_uv, |
| 1078 | int src_width, |
| 1079 | int src_height, |
| 1080 | uint8_t* dst_uv, |
| 1081 | int dst_stride_uv, |
| 1082 | int dst_width, |
| 1083 | int dst_height, |
| 1084 | enum FilterMode filtering) { |
| 1085 | if (!src_uv || src_width <= 0 || src_height == 0 || src_width > 32768 || |
| 1086 | src_height > 32768 || !dst_uv || dst_width <= 0 || dst_height <= 0) { |
| 1087 | return -1; |
| 1088 | } |
| 1089 | ScaleUV(src_uv, src_stride_uv, src_width, src_height, dst_uv, dst_stride_uv, |
| 1090 | dst_width, dst_height, 0, 0, dst_width, dst_height, filtering); |
| 1091 | return 0; |
| 1092 | } |
| 1093 |
| 1094 | // Scale a 16 bit UV image. |
| 1095 | // This function is currently incomplete, it can't handle all cases. |
| 1096 | LIBYUV_API |
| 1097 | int UVScale_16(const uint16_t* src_uv, |
| 1098 | int src_stride_uv, |
| 1099 | int src_width, |
| 1100 | int src_height, |
| 1101 | uint16_t* dst_uv, |
| 1102 | int dst_stride_uv, |
| 1103 | int dst_width, |
| 1104 | int dst_height, |
| 1105 | enum FilterMode filtering) { |
| 1106 | int dy = 0; |
| 1107 |
| 1108 | if (!src_uv || src_width <= 0 || src_height == 0 || src_width > 32768 || |
| 1109 | src_height > 32768 || !dst_uv || dst_width <= 0 || dst_height <= 0) { |
| 1110 | return -1; |
| 1111 | } |
| 1112 |
| 1113 | // UV does not support box filter yet, but allow the user to pass it. |
| 1114 | // Simplify filtering when possible. |
| 1115 | filtering = ScaleFilterReduce(src_width, src_height, dst_width, dst_height, |
| 1116 | filtering); |
| 1117 |
| 1118 | // Negative src_height means invert the image. |
| 1119 | if (src_height < 0) { |
| 1120 | src_height = -src_height; |
| 1121 | src_uv = src_uv + (src_height - 1) * (intptr_t)src_stride_uv; |
| 1122 | src_stride_uv = -src_stride_uv; |
| 1123 | } |
| 1124 | src_width = Abs(src_width); |
| 1125 |
| 1126 | #ifdef HAS_UVCOPY |
| 1127 | if (!filtering && src_width == dst_width && (src_height % dst_height == 0)) { |
| 1128 | if (dst_height == 1) { |
| 1129 | UVCopy_16(src_uv + ((src_height - 1) / 2) * (intptr_t)src_stride_uv, |
| 1130 | src_stride_uv, dst_uv, dst_stride_uv, dst_width, dst_height); |
| 1131 | } else { |
| 1132 | dy = src_height / dst_height; |
| 1133 | UVCopy_16(src_uv + ((dy - 1) / 2) * (intptr_t)src_stride_uv, |
| 1134 | (int)(dy * (intptr_t)src_stride_uv), dst_uv, dst_stride_uv, |
| 1135 | dst_width, dst_height); |
| 1136 | } |
| 1137 |
| 1138 | return 0; |
| 1139 | } |
| 1140 | #endif |
| 1141 |
| 1142 | if (filtering && (dst_width + 1) / 2 == src_width) { |
| 1143 | ScaleUVLinearUp2_16(src_width, src_height, dst_width, dst_height, |
| 1144 | src_stride_uv, dst_stride_uv, src_uv, dst_uv); |
| 1145 | return 0; |
| 1146 | } |
| 1147 |
| 1148 | if ((dst_height + 1) / 2 == src_height && (dst_width + 1) / 2 == src_width && |
| 1149 | (filtering == kFilterBilinear || filtering == kFilterBox)) { |
| 1150 | ScaleUVBilinearUp2_16(src_width, src_height, dst_width, dst_height, |
| 1151 | src_stride_uv, dst_stride_uv, src_uv, dst_uv); |
| 1152 | return 0; |
| 1153 | } |
| 1154 |
| 1155 | return -1; |
| 1156 | } |
| 1157 |
| 1158 | #ifdef __cplusplus |
| 1159 | } // extern "C" |
| 1160 | } // namespace libyuv |
| 1161 | #endif |
| 1162 |