blob: 1aa3a7eef4538b10a981188950c9832375819b42 [file] [log] [blame]
Urvang Joshi09c293e2017-04-20 17:56:27 -07001/*
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 Joshibad61922017-04-21 18:30:13 -070014#include <assert.h>
Urvang Joshi09c293e2017-04-20 17:56:27 -070015#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 Joshibad61922017-04-21 18:30:13 -070022static void find_mismatch_plane(const aom_image_t *const img1,
23 const aom_image_t *const img2, int plane,
Urvang Joshic798b142017-04-24 13:07:55 -070024 int use_highbitdepth, int loc[4]) {
Urvang Joshibad61922017-04-21 18:30:13 -070025 const unsigned char *const p1 = img1->planes[plane];
Urvang Joshic798b142017-04-24 13:07:55 -070026 const int p1_stride = img1->stride[plane] >> use_highbitdepth;
Urvang Joshibad61922017-04-21 18:30:13 -070027 const unsigned char *const p2 = img2->planes[plane];
Urvang Joshic798b142017-04-24 13:07:55 -070028 const int p2_stride = img2->stride[plane] >> use_highbitdepth;
Urvang Joshibad61922017-04-21 18:30:13 -070029 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 Joshic798b142017-04-24 13:07:55 -070058 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 Joshibad61922017-04-21 18:30:13 -070064 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 Joshic798b142017-04-24 13:07:55 -070078static 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
91void 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 Joshi09c293e2017-04-20 17:56:27 -070098void 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 Joshic798b142017-04-24 13:07:55 -0700101 find_mismatch_helper(img1, img2, 0, yloc, uloc, vloc);
Urvang Joshi09c293e2017-04-20 17:56:27 -0700102}
103
104int 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}