| 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/compare.h" |
| 12 |
| 13 | #include <float.h> |
| 14 | #include <math.h> |
| 15 | #ifdef _OPENMP |
| 16 | #include <omp.h> |
| 17 | #endif |
| 18 |
| 19 | #include "libyuv/basic_types.h" |
| 20 | #include "libyuv/compare_row.h" |
| 21 | #include "libyuv/cpu_id.h" |
| 22 | #include "libyuv/row.h" |
| 23 | #include "libyuv/video_common.h" |
| 24 |
| 25 | #ifdef __cplusplus |
| 26 | namespace libyuv { |
| 27 | extern "C" { |
| 28 | #endif |
| 29 |
| 30 | // hash seed of 5381 recommended. |
| 31 | LIBYUV_API |
| 32 | uint32_t HashDjb2(const uint8_t* src, uint64_t count, uint32_t seed) { |
| 33 | const int kBlockSize = 1 << 15; // 32768; |
| 34 | int remainder; |
| 35 | uint32_t (*HashDjb2_SSE)(const uint8_t* src, int count, uint32_t seed) = |
| 36 | HashDjb2_C; |
| 37 | #if defined(HAS_HASHDJB2_SSE41) |
| 38 | if (TestCpuFlag(kCpuHasSSE41)) { |
| 39 | HashDjb2_SSE = HashDjb2_SSE41; |
| 40 | } |
| 41 | #endif |
| 42 | #if defined(HAS_HASHDJB2_AVX2) |
| 43 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 44 | HashDjb2_SSE = HashDjb2_AVX2; |
| 45 | } |
| 46 | #endif |
| 47 |
| 48 | while (count >= (uint64_t)(kBlockSize)) { |
| 49 | seed = HashDjb2_SSE(src, kBlockSize, seed); |
| 50 | src += kBlockSize; |
| 51 | count -= kBlockSize; |
| 52 | } |
| 53 | remainder = (int)count & ~15; |
| 54 | if (remainder) { |
| 55 | seed = HashDjb2_SSE(src, remainder, seed); |
| 56 | src += remainder; |
| 57 | count -= remainder; |
| 58 | } |
| 59 | remainder = (int)count & 15; |
| 60 | if (remainder) { |
| 61 | seed = HashDjb2_C(src, remainder, seed); |
| 62 | } |
| 63 | return seed; |
| 64 | } |
| 65 |
| 66 | static uint32_t ARGBDetectRow_C(const uint8_t* argb, int width) { |
| 67 | int x; |
| 68 | for (x = 0; x < width - 1; x += 2) { |
| 69 | if (argb[0] != 255) { // First byte is not Alpha of 255, so not ARGB. |
| 70 | return FOURCC_BGRA; |
| 71 | } |
| 72 | if (argb[3] != 255) { // Fourth byte is not Alpha of 255, so not BGRA. |
| 73 | return FOURCC_ARGB; |
| 74 | } |
| 75 | if (argb[4] != 255) { // Second pixel first byte is not Alpha of 255. |
| 76 | return FOURCC_BGRA; |
| 77 | } |
| 78 | if (argb[7] != 255) { // Second pixel fourth byte is not Alpha of 255. |
| 79 | return FOURCC_ARGB; |
| 80 | } |
| 81 | argb += 8; |
| 82 | } |
| 83 | if (width & 1) { |
| 84 | if (argb[0] != 255) { // First byte is not Alpha of 255, so not ARGB. |
| 85 | return FOURCC_BGRA; |
| 86 | } |
| 87 | if (argb[3] != 255) { // 4th byte is not Alpha of 255, so not BGRA. |
| 88 | return FOURCC_ARGB; |
| 89 | } |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 |
| 94 | // Scan an opaque argb image and return fourcc based on alpha offset. |
| 95 | // Returns FOURCC_ARGB, FOURCC_BGRA, or 0 if unknown. |
| 96 | LIBYUV_API |
| 97 | uint32_t ARGBDetect(const uint8_t* argb, |
| 98 | int stride_argb, |
| 99 | int width, |
| 100 | int height) { |
| 101 | uint32_t fourcc = 0; |
| 102 | int h; |
| 103 |
| 104 | // Coalesce rows. |
| 105 | if (stride_argb == width * 4) { |
| 106 | width *= height; |
| 107 | height = 1; |
| 108 | stride_argb = 0; |
| 109 | } |
| 110 | for (h = 0; h < height && fourcc == 0; ++h) { |
| 111 | fourcc = ARGBDetectRow_C(argb, width); |
| 112 | argb += stride_argb; |
| 113 | } |
| 114 | return fourcc; |
| 115 | } |
| 116 |
| 117 | // NEON version accumulates in 16 bit shorts which overflow at 65536 bytes. |
| 118 | // So actual maximum is 1 less loop, which is 64436 - 32 bytes. |
| 119 |
| 120 | LIBYUV_API |
| 121 | uint64_t ComputeHammingDistance(const uint8_t* src_a, |
| 122 | const uint8_t* src_b, |
| 123 | int count) { |
| 124 | const int kBlockSize = 1 << 15; // 32768; |
| 125 | const int kSimdSize = 64; |
| 126 | // SIMD for multiple of 64, and C for remainder |
| 127 | int remainder = count & (kBlockSize - 1) & ~(kSimdSize - 1); |
| 128 | uint64_t diff = 0; |
| 129 | int i; |
| 130 | uint32_t (*HammingDistance)(const uint8_t* src_a, const uint8_t* src_b, |
| 131 | int count) = HammingDistance_C; |
| 132 | #if defined(HAS_HAMMINGDISTANCE_NEON) |
| 133 | if (TestCpuFlag(kCpuHasNEON)) { |
| 134 | HammingDistance = HammingDistance_NEON; |
| 135 | } |
| 136 | #endif |
| 137 | #if defined(HAS_HAMMINGDISTANCE_SSSE3) |
| 138 | if (TestCpuFlag(kCpuHasSSSE3)) { |
| 139 | HammingDistance = HammingDistance_SSSE3; |
| 140 | } |
| 141 | #endif |
| 142 | #if defined(HAS_HAMMINGDISTANCE_SSE42) |
| 143 | if (TestCpuFlag(kCpuHasSSE42)) { |
| 144 | HammingDistance = HammingDistance_SSE42; |
| 145 | } |
| 146 | #endif |
| 147 | #if defined(HAS_HAMMINGDISTANCE_AVX2) |
| 148 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 149 | HammingDistance = HammingDistance_AVX2; |
| 150 | } |
| 151 | #endif |
| 152 | #if defined(HAS_HAMMINGDISTANCE_MMI) |
| 153 | if (TestCpuFlag(kCpuHasMMI)) { |
| 154 | HammingDistance = HammingDistance_MMI; |
| 155 | } |
| 156 | #endif |
| 157 | #if defined(HAS_HAMMINGDISTANCE_MSA) |
| 158 | if (TestCpuFlag(kCpuHasMSA)) { |
| 159 | HammingDistance = HammingDistance_MSA; |
| 160 | } |
| 161 | #endif |
| 162 |
| 163 | #ifdef _OPENMP |
| 164 | #pragma omp parallel for reduction(+ : diff) |
| 165 | #endif |
| 166 | for (i = 0; i < (count - (kBlockSize - 1)); i += kBlockSize) { |
| 167 | diff += HammingDistance(src_a + i, src_b + i, kBlockSize); |
| 168 | } |
| 169 | src_a += count & ~(kBlockSize - 1); |
| 170 | src_b += count & ~(kBlockSize - 1); |
| 171 | if (remainder) { |
| 172 | diff += HammingDistance(src_a, src_b, remainder); |
| 173 | src_a += remainder; |
| 174 | src_b += remainder; |
| 175 | } |
| 176 | remainder = count & (kSimdSize - 1); |
| 177 | if (remainder) { |
| 178 | diff += HammingDistance_C(src_a, src_b, remainder); |
| 179 | } |
| 180 | return diff; |
| 181 | } |
| 182 |
| 183 | // TODO(fbarchard): Refactor into row function. |
| 184 | LIBYUV_API |
| 185 | uint64_t ComputeSumSquareError(const uint8_t* src_a, |
| 186 | const uint8_t* src_b, |
| 187 | int count) { |
| 188 | // SumSquareError returns values 0 to 65535 for each squared difference. |
| 189 | // Up to 65536 of those can be summed and remain within a uint32_t. |
| 190 | // After each block of 65536 pixels, accumulate into a uint64_t. |
| 191 | const int kBlockSize = 65536; |
| 192 | int remainder = count & (kBlockSize - 1) & ~31; |
| 193 | uint64_t sse = 0; |
| 194 | int i; |
| 195 | uint32_t (*SumSquareError)(const uint8_t* src_a, const uint8_t* src_b, |
| 196 | int count) = SumSquareError_C; |
| 197 | #if defined(HAS_SUMSQUAREERROR_NEON) |
| 198 | if (TestCpuFlag(kCpuHasNEON)) { |
| 199 | SumSquareError = SumSquareError_NEON; |
| 200 | } |
| 201 | #endif |
| 202 | #if defined(HAS_SUMSQUAREERROR_SSE2) |
| 203 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 204 | // Note only used for multiples of 16 so count is not checked. |
| 205 | SumSquareError = SumSquareError_SSE2; |
| 206 | } |
| 207 | #endif |
| 208 | #if defined(HAS_SUMSQUAREERROR_AVX2) |
| 209 | if (TestCpuFlag(kCpuHasAVX2)) { |
| 210 | // Note only used for multiples of 32 so count is not checked. |
| 211 | SumSquareError = SumSquareError_AVX2; |
| 212 | } |
| 213 | #endif |
| 214 | #if defined(HAS_SUMSQUAREERROR_MMI) |
| 215 | if (TestCpuFlag(kCpuHasMMI)) { |
| 216 | SumSquareError = SumSquareError_MMI; |
| 217 | } |
| 218 | #endif |
| 219 | #if defined(HAS_SUMSQUAREERROR_MSA) |
| 220 | if (TestCpuFlag(kCpuHasMSA)) { |
| 221 | SumSquareError = SumSquareError_MSA; |
| 222 | } |
| 223 | #endif |
| 224 | #ifdef _OPENMP |
| 225 | #pragma omp parallel for reduction(+ : sse) |
| 226 | #endif |
| 227 | for (i = 0; i < (count - (kBlockSize - 1)); i += kBlockSize) { |
| 228 | sse += SumSquareError(src_a + i, src_b + i, kBlockSize); |
| 229 | } |
| 230 | src_a += count & ~(kBlockSize - 1); |
| 231 | src_b += count & ~(kBlockSize - 1); |
| 232 | if (remainder) { |
| 233 | sse += SumSquareError(src_a, src_b, remainder); |
| 234 | src_a += remainder; |
| 235 | src_b += remainder; |
| 236 | } |
| 237 | remainder = count & 31; |
| 238 | if (remainder) { |
| 239 | sse += SumSquareError_C(src_a, src_b, remainder); |
| 240 | } |
| 241 | return sse; |
| 242 | } |
| 243 |
| 244 | LIBYUV_API |
| 245 | uint64_t ComputeSumSquareErrorPlane(const uint8_t* src_a, |
| 246 | int stride_a, |
| 247 | const uint8_t* src_b, |
| 248 | int stride_b, |
| 249 | int width, |
| 250 | int height) { |
| 251 | uint64_t sse = 0; |
| 252 | int h; |
| 253 | // Coalesce rows. |
| 254 | if (stride_a == width && stride_b == width) { |
| 255 | width *= height; |
| 256 | height = 1; |
| 257 | stride_a = stride_b = 0; |
| 258 | } |
| 259 | for (h = 0; h < height; ++h) { |
| 260 | sse += ComputeSumSquareError(src_a, src_b, width); |
| 261 | src_a += stride_a; |
| 262 | src_b += stride_b; |
| 263 | } |
| 264 | return sse; |
| 265 | } |
| 266 |
| 267 | LIBYUV_API |
| 268 | double SumSquareErrorToPsnr(uint64_t sse, uint64_t count) { |
| 269 | double psnr; |
| 270 | if (sse > 0) { |
| 271 | double mse = (double)count / (double)sse; |
| 272 | psnr = 10.0 * log10(255.0 * 255.0 * mse); |
| 273 | } else { |
| 274 | psnr = kMaxPsnr; // Limit to prevent divide by 0 |
| 275 | } |
| 276 |
| 277 | if (psnr > kMaxPsnr) { |
| 278 | psnr = kMaxPsnr; |
| 279 | } |
| 280 |
| 281 | return psnr; |
| 282 | } |
| 283 |
| 284 | LIBYUV_API |
| 285 | double CalcFramePsnr(const uint8_t* src_a, |
| 286 | int stride_a, |
| 287 | const uint8_t* src_b, |
| 288 | int stride_b, |
| 289 | int width, |
| 290 | int height) { |
| 291 | const uint64_t samples = (uint64_t)width * (uint64_t)height; |
| 292 | const uint64_t sse = ComputeSumSquareErrorPlane(src_a, stride_a, src_b, |
| 293 | stride_b, width, height); |
| 294 | return SumSquareErrorToPsnr(sse, samples); |
| 295 | } |
| 296 |
| 297 | LIBYUV_API |
| 298 | double I420Psnr(const uint8_t* src_y_a, |
| 299 | int stride_y_a, |
| 300 | const uint8_t* src_u_a, |
| 301 | int stride_u_a, |
| 302 | const uint8_t* src_v_a, |
| 303 | int stride_v_a, |
| 304 | const uint8_t* src_y_b, |
| 305 | int stride_y_b, |
| 306 | const uint8_t* src_u_b, |
| 307 | int stride_u_b, |
| 308 | const uint8_t* src_v_b, |
| 309 | int stride_v_b, |
| 310 | int width, |
| 311 | int height) { |
| 312 | const uint64_t sse_y = ComputeSumSquareErrorPlane( |
| 313 | src_y_a, stride_y_a, src_y_b, stride_y_b, width, height); |
| 314 | const int width_uv = (width + 1) >> 1; |
| 315 | const int height_uv = (height + 1) >> 1; |
| 316 | const uint64_t sse_u = ComputeSumSquareErrorPlane( |
| 317 | src_u_a, stride_u_a, src_u_b, stride_u_b, width_uv, height_uv); |
| 318 | const uint64_t sse_v = ComputeSumSquareErrorPlane( |
| 319 | src_v_a, stride_v_a, src_v_b, stride_v_b, width_uv, height_uv); |
| 320 | const uint64_t samples = (uint64_t)width * (uint64_t)height + |
| 321 | 2 * ((uint64_t)width_uv * (uint64_t)height_uv); |
| 322 | const uint64_t sse = sse_y + sse_u + sse_v; |
| 323 | return SumSquareErrorToPsnr(sse, samples); |
| 324 | } |
| 325 |
| 326 | static const int64_t cc1 = 26634; // (64^2*(.01*255)^2 |
| 327 | static const int64_t cc2 = 239708; // (64^2*(.03*255)^2 |
| 328 |
| 329 | static double Ssim8x8_C(const uint8_t* src_a, |
| 330 | int stride_a, |
| 331 | const uint8_t* src_b, |
| 332 | int stride_b) { |
| 333 | int64_t sum_a = 0; |
| 334 | int64_t sum_b = 0; |
| 335 | int64_t sum_sq_a = 0; |
| 336 | int64_t sum_sq_b = 0; |
| 337 | int64_t sum_axb = 0; |
| 338 |
| 339 | int i; |
| 340 | for (i = 0; i < 8; ++i) { |
| 341 | int j; |
| 342 | for (j = 0; j < 8; ++j) { |
| 343 | sum_a += src_a[j]; |
| 344 | sum_b += src_b[j]; |
| 345 | sum_sq_a += src_a[j] * src_a[j]; |
| 346 | sum_sq_b += src_b[j] * src_b[j]; |
| 347 | sum_axb += src_a[j] * src_b[j]; |
| 348 | } |
| 349 |
| 350 | src_a += stride_a; |
| 351 | src_b += stride_b; |
| 352 | } |
| 353 |
| 354 | { |
| 355 | const int64_t count = 64; |
| 356 | // scale the constants by number of pixels |
| 357 | const int64_t c1 = (cc1 * count * count) >> 12; |
| 358 | const int64_t c2 = (cc2 * count * count) >> 12; |
| 359 |
| 360 | const int64_t sum_a_x_sum_b = sum_a * sum_b; |
| 361 |
| 362 | const int64_t ssim_n = (2 * sum_a_x_sum_b + c1) * |
| 363 | (2 * count * sum_axb - 2 * sum_a_x_sum_b + c2); |
| 364 |
| 365 | const int64_t sum_a_sq = sum_a * sum_a; |
| 366 | const int64_t sum_b_sq = sum_b * sum_b; |
| 367 |
| 368 | const int64_t ssim_d = |
| 369 | (sum_a_sq + sum_b_sq + c1) * |
| 370 | (count * sum_sq_a - sum_a_sq + count * sum_sq_b - sum_b_sq + c2); |
| 371 |
| 372 | if (ssim_d == 0.0) { |
| 373 | return DBL_MAX; |
| 374 | } |
| 375 | return ssim_n * 1.0 / ssim_d; |
| 376 | } |
| 377 | } |
| 378 |
| 379 | // We are using a 8x8 moving window with starting location of each 8x8 window |
| 380 | // on the 4x4 pixel grid. Such arrangement allows the windows to overlap |
| 381 | // block boundaries to penalize blocking artifacts. |
| 382 | LIBYUV_API |
| 383 | double CalcFrameSsim(const uint8_t* src_a, |
| 384 | int stride_a, |
| 385 | const uint8_t* src_b, |
| 386 | int stride_b, |
| 387 | int width, |
| 388 | int height) { |
| 389 | int samples = 0; |
| 390 | double ssim_total = 0; |
| 391 | double (*Ssim8x8)(const uint8_t* src_a, int stride_a, const uint8_t* src_b, |
| 392 | int stride_b) = Ssim8x8_C; |
| 393 |
| 394 | // sample point start with each 4x4 location |
| 395 | int i; |
| 396 | for (i = 0; i < height - 8; i += 4) { |
| 397 | int j; |
| 398 | for (j = 0; j < width - 8; j += 4) { |
| 399 | ssim_total += Ssim8x8(src_a + j, stride_a, src_b + j, stride_b); |
| 400 | samples++; |
| 401 | } |
| 402 |
| 403 | src_a += stride_a * 4; |
| 404 | src_b += stride_b * 4; |
| 405 | } |
| 406 |
| 407 | ssim_total /= samples; |
| 408 | return ssim_total; |
| 409 | } |
| 410 |
| 411 | LIBYUV_API |
| 412 | double I420Ssim(const uint8_t* src_y_a, |
| 413 | int stride_y_a, |
| 414 | const uint8_t* src_u_a, |
| 415 | int stride_u_a, |
| 416 | const uint8_t* src_v_a, |
| 417 | int stride_v_a, |
| 418 | const uint8_t* src_y_b, |
| 419 | int stride_y_b, |
| 420 | const uint8_t* src_u_b, |
| 421 | int stride_u_b, |
| 422 | const uint8_t* src_v_b, |
| 423 | int stride_v_b, |
| 424 | int width, |
| 425 | int height) { |
| 426 | const double ssim_y = |
| 427 | CalcFrameSsim(src_y_a, stride_y_a, src_y_b, stride_y_b, width, height); |
| 428 | const int width_uv = (width + 1) >> 1; |
| 429 | const int height_uv = (height + 1) >> 1; |
| 430 | const double ssim_u = CalcFrameSsim(src_u_a, stride_u_a, src_u_b, stride_u_b, |
| 431 | width_uv, height_uv); |
| 432 | const double ssim_v = CalcFrameSsim(src_v_a, stride_v_a, src_v_b, stride_v_b, |
| 433 | width_uv, height_uv); |
| 434 | return ssim_y * 0.8 + 0.1 * (ssim_u + ssim_v); |
| 435 | } |
| 436 |
| 437 | #ifdef __cplusplus |
| 438 | } // extern "C" |
| 439 | } // namespace libyuv |
| 440 | #endif |
| 441 |