Urvang Joshi | 09c293e | 2017-04-20 17:56:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Alliance for Open Media. All rights reserved |
| 3 | * |
| 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 10 | */ |
| 11 | |
| 12 | // Utility functions used by encoder binaries. |
| 13 | |
Urvang Joshi | bad6192 | 2017-04-21 18:30:13 -0700 | [diff] [blame] | 14 | #include <assert.h> |
Urvang Joshi | 09c293e | 2017-04-20 17:56:27 -0700 | [diff] [blame] | 15 | #include <string.h> |
| 16 | |
| 17 | #include "./encoder_util.h" |
| 18 | #include "aom/aom_integer.h" |
| 19 | |
| 20 | #define mmin(a, b) ((a) < (b) ? (a) : (b)) |
| 21 | |
Urvang Joshi | bad6192 | 2017-04-21 18:30:13 -0700 | [diff] [blame] | 22 | static void find_mismatch_plane(const aom_image_t *const img1, |
| 23 | const aom_image_t *const img2, int plane, |
Urvang Joshi | c798b14 | 2017-04-24 13:07:55 -0700 | [diff] [blame] | 24 | int use_highbitdepth, int loc[4]) { |
Urvang Joshi | bad6192 | 2017-04-21 18:30:13 -0700 | [diff] [blame] | 25 | const unsigned char *const p1 = img1->planes[plane]; |
Urvang Joshi | c798b14 | 2017-04-24 13:07:55 -0700 | [diff] [blame] | 26 | const int p1_stride = img1->stride[plane] >> use_highbitdepth; |
Urvang Joshi | bad6192 | 2017-04-21 18:30:13 -0700 | [diff] [blame] | 27 | const unsigned char *const p2 = img2->planes[plane]; |
Urvang Joshi | c798b14 | 2017-04-24 13:07:55 -0700 | [diff] [blame] | 28 | const int p2_stride = img2->stride[plane] >> use_highbitdepth; |
Urvang Joshi | bad6192 | 2017-04-21 18:30:13 -0700 | [diff] [blame] | 29 | const uint32_t bsize = 64; |
| 30 | const int is_y_plane = (plane == AOM_PLANE_Y); |
| 31 | const uint32_t bsizex = is_y_plane ? bsize : bsize >> img1->x_chroma_shift; |
| 32 | const uint32_t bsizey = is_y_plane ? bsize : bsize >> img1->y_chroma_shift; |
| 33 | const uint32_t c_w = |
| 34 | is_y_plane ? img1->d_w |
| 35 | : (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift; |
| 36 | const uint32_t c_h = |
| 37 | is_y_plane ? img1->d_h |
| 38 | : (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift; |
| 39 | assert(img1->d_w == img2->d_w && img1->d_h == img2->d_h); |
| 40 | assert(img1->x_chroma_shift == img2->x_chroma_shift && |
| 41 | img1->y_chroma_shift == img2->y_chroma_shift); |
| 42 | loc[0] = loc[1] = loc[2] = loc[3] = -1; |
| 43 | int match = 1; |
| 44 | uint32_t i, j; |
| 45 | for (i = 0; match && i < c_h; i += bsizey) { |
| 46 | for (j = 0; match && j < c_w; j += bsizex) { |
| 47 | const int si = |
| 48 | is_y_plane ? mmin(i + bsizey, c_h) - i : mmin(i + bsizey, c_h - i); |
| 49 | const int sj = |
| 50 | is_y_plane ? mmin(j + bsizex, c_w) - j : mmin(j + bsizex, c_w - j); |
| 51 | int k, l; |
| 52 | for (k = 0; match && k < si; ++k) { |
| 53 | for (l = 0; match && l < sj; ++l) { |
| 54 | const int row = i + k; |
| 55 | const int col = j + l; |
| 56 | const int offset1 = row * p1_stride + col; |
| 57 | const int offset2 = row * p2_stride + col; |
Urvang Joshi | c798b14 | 2017-04-24 13:07:55 -0700 | [diff] [blame] | 58 | const int val1 = use_highbitdepth |
| 59 | ? p1[2 * offset1] | (p1[2 * offset1 + 1] << 8) |
| 60 | : p1[offset1]; |
| 61 | const int val2 = use_highbitdepth |
| 62 | ? p2[2 * offset2] | (p2[2 * offset2 + 1] << 8) |
| 63 | : p2[offset2]; |
Urvang Joshi | bad6192 | 2017-04-21 18:30:13 -0700 | [diff] [blame] | 64 | if (val1 != val2) { |
| 65 | loc[0] = row; |
| 66 | loc[1] = col; |
| 67 | loc[2] = val1; |
| 68 | loc[3] = val2; |
| 69 | match = 0; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Urvang Joshi | c798b14 | 2017-04-24 13:07:55 -0700 | [diff] [blame] | 78 | static void find_mismatch_helper(const aom_image_t *const img1, |
| 79 | const aom_image_t *const img2, |
| 80 | int use_highbitdepth, int yloc[4], int uloc[4], |
| 81 | int vloc[4]) { |
| 82 | #if !CONFIG_HIGHBITDEPTH |
| 83 | assert(!use_highbitdepth); |
| 84 | #endif // !CONFIG_HIGHBITDEPTH |
| 85 | find_mismatch_plane(img1, img2, AOM_PLANE_Y, use_highbitdepth, yloc); |
| 86 | find_mismatch_plane(img1, img2, AOM_PLANE_U, use_highbitdepth, uloc); |
| 87 | find_mismatch_plane(img1, img2, AOM_PLANE_V, use_highbitdepth, vloc); |
| 88 | } |
| 89 | |
| 90 | #if CONFIG_HIGHBITDEPTH |
| 91 | void aom_find_mismatch_high(const aom_image_t *const img1, |
| 92 | const aom_image_t *const img2, int yloc[4], |
| 93 | int uloc[4], int vloc[4]) { |
| 94 | find_mismatch_helper(img1, img2, 1, yloc, uloc, vloc); |
| 95 | } |
| 96 | #endif |
| 97 | |
Urvang Joshi | 09c293e | 2017-04-20 17:56:27 -0700 | [diff] [blame] | 98 | void aom_find_mismatch(const aom_image_t *const img1, |
| 99 | const aom_image_t *const img2, int yloc[4], int uloc[4], |
| 100 | int vloc[4]) { |
Urvang Joshi | c798b14 | 2017-04-24 13:07:55 -0700 | [diff] [blame] | 101 | find_mismatch_helper(img1, img2, 0, yloc, uloc, vloc); |
Urvang Joshi | 09c293e | 2017-04-20 17:56:27 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int aom_compare_img(const aom_image_t *const img1, |
| 105 | const aom_image_t *const img2) { |
| 106 | uint32_t l_w = img1->d_w; |
| 107 | uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift; |
| 108 | const uint32_t c_h = |
| 109 | (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift; |
| 110 | uint32_t i; |
| 111 | int match = 1; |
| 112 | |
| 113 | match &= (img1->fmt == img2->fmt); |
| 114 | match &= (img1->d_w == img2->d_w); |
| 115 | match &= (img1->d_h == img2->d_h); |
| 116 | #if CONFIG_HIGHBITDEPTH |
| 117 | if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) { |
| 118 | l_w *= 2; |
| 119 | c_w *= 2; |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | for (i = 0; i < img1->d_h; ++i) |
| 124 | match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y], |
| 125 | img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y], |
| 126 | l_w) == 0); |
| 127 | |
| 128 | for (i = 0; i < c_h; ++i) |
| 129 | match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U], |
| 130 | img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U], |
| 131 | c_w) == 0); |
| 132 | |
| 133 | for (i = 0; i < c_h; ++i) |
| 134 | match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V], |
| 135 | img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V], |
| 136 | c_w) == 0); |
| 137 | |
| 138 | return match; |
| 139 | } |