| 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 CopyARGB |
| 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 | // ScaleARGB ARGB, 1/2 |
| 31 | // This is an optimized version for scaling down a ARGB to 1/2 of |
| 32 | // its original size. |
| 33 | static void ScaleARGBDown2(int src_width, int src_height, |
| 34 | int dst_width, int dst_height, |
| 35 | int src_stride, int dst_stride, |
| 36 | const uint8* src_argb, uint8* dst_argb, |
| 37 | int x, int dx, int y, int dy, |
| 38 | enum FilterMode filtering) { |
| 39 | int j; |
| 40 | int row_stride = src_stride * (dy >> 16); |
| 41 | void (*ScaleARGBRowDown2)(const uint8* src_argb, ptrdiff_t src_stride, |
| 42 | uint8* dst_argb, int dst_width) = |
| 43 | filtering == kFilterNone ? ScaleARGBRowDown2_C : |
| 44 | (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_C : |
| 45 | ScaleARGBRowDown2Box_C); |
| 46 | assert(dx == 65536 * 2); // Test scale factor of 2. |
| 47 | assert((dy & 0x1ffff) == 0); // Test vertical scale is multiple of 2. |
| 48 | // Advance to odd row, even column. |
| 49 | if (filtering == kFilterBilinear) { |
| 50 | src_argb += (y >> 16) * src_stride + (x >> 16) * 4; |
| 51 | } else { |
| 52 | src_argb += (y >> 16) * src_stride + ((x >> 16) - 1) * 4; |
| 53 | } |
| 54 |
| 55 | #if defined(HAS_SCALEARGBROWDOWN2_SSE2) |
| 56 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 57 | ScaleARGBRowDown2 = filtering == kFilterNone ? ScaleARGBRowDown2_Any_SSE2 : |
| 58 | (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_Any_SSE2 : |
| 59 | ScaleARGBRowDown2Box_Any_SSE2); |
| 60 | if (IS_ALIGNED(dst_width, 4)) { |
| 61 | ScaleARGBRowDown2 = filtering == kFilterNone ? ScaleARGBRowDown2_SSE2 : |
| 62 | (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_SSE2 : |
| 63 | ScaleARGBRowDown2Box_SSE2); |
| 64 | } |
| 65 | } |
| 66 | #endif |
| 67 | #if defined(HAS_SCALEARGBROWDOWN2_NEON) |
| 68 | if (TestCpuFlag(kCpuHasNEON)) { |
| 69 | ScaleARGBRowDown2 = filtering == kFilterNone ? ScaleARGBRowDown2_Any_NEON : |
| 70 | (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_Any_NEON : |
| 71 | ScaleARGBRowDown2Box_Any_NEON); |
| 72 | if (IS_ALIGNED(dst_width, 8)) { |
| 73 | ScaleARGBRowDown2 = filtering == kFilterNone ? ScaleARGBRowDown2_NEON : |
| 74 | (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_NEON : |
| 75 | ScaleARGBRowDown2Box_NEON); |
| 76 | } |
| 77 | } |
| 78 | #endif |
| 79 |
| 80 | if (filtering == kFilterLinear) { |
| 81 | src_stride = 0; |
| 82 | } |
| 83 | for (j = 0; j < dst_height; ++j) { |
| 84 | ScaleARGBRowDown2(src_argb, src_stride, dst_argb, dst_width); |
| 85 | src_argb += row_stride; |
| 86 | dst_argb += dst_stride; |
| 87 | } |
| 88 | } |
| 89 |
| 90 | // ScaleARGB ARGB, 1/4 |
| 91 | // This is an optimized version for scaling down a ARGB to 1/4 of |
| 92 | // its original size. |
| 93 | static void ScaleARGBDown4Box(int src_width, int src_height, |
| 94 | int dst_width, int dst_height, |
| 95 | int src_stride, int dst_stride, |
| 96 | const uint8* src_argb, uint8* dst_argb, |
| 97 | int x, int dx, int y, int dy) { |
| 98 | int j; |
| 99 | // Allocate 2 rows of ARGB. |
| 100 | const int kRowSize = (dst_width * 2 * 4 + 31) & ~31; |
| 101 | align_buffer_64(row, kRowSize * 2); |
| 102 | int row_stride = src_stride * (dy >> 16); |
| 103 | void (*ScaleARGBRowDown2)(const uint8* src_argb, ptrdiff_t src_stride, |
| 104 | uint8* dst_argb, int dst_width) = ScaleARGBRowDown2Box_C; |
| 105 | // Advance to odd row, even column. |
| 106 | src_argb += (y >> 16) * src_stride + (x >> 16) * 4; |
| 107 | assert(dx == 65536 * 4); // Test scale factor of 4. |
| 108 | assert((dy & 0x3ffff) == 0); // Test vertical scale is multiple of 4. |
| 109 | #if defined(HAS_SCALEARGBROWDOWN2_SSE2) |
| 110 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 111 | ScaleARGBRowDown2 = ScaleARGBRowDown2Box_Any_SSE2; |
| 112 | if (IS_ALIGNED(dst_width, 4)) { |
| 113 | ScaleARGBRowDown2 = ScaleARGBRowDown2Box_SSE2; |
| 114 | } |
| 115 | } |
| 116 | #endif |
| 117 | #if defined(HAS_SCALEARGBROWDOWN2_NEON) |
| 118 | if (TestCpuFlag(kCpuHasNEON)) { |
| 119 | ScaleARGBRowDown2 = ScaleARGBRowDown2Box_Any_NEON; |
| 120 | if (IS_ALIGNED(dst_width, 8)) { |
| 121 | ScaleARGBRowDown2 = ScaleARGBRowDown2Box_NEON; |
| 122 | } |
| 123 | } |
| 124 | #endif |
| 125 |
| 126 | for (j = 0; j < dst_height; ++j) { |
| 127 | ScaleARGBRowDown2(src_argb, src_stride, row, dst_width * 2); |
| 128 | ScaleARGBRowDown2(src_argb + src_stride * 2, src_stride, |
| 129 | row + kRowSize, dst_width * 2); |
| 130 | ScaleARGBRowDown2(row, kRowSize, dst_argb, dst_width); |
| 131 | src_argb += row_stride; |
| 132 | dst_argb += dst_stride; |
| 133 | } |
| 134 | free_aligned_buffer_64(row); |
| 135 | } |
| 136 |
| 137 | // ScaleARGB ARGB Even |
| 138 | // This is an optimized version for scaling down a ARGB to even |
| 139 | // multiple of its original size. |
| 140 | static void ScaleARGBDownEven(int src_width, int src_height, |
| 141 | int dst_width, int dst_height, |
| 142 | int src_stride, int dst_stride, |
| 143 | const uint8* src_argb, uint8* dst_argb, |
| 144 | int x, int dx, int y, int dy, |
| 145 | enum FilterMode filtering) { |
| 146 | int j; |
| 147 | int col_step = dx >> 16; |
| 148 | int row_stride = (dy >> 16) * src_stride; |
| 149 | void (*ScaleARGBRowDownEven)(const uint8* src_argb, ptrdiff_t src_stride, |
| 150 | int src_step, uint8* dst_argb, int dst_width) = |
| 151 | filtering ? ScaleARGBRowDownEvenBox_C : ScaleARGBRowDownEven_C; |
| 152 | assert(IS_ALIGNED(src_width, 2)); |
| 153 | assert(IS_ALIGNED(src_height, 2)); |
| 154 | src_argb += (y >> 16) * src_stride + (x >> 16) * 4; |
| 155 | #if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2) |
| 156 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 157 | ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_Any_SSE2 : |
| 158 | ScaleARGBRowDownEven_Any_SSE2; |
| 159 | if (IS_ALIGNED(dst_width, 4)) { |
| 160 | ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_SSE2 : |
| 161 | ScaleARGBRowDownEven_SSE2; |
| 162 | } |
| 163 | } |
| 164 | #endif |
| 165 | #if defined(HAS_SCALEARGBROWDOWNEVEN_NEON) |
| 166 | if (TestCpuFlag(kCpuHasNEON)) { |
| 167 | ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_Any_NEON : |
| 168 | ScaleARGBRowDownEven_Any_NEON; |
| 169 | if (IS_ALIGNED(dst_width, 4)) { |
| 170 | ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_NEON : |
| 171 | ScaleARGBRowDownEven_NEON; |
| 172 | } |
| 173 | } |
| 174 | #endif |
| 175 |
| 176 | if (filtering == kFilterLinear) { |
| 177 | src_stride = 0; |
| 178 | } |
| 179 | for (j = 0; j < dst_height; ++j) { |
| 180 | ScaleARGBRowDownEven(src_argb, src_stride, col_step, dst_argb, dst_width); |
| 181 | src_argb += row_stride; |
| 182 | dst_argb += dst_stride; |
| 183 | } |
| 184 | } |
| 185 |
| 186 | // Scale ARGB down with bilinear interpolation. |
| 187 | static void ScaleARGBBilinearDown(int src_width, int src_height, |
| 188 | int dst_width, int dst_height, |
| 189 | int src_stride, int dst_stride, |
| 190 | const uint8* src_argb, uint8* dst_argb, |
| 191 | int x, int dx, int y, int dy, |
| 192 | enum FilterMode filtering) { |
| 193 | int j; |
| 194 | void (*InterpolateRow)(uint8* dst_argb, const uint8* src_argb, |
| 195 | ptrdiff_t src_stride, int dst_width, int source_y_fraction) = |
| 196 | InterpolateRow_C; |
| 197 | void (*ScaleARGBFilterCols)(uint8* dst_argb, const uint8* src_argb, |
| 198 | int dst_width, int x, int dx) = |
| 199 | (src_width >= 32768) ? ScaleARGBFilterCols64_C : ScaleARGBFilterCols_C; |
| 200 | int64 xlast = x + (int64)(dst_width - 1) * dx; |
| 201 | int64 xl = (dx >= 0) ? x : xlast; |
| 202 | int64 xr = (dx >= 0) ? xlast : x; |
| 203 | int clip_src_width; |
| 204 | xl = (xl >> 16) & ~3; // Left edge aligned. |
| 205 | xr = (xr >> 16) + 1; // Right most pixel used. Bilinear uses 2 pixels. |
| 206 | xr = (xr + 1 + 3) & ~3; // 1 beyond 4 pixel aligned right most pixel. |
| 207 | if (xr > src_width) { |
| 208 | xr = src_width; |
| 209 | } |
| 210 | clip_src_width = (int)(xr - xl) * 4; // Width aligned to 4. |
| 211 | src_argb += xl * 4; |
| 212 | x -= (int)(xl << 16); |
| 213 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 214 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 215 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 216 | if (IS_ALIGNED(clip_src_width, 16)) { |
| 217 | InterpolateRow = InterpolateRow_SSSE3; |
| 218 | } |
| 219 | } |
| 220 | #endif |
| 221 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 222 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 223 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 224 | if (IS_ALIGNED(clip_src_width, 32)) { |
| 225 | InterpolateRow = InterpolateRow_AVX2; |
| 226 | } |
| 227 | } |
| 228 | #endif |
| 229 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 230 | if (TestCpuFlag(kCpuHasNEON)) { |
| 231 | InterpolateRow = InterpolateRow_Any_NEON; |
| 232 | if (IS_ALIGNED(clip_src_width, 16)) { |
| 233 | InterpolateRow = InterpolateRow_NEON; |
| 234 | } |
| 235 | } |
| 236 | #endif |
| 237 | #if defined(HAS_INTERPOLATEROW_DSPR2) |
| 238 | if (TestCpuFlag(kCpuHasDSPR2) && |
| 239 | IS_ALIGNED(src_argb, 4) && IS_ALIGNED(src_stride, 4)) { |
| 240 | InterpolateRow = InterpolateRow_Any_DSPR2; |
| 241 | if (IS_ALIGNED(clip_src_width, 4)) { |
| 242 | InterpolateRow = InterpolateRow_DSPR2; |
| 243 | } |
| 244 | } |
| 245 | #endif |
| 246 | #if defined(HAS_SCALEARGBFILTERCOLS_SSSE3) |
| 247 | if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 248 | ScaleARGBFilterCols = ScaleARGBFilterCols_SSSE3; |
| 249 | } |
| 250 | #endif |
| 251 | #if defined(HAS_SCALEARGBFILTERCOLS_NEON) |
| 252 | if (TestCpuFlag(kCpuHasNEON)) { |
| 253 | ScaleARGBFilterCols = ScaleARGBFilterCols_Any_NEON; |
| 254 | if (IS_ALIGNED(dst_width, 4)) { |
| 255 | ScaleARGBFilterCols = ScaleARGBFilterCols_NEON; |
| 256 | } |
| 257 | } |
| 258 | #endif |
| 259 | // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear. |
| 260 | // Allocate a row of ARGB. |
| 261 | { |
| 262 | align_buffer_64(row, clip_src_width * 4); |
| 263 |
| 264 | const int max_y = (src_height - 1) << 16; |
| 265 | if (y > max_y) { |
| 266 | y = max_y; |
| 267 | } |
| 268 | for (j = 0; j < dst_height; ++j) { |
| 269 | int yi = y >> 16; |
| 270 | const uint8* src = src_argb + yi * src_stride; |
| 271 | if (filtering == kFilterLinear) { |
| 272 | ScaleARGBFilterCols(dst_argb, src, dst_width, x, dx); |
| 273 | } else { |
| 274 | int yf = (y >> 8) & 255; |
| 275 | InterpolateRow(row, src, src_stride, clip_src_width, yf); |
| 276 | ScaleARGBFilterCols(dst_argb, row, dst_width, x, dx); |
| 277 | } |
| 278 | dst_argb += dst_stride; |
| 279 | y += dy; |
| 280 | if (y > max_y) { |
| 281 | y = max_y; |
| 282 | } |
| 283 | } |
| 284 | free_aligned_buffer_64(row); |
| 285 | } |
| 286 | } |
| 287 |
| 288 | // Scale ARGB up with bilinear interpolation. |
| 289 | static void ScaleARGBBilinearUp(int src_width, int src_height, |
| 290 | int dst_width, int dst_height, |
| 291 | int src_stride, int dst_stride, |
| 292 | const uint8* src_argb, uint8* dst_argb, |
| 293 | int x, int dx, int y, int dy, |
| 294 | enum FilterMode filtering) { |
| 295 | int j; |
| 296 | void (*InterpolateRow)(uint8* dst_argb, const uint8* src_argb, |
| 297 | ptrdiff_t src_stride, int dst_width, int source_y_fraction) = |
| 298 | InterpolateRow_C; |
| 299 | void (*ScaleARGBFilterCols)(uint8* dst_argb, const uint8* src_argb, |
| 300 | int dst_width, int x, int dx) = |
| 301 | filtering ? ScaleARGBFilterCols_C : ScaleARGBCols_C; |
| 302 | const int max_y = (src_height - 1) << 16; |
| 303 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 304 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 305 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 306 | if (IS_ALIGNED(dst_width, 4)) { |
| 307 | InterpolateRow = InterpolateRow_SSSE3; |
| 308 | } |
| 309 | } |
| 310 | #endif |
| 311 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 312 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 313 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 314 | if (IS_ALIGNED(dst_width, 8)) { |
| 315 | InterpolateRow = InterpolateRow_AVX2; |
| 316 | } |
| 317 | } |
| 318 | #endif |
| 319 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 320 | if (TestCpuFlag(kCpuHasNEON)) { |
| 321 | InterpolateRow = InterpolateRow_Any_NEON; |
| 322 | if (IS_ALIGNED(dst_width, 4)) { |
| 323 | InterpolateRow = InterpolateRow_NEON; |
| 324 | } |
| 325 | } |
| 326 | #endif |
| 327 | #if defined(HAS_INTERPOLATEROW_DSPR2) |
| 328 | if (TestCpuFlag(kCpuHasDSPR2) && |
| 329 | IS_ALIGNED(dst_argb, 4) && IS_ALIGNED(dst_stride, 4)) { |
| 330 | InterpolateRow = InterpolateRow_DSPR2; |
| 331 | } |
| 332 | #endif |
| 333 | if (src_width >= 32768) { |
| 334 | ScaleARGBFilterCols = filtering ? |
| 335 | ScaleARGBFilterCols64_C : ScaleARGBCols64_C; |
| 336 | } |
| 337 | #if defined(HAS_SCALEARGBFILTERCOLS_SSSE3) |
| 338 | if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 339 | ScaleARGBFilterCols = ScaleARGBFilterCols_SSSE3; |
| 340 | } |
| 341 | #endif |
| 342 | #if defined(HAS_SCALEARGBFILTERCOLS_NEON) |
| 343 | if (filtering && TestCpuFlag(kCpuHasNEON)) { |
| 344 | ScaleARGBFilterCols = ScaleARGBFilterCols_Any_NEON; |
| 345 | if (IS_ALIGNED(dst_width, 4)) { |
| 346 | ScaleARGBFilterCols = ScaleARGBFilterCols_NEON; |
| 347 | } |
| 348 | } |
| 349 | #endif |
| 350 | #if defined(HAS_SCALEARGBCOLS_SSE2) |
| 351 | if (!filtering && TestCpuFlag(kCpuHasSSE2) && src_width < 32768) { |
| 352 | ScaleARGBFilterCols = ScaleARGBCols_SSE2; |
| 353 | } |
| 354 | #endif |
| 355 | #if defined(HAS_SCALEARGBCOLS_NEON) |
| 356 | if (!filtering && TestCpuFlag(kCpuHasNEON)) { |
| 357 | ScaleARGBFilterCols = ScaleARGBCols_Any_NEON; |
| 358 | if (IS_ALIGNED(dst_width, 8)) { |
| 359 | ScaleARGBFilterCols = ScaleARGBCols_NEON; |
| 360 | } |
| 361 | } |
| 362 | #endif |
| 363 | if (!filtering && src_width * 2 == dst_width && x < 0x8000) { |
| 364 | ScaleARGBFilterCols = ScaleARGBColsUp2_C; |
| 365 | #if defined(HAS_SCALEARGBCOLSUP2_SSE2) |
| 366 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 367 | ScaleARGBFilterCols = ScaleARGBColsUp2_SSE2; |
| 368 | } |
| 369 | #endif |
| 370 | } |
| 371 |
| 372 | if (y > max_y) { |
| 373 | y = max_y; |
| 374 | } |
| 375 |
| 376 | { |
| 377 | int yi = y >> 16; |
| 378 | const uint8* src = src_argb + yi * src_stride; |
| 379 |
| 380 | // Allocate 2 rows of ARGB. |
| 381 | const int kRowSize = (dst_width * 4 + 31) & ~31; |
| 382 | align_buffer_64(row, kRowSize * 2); |
| 383 |
| 384 | uint8* rowptr = row; |
| 385 | int rowstride = kRowSize; |
| 386 | int lasty = yi; |
| 387 |
| 388 | ScaleARGBFilterCols(rowptr, src, dst_width, x, dx); |
| 389 | if (src_height > 1) { |
| 390 | src += src_stride; |
| 391 | } |
| 392 | ScaleARGBFilterCols(rowptr + rowstride, src, dst_width, x, dx); |
| 393 | src += src_stride; |
| 394 |
| 395 | for (j = 0; j < dst_height; ++j) { |
| 396 | yi = y >> 16; |
| 397 | if (yi != lasty) { |
| 398 | if (y > max_y) { |
| 399 | y = max_y; |
| 400 | yi = y >> 16; |
| 401 | src = src_argb + yi * src_stride; |
| 402 | } |
| 403 | if (yi != lasty) { |
| 404 | ScaleARGBFilterCols(rowptr, src, dst_width, x, dx); |
| 405 | rowptr += rowstride; |
| 406 | rowstride = -rowstride; |
| 407 | lasty = yi; |
| 408 | src += src_stride; |
| 409 | } |
| 410 | } |
| 411 | if (filtering == kFilterLinear) { |
| 412 | InterpolateRow(dst_argb, rowptr, 0, dst_width * 4, 0); |
| 413 | } else { |
| 414 | int yf = (y >> 8) & 255; |
| 415 | InterpolateRow(dst_argb, rowptr, rowstride, dst_width * 4, yf); |
| 416 | } |
| 417 | dst_argb += dst_stride; |
| 418 | y += dy; |
| 419 | } |
| 420 | free_aligned_buffer_64(row); |
| 421 | } |
| 422 | } |
| 423 |
| 424 | #ifdef YUVSCALEUP |
| 425 | // Scale YUV to ARGB up with bilinear interpolation. |
| 426 | static void ScaleYUVToARGBBilinearUp(int src_width, int src_height, |
| 427 | int dst_width, int dst_height, |
| 428 | int src_stride_y, |
| 429 | int src_stride_u, |
| 430 | int src_stride_v, |
| 431 | int dst_stride_argb, |
| 432 | const uint8* src_y, |
| 433 | const uint8* src_u, |
| 434 | const uint8* src_v, |
| 435 | uint8* dst_argb, |
| 436 | int x, int dx, int y, int dy, |
| 437 | enum FilterMode filtering) { |
| 438 | int j; |
| 439 | void (*I422ToARGBRow)(const uint8* y_buf, |
| 440 | const uint8* u_buf, |
| 441 | const uint8* v_buf, |
| 442 | uint8* rgb_buf, |
| 443 | int width) = I422ToARGBRow_C; |
| 444 | #if defined(HAS_I422TOARGBROW_SSSE3) |
| 445 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 446 | I422ToARGBRow = I422ToARGBRow_Any_SSSE3; |
| 447 | if (IS_ALIGNED(src_width, 8)) { |
| 448 | I422ToARGBRow = I422ToARGBRow_SSSE3; |
| 449 | } |
| 450 | } |
| 451 | #endif |
| 452 | #if defined(HAS_I422TOARGBROW_AVX2) |
| 453 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 454 | I422ToARGBRow = I422ToARGBRow_Any_AVX2; |
| 455 | if (IS_ALIGNED(src_width, 16)) { |
| 456 | I422ToARGBRow = I422ToARGBRow_AVX2; |
| 457 | } |
| 458 | } |
| 459 | #endif |
| 460 | #if defined(HAS_I422TOARGBROW_NEON) |
| 461 | if (TestCpuFlag(kCpuHasNEON)) { |
| 462 | I422ToARGBRow = I422ToARGBRow_Any_NEON; |
| 463 | if (IS_ALIGNED(src_width, 8)) { |
| 464 | I422ToARGBRow = I422ToARGBRow_NEON; |
| 465 | } |
| 466 | } |
| 467 | #endif |
| 468 | #if defined(HAS_I422TOARGBROW_DSPR2) |
| 469 | if (TestCpuFlag(kCpuHasDSPR2) && IS_ALIGNED(src_width, 4) && |
| 470 | IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) && |
| 471 | IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) && |
| 472 | IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2) && |
| 473 | IS_ALIGNED(dst_argb, 4) && IS_ALIGNED(dst_stride_argb, 4)) { |
| 474 | I422ToARGBRow = I422ToARGBRow_DSPR2; |
| 475 | } |
| 476 | #endif |
| 477 |
| 478 | void (*InterpolateRow)(uint8* dst_argb, const uint8* src_argb, |
| 479 | ptrdiff_t src_stride, int dst_width, int source_y_fraction) = |
| 480 | InterpolateRow_C; |
| 481 | #if defined(HAS_INTERPOLATEROW_SSSE3) |
| 482 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 483 | InterpolateRow = InterpolateRow_Any_SSSE3; |
| 484 | if (IS_ALIGNED(dst_width, 4)) { |
| 485 | InterpolateRow = InterpolateRow_SSSE3; |
| 486 | } |
| 487 | } |
| 488 | #endif |
| 489 | #if defined(HAS_INTERPOLATEROW_AVX2) |
| 490 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 491 | InterpolateRow = InterpolateRow_Any_AVX2; |
| 492 | if (IS_ALIGNED(dst_width, 8)) { |
| 493 | InterpolateRow = InterpolateRow_AVX2; |
| 494 | } |
| 495 | } |
| 496 | #endif |
| 497 | #if defined(HAS_INTERPOLATEROW_NEON) |
| 498 | if (TestCpuFlag(kCpuHasNEON)) { |
| 499 | InterpolateRow = InterpolateRow_Any_NEON; |
| 500 | if (IS_ALIGNED(dst_width, 4)) { |
| 501 | InterpolateRow = InterpolateRow_NEON; |
| 502 | } |
| 503 | } |
| 504 | #endif |
| 505 | #if defined(HAS_INTERPOLATEROW_DSPR2) |
| 506 | if (TestCpuFlag(kCpuHasDSPR2) && |
| 507 | IS_ALIGNED(dst_argb, 4) && IS_ALIGNED(dst_stride_argb, 4)) { |
| 508 | InterpolateRow = InterpolateRow_DSPR2; |
| 509 | } |
| 510 | #endif |
| 511 |
| 512 | void (*ScaleARGBFilterCols)(uint8* dst_argb, const uint8* src_argb, |
| 513 | int dst_width, int x, int dx) = |
| 514 | filtering ? ScaleARGBFilterCols_C : ScaleARGBCols_C; |
| 515 | if (src_width >= 32768) { |
| 516 | ScaleARGBFilterCols = filtering ? |
| 517 | ScaleARGBFilterCols64_C : ScaleARGBCols64_C; |
| 518 | } |
| 519 | #if defined(HAS_SCALEARGBFILTERCOLS_SSSE3) |
| 520 | if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) { |
| 521 | ScaleARGBFilterCols = ScaleARGBFilterCols_SSSE3; |
| 522 | } |
| 523 | #endif |
| 524 | #if defined(HAS_SCALEARGBFILTERCOLS_NEON) |
| 525 | if (filtering && TestCpuFlag(kCpuHasNEON)) { |
| 526 | ScaleARGBFilterCols = ScaleARGBFilterCols_Any_NEON; |
| 527 | if (IS_ALIGNED(dst_width, 4)) { |
| 528 | ScaleARGBFilterCols = ScaleARGBFilterCols_NEON; |
| 529 | } |
| 530 | } |
| 531 | #endif |
| 532 | #if defined(HAS_SCALEARGBCOLS_SSE2) |
| 533 | if (!filtering && TestCpuFlag(kCpuHasSSE2) && src_width < 32768) { |
| 534 | ScaleARGBFilterCols = ScaleARGBCols_SSE2; |
| 535 | } |
| 536 | #endif |
| 537 | #if defined(HAS_SCALEARGBCOLS_NEON) |
| 538 | if (!filtering && TestCpuFlag(kCpuHasNEON)) { |
| 539 | ScaleARGBFilterCols = ScaleARGBCols_Any_NEON; |
| 540 | if (IS_ALIGNED(dst_width, 8)) { |
| 541 | ScaleARGBFilterCols = ScaleARGBCols_NEON; |
| 542 | } |
| 543 | } |
| 544 | #endif |
| 545 | if (!filtering && src_width * 2 == dst_width && x < 0x8000) { |
| 546 | ScaleARGBFilterCols = ScaleARGBColsUp2_C; |
| 547 | #if defined(HAS_SCALEARGBCOLSUP2_SSE2) |
| 548 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 549 | ScaleARGBFilterCols = ScaleARGBColsUp2_SSE2; |
| 550 | } |
| 551 | #endif |
| 552 | } |
| 553 |
| 554 | const int max_y = (src_height - 1) << 16; |
| 555 | if (y > max_y) { |
| 556 | y = max_y; |
| 557 | } |
| 558 | const int kYShift = 1; // Shift Y by 1 to convert Y plane to UV coordinate. |
| 559 | int yi = y >> 16; |
| 560 | int uv_yi = yi >> kYShift; |
| 561 | const uint8* src_row_y = src_y + yi * src_stride_y; |
| 562 | const uint8* src_row_u = src_u + uv_yi * src_stride_u; |
| 563 | const uint8* src_row_v = src_v + uv_yi * src_stride_v; |
| 564 |
| 565 | // Allocate 2 rows of ARGB. |
| 566 | const int kRowSize = (dst_width * 4 + 31) & ~31; |
| 567 | align_buffer_64(row, kRowSize * 2); |
| 568 |
| 569 | // Allocate 1 row of ARGB for source conversion. |
| 570 | align_buffer_64(argb_row, src_width * 4); |
| 571 |
| 572 | uint8* rowptr = row; |
| 573 | int rowstride = kRowSize; |
| 574 | int lasty = yi; |
| 575 |
| 576 | // TODO(fbarchard): Convert first 2 rows of YUV to ARGB. |
| 577 | ScaleARGBFilterCols(rowptr, src_row_y, dst_width, x, dx); |
| 578 | if (src_height > 1) { |
| 579 | src_row_y += src_stride_y; |
| 580 | if (yi & 1) { |
| 581 | src_row_u += src_stride_u; |
| 582 | src_row_v += src_stride_v; |
| 583 | } |
| 584 | } |
| 585 | ScaleARGBFilterCols(rowptr + rowstride, src_row_y, dst_width, x, dx); |
| 586 | if (src_height > 2) { |
| 587 | src_row_y += src_stride_y; |
| 588 | if (!(yi & 1)) { |
| 589 | src_row_u += src_stride_u; |
| 590 | src_row_v += src_stride_v; |
| 591 | } |
| 592 | } |
| 593 |
| 594 | for (j = 0; j < dst_height; ++j) { |
| 595 | yi = y >> 16; |
| 596 | if (yi != lasty) { |
| 597 | if (y > max_y) { |
| 598 | y = max_y; |
| 599 | yi = y >> 16; |
| 600 | uv_yi = yi >> kYShift; |
| 601 | src_row_y = src_y + yi * src_stride_y; |
| 602 | src_row_u = src_u + uv_yi * src_stride_u; |
| 603 | src_row_v = src_v + uv_yi * src_stride_v; |
| 604 | } |
| 605 | if (yi != lasty) { |
| 606 | // TODO(fbarchard): Convert the clipped region of row. |
| 607 | I422ToARGBRow(src_row_y, src_row_u, src_row_v, argb_row, src_width); |
| 608 | ScaleARGBFilterCols(rowptr, argb_row, dst_width, x, dx); |
| 609 | rowptr += rowstride; |
| 610 | rowstride = -rowstride; |
| 611 | lasty = yi; |
| 612 | src_row_y += src_stride_y; |
| 613 | if (yi & 1) { |
| 614 | src_row_u += src_stride_u; |
| 615 | src_row_v += src_stride_v; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | if (filtering == kFilterLinear) { |
| 620 | InterpolateRow(dst_argb, rowptr, 0, dst_width * 4, 0); |
| 621 | } else { |
| 622 | int yf = (y >> 8) & 255; |
| 623 | InterpolateRow(dst_argb, rowptr, rowstride, dst_width * 4, yf); |
| 624 | } |
| 625 | dst_argb += dst_stride_argb; |
| 626 | y += dy; |
| 627 | } |
| 628 | free_aligned_buffer_64(row); |
| 629 | free_aligned_buffer_64(row_argb); |
| 630 | } |
| 631 | #endif |
| 632 |
| 633 | // Scale ARGB to/from any dimensions, without interpolation. |
| 634 | // Fixed point math is used for performance: The upper 16 bits |
| 635 | // of x and dx is the integer part of the source position and |
| 636 | // the lower 16 bits are the fixed decimal part. |
| 637 |
| 638 | static void ScaleARGBSimple(int src_width, int src_height, |
| 639 | int dst_width, int dst_height, |
| 640 | int src_stride, int dst_stride, |
| 641 | const uint8* src_argb, uint8* dst_argb, |
| 642 | int x, int dx, int y, int dy) { |
| 643 | int j; |
| 644 | void (*ScaleARGBCols)(uint8* dst_argb, const uint8* src_argb, |
| 645 | int dst_width, int x, int dx) = |
| 646 | (src_width >= 32768) ? ScaleARGBCols64_C : ScaleARGBCols_C; |
| 647 | #if defined(HAS_SCALEARGBCOLS_SSE2) |
| 648 | if (TestCpuFlag(kCpuHasSSE2) && src_width < 32768) { |
| 649 | ScaleARGBCols = ScaleARGBCols_SSE2; |
| 650 | } |
| 651 | #endif |
| 652 | #if defined(HAS_SCALEARGBCOLS_NEON) |
| 653 | if (TestCpuFlag(kCpuHasNEON)) { |
| 654 | ScaleARGBCols = ScaleARGBCols_Any_NEON; |
| 655 | if (IS_ALIGNED(dst_width, 8)) { |
| 656 | ScaleARGBCols = ScaleARGBCols_NEON; |
| 657 | } |
| 658 | } |
| 659 | #endif |
| 660 | if (src_width * 2 == dst_width && x < 0x8000) { |
| 661 | ScaleARGBCols = ScaleARGBColsUp2_C; |
| 662 | #if defined(HAS_SCALEARGBCOLSUP2_SSE2) |
| 663 | if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) { |
| 664 | ScaleARGBCols = ScaleARGBColsUp2_SSE2; |
| 665 | } |
| 666 | #endif |
| 667 | } |
| 668 |
| 669 | for (j = 0; j < dst_height; ++j) { |
| 670 | ScaleARGBCols(dst_argb, src_argb + (y >> 16) * src_stride, |
| 671 | dst_width, x, dx); |
| 672 | dst_argb += dst_stride; |
| 673 | y += dy; |
| 674 | } |
| 675 | } |
| 676 |
| 677 | // ScaleARGB a ARGB. |
| 678 | // This function in turn calls a scaling function |
| 679 | // suitable for handling the desired resolutions. |
| 680 | static void ScaleARGB(const uint8* src, int src_stride, |
| 681 | int src_width, int src_height, |
| 682 | uint8* dst, int dst_stride, |
| 683 | int dst_width, int dst_height, |
| 684 | int clip_x, int clip_y, int clip_width, int clip_height, |
| 685 | enum FilterMode filtering) { |
| 686 | // Initial source x/y coordinate and step values as 16.16 fixed point. |
| 687 | int x = 0; |
| 688 | int y = 0; |
| 689 | int dx = 0; |
| 690 | int dy = 0; |
| 691 | // ARGB does not support box filter yet, but allow the user to pass it. |
| 692 | // Simplify filtering when possible. |
| 693 | filtering = ScaleFilterReduce(src_width, src_height, |
| 694 | dst_width, dst_height, |
| 695 | filtering); |
| 696 |
| 697 | // Negative src_height means invert the image. |
| 698 | if (src_height < 0) { |
| 699 | src_height = -src_height; |
| 700 | src = src + (src_height - 1) * src_stride; |
| 701 | src_stride = -src_stride; |
| 702 | } |
| 703 | ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, |
| 704 | &x, &y, &dx, &dy); |
| 705 | src_width = Abs(src_width); |
| 706 | if (clip_x) { |
| 707 | int64 clipf = (int64)(clip_x) * dx; |
| 708 | x += (clipf & 0xffff); |
| 709 | src += (clipf >> 16) * 4; |
| 710 | dst += clip_x * 4; |
| 711 | } |
| 712 | if (clip_y) { |
| 713 | int64 clipf = (int64)(clip_y) * dy; |
| 714 | y += (clipf & 0xffff); |
| 715 | src += (clipf >> 16) * src_stride; |
| 716 | dst += clip_y * dst_stride; |
| 717 | } |
| 718 |
| 719 | // Special case for integer step values. |
| 720 | if (((dx | dy) & 0xffff) == 0) { |
| 721 | if (!dx || !dy) { // 1 pixel wide and/or tall. |
| 722 | filtering = kFilterNone; |
| 723 | } else { |
| 724 | // Optimized even scale down. ie 2, 4, 6, 8, 10x. |
| 725 | if (!(dx & 0x10000) && !(dy & 0x10000)) { |
| 726 | if (dx == 0x20000) { |
| 727 | // Optimized 1/2 downsample. |
| 728 | ScaleARGBDown2(src_width, src_height, |
| 729 | clip_width, clip_height, |
| 730 | src_stride, dst_stride, src, dst, |
| 731 | x, dx, y, dy, filtering); |
| 732 | return; |
| 733 | } |
| 734 | if (dx == 0x40000 && filtering == kFilterBox) { |
| 735 | // Optimized 1/4 box downsample. |
| 736 | ScaleARGBDown4Box(src_width, src_height, |
| 737 | clip_width, clip_height, |
| 738 | src_stride, dst_stride, src, dst, |
| 739 | x, dx, y, dy); |
| 740 | return; |
| 741 | } |
| 742 | ScaleARGBDownEven(src_width, src_height, |
| 743 | clip_width, clip_height, |
| 744 | src_stride, dst_stride, src, dst, |
| 745 | x, dx, y, dy, filtering); |
| 746 | return; |
| 747 | } |
| 748 | // Optimized odd scale down. ie 3, 5, 7, 9x. |
| 749 | if ((dx & 0x10000) && (dy & 0x10000)) { |
| 750 | filtering = kFilterNone; |
| 751 | if (dx == 0x10000 && dy == 0x10000) { |
| 752 | // Straight copy. |
| 753 | ARGBCopy(src + (y >> 16) * src_stride + (x >> 16) * 4, src_stride, |
| 754 | dst, dst_stride, clip_width, clip_height); |
| 755 | return; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | if (dx == 0x10000 && (x & 0xffff) == 0) { |
| 761 | // Arbitrary scale vertically, but unscaled vertically. |
| 762 | ScalePlaneVertical(src_height, |
| 763 | clip_width, clip_height, |
| 764 | src_stride, dst_stride, src, dst, |
| 765 | x, y, dy, 4, filtering); |
| 766 | return; |
| 767 | } |
| 768 | if (filtering && dy < 65536) { |
| 769 | ScaleARGBBilinearUp(src_width, src_height, |
| 770 | clip_width, clip_height, |
| 771 | src_stride, dst_stride, src, dst, |
| 772 | x, dx, y, dy, filtering); |
| 773 | return; |
| 774 | } |
| 775 | if (filtering) { |
| 776 | ScaleARGBBilinearDown(src_width, src_height, |
| 777 | clip_width, clip_height, |
| 778 | src_stride, dst_stride, src, dst, |
| 779 | x, dx, y, dy, filtering); |
| 780 | return; |
| 781 | } |
| 782 | ScaleARGBSimple(src_width, src_height, clip_width, clip_height, |
| 783 | src_stride, dst_stride, src, dst, |
| 784 | x, dx, y, dy); |
| 785 | } |
| 786 |
| 787 | LIBYUV_API |
| 788 | int ARGBScaleClip(const uint8* src_argb, int src_stride_argb, |
| 789 | int src_width, int src_height, |
| 790 | uint8* dst_argb, int dst_stride_argb, |
| 791 | int dst_width, int dst_height, |
| 792 | int clip_x, int clip_y, int clip_width, int clip_height, |
| 793 | enum FilterMode filtering) { |
| 794 | if (!src_argb || src_width == 0 || src_height == 0 || |
| 795 | !dst_argb || dst_width <= 0 || dst_height <= 0 || |
| 796 | clip_x < 0 || clip_y < 0 || |
| 797 | clip_width > 32768 || clip_height > 32768 || |
| 798 | (clip_x + clip_width) > dst_width || |
| 799 | (clip_y + clip_height) > dst_height) { |
| 800 | return -1; |
| 801 | } |
| 802 | ScaleARGB(src_argb, src_stride_argb, src_width, src_height, |
| 803 | dst_argb, dst_stride_argb, dst_width, dst_height, |
| 804 | clip_x, clip_y, clip_width, clip_height, filtering); |
| 805 | return 0; |
| 806 | } |
| 807 |
| 808 | // Scale an ARGB image. |
| 809 | LIBYUV_API |
| 810 | int ARGBScale(const uint8* src_argb, int src_stride_argb, |
| 811 | int src_width, int src_height, |
| 812 | uint8* dst_argb, int dst_stride_argb, |
| 813 | int dst_width, int dst_height, |
| 814 | enum FilterMode filtering) { |
| 815 | if (!src_argb || src_width == 0 || src_height == 0 || |
| 816 | src_width > 32768 || src_height > 32768 || |
| 817 | !dst_argb || dst_width <= 0 || dst_height <= 0) { |
| 818 | return -1; |
| 819 | } |
| 820 | ScaleARGB(src_argb, src_stride_argb, src_width, src_height, |
| 821 | dst_argb, dst_stride_argb, dst_width, dst_height, |
| 822 | 0, 0, dst_width, dst_height, filtering); |
| 823 | return 0; |
| 824 | } |
| 825 |
| 826 | // Scale with YUV conversion to ARGB and clipping. |
| 827 | LIBYUV_API |
| 828 | int YUVToARGBScaleClip(const uint8* src_y, int src_stride_y, |
| 829 | const uint8* src_u, int src_stride_u, |
| 830 | const uint8* src_v, int src_stride_v, |
| 831 | uint32 src_fourcc, |
| 832 | int src_width, int src_height, |
| 833 | uint8* dst_argb, int dst_stride_argb, |
| 834 | uint32 dst_fourcc, |
| 835 | int dst_width, int dst_height, |
| 836 | int clip_x, int clip_y, int clip_width, int clip_height, |
| 837 | enum FilterMode filtering) { |
| 838 | uint8* argb_buffer = (uint8*)malloc(src_width * src_height * 4); |
| 839 | int r; |
| 840 | I420ToARGB(src_y, src_stride_y, |
| 841 | src_u, src_stride_u, |
| 842 | src_v, src_stride_v, |
| 843 | argb_buffer, src_width * 4, |
| 844 | src_width, src_height); |
| 845 |
| 846 | r = ARGBScaleClip(argb_buffer, src_width * 4, |
| 847 | src_width, src_height, |
| 848 | dst_argb, dst_stride_argb, |
| 849 | dst_width, dst_height, |
| 850 | clip_x, clip_y, clip_width, clip_height, |
| 851 | filtering); |
| 852 | free(argb_buffer); |
| 853 | return r; |
| 854 | } |
| 855 |
| 856 | #ifdef __cplusplus |
| 857 | } // extern "C" |
| 858 | } // namespace libyuv |
| 859 | #endif |
| 860 |