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