Move image matching functions to common location.

Exact same functions were present in two encoder binaries.

BUG=aomedia:442

Change-Id: Ib169c29cee8ae40cbd71c26099a3339ec7143315
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 98b81eb..21a29b6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -125,7 +125,9 @@
     "${AOM_ROOT}/warnings.c"
     "${AOM_ROOT}/warnings.h"
     "${AOM_ROOT}/y4minput.c"
-    "${AOM_ROOT}/y4minput.h")
+    "${AOM_ROOT}/y4minput.h"
+    "${AOM_ROOT}/examples/encoder_util.h"
+    "${AOM_ROOT}/examples/encoder_util.c")
 
 set(AOM_ENCODER_STATS_SOURCES
     "${AOM_ROOT}/aomstats.c"
diff --git a/aomenc.c b/aomenc.c
index 62aae01..2d0836e 100644
--- a/aomenc.c
+++ b/aomenc.c
@@ -32,6 +32,7 @@
 #include "./args.h"
 #include "./ivfenc.h"
 #include "./tools_common.h"
+#include "examples/encoder_util.h"
 
 #if CONFIG_AV1_ENCODER
 #include "aom/aomcx.h"
@@ -615,230 +616,6 @@
   exit(EXIT_FAILURE);
 }
 
-#define mmin(a, b) ((a) < (b) ? (a) : (b))
-
-#if CONFIG_HIGHBITDEPTH
-static void find_mismatch_high(const aom_image_t *const img1,
-                               const aom_image_t *const img2, int yloc[4],
-                               int uloc[4], int vloc[4]) {
-  uint16_t *plane1, *plane2;
-  uint32_t stride1, stride2;
-  const uint32_t bsize = 64;
-  const uint32_t bsizey = bsize >> img1->y_chroma_shift;
-  const uint32_t bsizex = bsize >> img1->x_chroma_shift;
-  const uint32_t c_w =
-      (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
-  const uint32_t c_h =
-      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
-  int match = 1;
-  uint32_t i, j;
-  yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
-  plane1 = (uint16_t *)img1->planes[AOM_PLANE_Y];
-  plane2 = (uint16_t *)img2->planes[AOM_PLANE_Y];
-  stride1 = img1->stride[AOM_PLANE_Y] / 2;
-  stride2 = img2->stride[AOM_PLANE_Y] / 2;
-  for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
-    for (j = 0; match && j < img1->d_w; j += bsize) {
-      int k, l;
-      const int si = mmin(i + bsize, img1->d_h) - i;
-      const int sj = mmin(j + bsize, img1->d_w) - j;
-      for (k = 0; match && k < si; ++k) {
-        for (l = 0; match && l < sj; ++l) {
-          if (*(plane1 + (i + k) * stride1 + j + l) !=
-              *(plane2 + (i + k) * stride2 + j + l)) {
-            yloc[0] = i + k;
-            yloc[1] = j + l;
-            yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
-            yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
-            match = 0;
-            break;
-          }
-        }
-      }
-    }
-  }
-
-  uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
-  plane1 = (uint16_t *)img1->planes[AOM_PLANE_U];
-  plane2 = (uint16_t *)img2->planes[AOM_PLANE_U];
-  stride1 = img1->stride[AOM_PLANE_U] / 2;
-  stride2 = img2->stride[AOM_PLANE_U] / 2;
-  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
-    for (j = 0; match && j < c_w; j += bsizex) {
-      int k, l;
-      const int si = mmin(i + bsizey, c_h - i);
-      const int sj = mmin(j + bsizex, c_w - j);
-      for (k = 0; match && k < si; ++k) {
-        for (l = 0; match && l < sj; ++l) {
-          if (*(plane1 + (i + k) * stride1 + j + l) !=
-              *(plane2 + (i + k) * stride2 + j + l)) {
-            uloc[0] = i + k;
-            uloc[1] = j + l;
-            uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
-            uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
-            match = 0;
-            break;
-          }
-        }
-      }
-    }
-  }
-
-  vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
-  plane1 = (uint16_t *)img1->planes[AOM_PLANE_V];
-  plane2 = (uint16_t *)img2->planes[AOM_PLANE_V];
-  stride1 = img1->stride[AOM_PLANE_V] / 2;
-  stride2 = img2->stride[AOM_PLANE_V] / 2;
-  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
-    for (j = 0; match && j < c_w; j += bsizex) {
-      int k, l;
-      const int si = mmin(i + bsizey, c_h - i);
-      const int sj = mmin(j + bsizex, c_w - j);
-      for (k = 0; match && k < si; ++k) {
-        for (l = 0; match && l < sj; ++l) {
-          if (*(plane1 + (i + k) * stride1 + j + l) !=
-              *(plane2 + (i + k) * stride2 + j + l)) {
-            vloc[0] = i + k;
-            vloc[1] = j + l;
-            vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
-            vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
-            match = 0;
-            break;
-          }
-        }
-      }
-    }
-  }
-}
-#endif
-
-static void find_mismatch(const aom_image_t *const img1,
-                          const aom_image_t *const img2, int yloc[4],
-                          int uloc[4], int vloc[4]) {
-  const uint32_t bsize = 64;
-  const uint32_t bsizey = bsize >> img1->y_chroma_shift;
-  const uint32_t bsizex = bsize >> img1->x_chroma_shift;
-  const uint32_t c_w =
-      (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
-  const uint32_t c_h =
-      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
-  int match = 1;
-  uint32_t i, j;
-  yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
-  for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
-    for (j = 0; match && j < img1->d_w; j += bsize) {
-      int k, l;
-      const int si = mmin(i + bsize, img1->d_h) - i;
-      const int sj = mmin(j + bsize, img1->d_w) - j;
-      for (k = 0; match && k < si; ++k) {
-        for (l = 0; match && l < sj; ++l) {
-          if (*(img1->planes[AOM_PLANE_Y] +
-                (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
-              *(img2->planes[AOM_PLANE_Y] +
-                (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
-            yloc[0] = i + k;
-            yloc[1] = j + l;
-            yloc[2] = *(img1->planes[AOM_PLANE_Y] +
-                        (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
-            yloc[3] = *(img2->planes[AOM_PLANE_Y] +
-                        (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
-            match = 0;
-            break;
-          }
-        }
-      }
-    }
-  }
-
-  uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
-  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
-    for (j = 0; match && j < c_w; j += bsizex) {
-      int k, l;
-      const int si = mmin(i + bsizey, c_h - i);
-      const int sj = mmin(j + bsizex, c_w - j);
-      for (k = 0; match && k < si; ++k) {
-        for (l = 0; match && l < sj; ++l) {
-          if (*(img1->planes[AOM_PLANE_U] +
-                (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
-              *(img2->planes[AOM_PLANE_U] +
-                (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
-            uloc[0] = i + k;
-            uloc[1] = j + l;
-            uloc[2] = *(img1->planes[AOM_PLANE_U] +
-                        (i + k) * img1->stride[AOM_PLANE_U] + j + l);
-            uloc[3] = *(img2->planes[AOM_PLANE_U] +
-                        (i + k) * img2->stride[AOM_PLANE_U] + j + l);
-            match = 0;
-            break;
-          }
-        }
-      }
-    }
-  }
-  vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
-  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
-    for (j = 0; match && j < c_w; j += bsizex) {
-      int k, l;
-      const int si = mmin(i + bsizey, c_h - i);
-      const int sj = mmin(j + bsizex, c_w - j);
-      for (k = 0; match && k < si; ++k) {
-        for (l = 0; match && l < sj; ++l) {
-          if (*(img1->planes[AOM_PLANE_V] +
-                (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
-              *(img2->planes[AOM_PLANE_V] +
-                (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
-            vloc[0] = i + k;
-            vloc[1] = j + l;
-            vloc[2] = *(img1->planes[AOM_PLANE_V] +
-                        (i + k) * img1->stride[AOM_PLANE_V] + j + l);
-            vloc[3] = *(img2->planes[AOM_PLANE_V] +
-                        (i + k) * img2->stride[AOM_PLANE_V] + j + l);
-            match = 0;
-            break;
-          }
-        }
-      }
-    }
-  }
-}
-
-static int compare_img(const aom_image_t *const img1,
-                       const aom_image_t *const img2) {
-  uint32_t l_w = img1->d_w;
-  uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
-  const uint32_t c_h =
-      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
-  uint32_t i;
-  int match = 1;
-
-  match &= (img1->fmt == img2->fmt);
-  match &= (img1->d_w == img2->d_w);
-  match &= (img1->d_h == img2->d_h);
-#if CONFIG_HIGHBITDEPTH
-  if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
-    l_w *= 2;
-    c_w *= 2;
-  }
-#endif
-
-  for (i = 0; i < img1->d_h; ++i)
-    match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
-                     img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
-                     l_w) == 0);
-
-  for (i = 0; i < c_h; ++i)
-    match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
-                     img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
-                     c_w) == 0);
-
-  for (i = 0; i < c_h; ++i)
-    match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
-                     img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
-                     c_w) == 0);
-
-  return match;
-}
-
 #define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
 #if CONFIG_AV1_ENCODER
 #define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
@@ -1839,16 +1616,16 @@
   ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
   ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
 
-  if (!compare_img(&enc_img, &dec_img)) {
+  if (!aom_compare_img(&enc_img, &dec_img)) {
     int y[4], u[4], v[4];
 #if CONFIG_HIGHBITDEPTH
     if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
-      find_mismatch_high(&enc_img, &dec_img, y, u, v);
+      aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
     } else {
-      find_mismatch(&enc_img, &dec_img, y, u, v);
+      aom_find_mismatch(&enc_img, &dec_img, y, u, v);
     }
 #else
-    find_mismatch(&enc_img, &dec_img, y, u, v);
+    aom_find_mismatch(&enc_img, &dec_img, y, u, v);
 #endif
     stream->decoder.err = 1;
     warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
diff --git a/examples.mk b/examples.mk
index dfc13f4..9520692 100644
--- a/examples.mk
+++ b/examples.mk
@@ -92,6 +92,7 @@
 aomenc.SRCS                 += ivfenc.c ivfenc.h
 aomenc.SRCS                 += rate_hist.c rate_hist.h
 aomenc.SRCS                 += tools_common.c tools_common.h
+aomenc.SRCS                 += examples/encoder_util.h examples/encoder_util.c
 aomenc.SRCS                 += warnings.c warnings.h
 aomenc.SRCS                 += aom_ports/mem_ops.h
 aomenc.SRCS                 += aom_ports/mem_ops_aligned.h
@@ -203,6 +204,8 @@
 EXAMPLES-$(CONFIG_ENCODERS)        += aom_cx_set_ref.c
 aom_cx_set_ref.SRCS                += ivfenc.h ivfenc.c
 aom_cx_set_ref.SRCS                += tools_common.h tools_common.c
+aom_cx_set_ref.SRCS                += examples/encoder_util.h
+aom_cx_set_ref.SRCS                += examples/encoder_util.c
 aom_cx_set_ref.SRCS                += video_common.h
 aom_cx_set_ref.SRCS                += video_writer.h video_writer.c
 aom_cx_set_ref.SRCS                += aom_ports/msvc.h
diff --git a/examples/aom_cx_set_ref.c b/examples/aom_cx_set_ref.c
index d1aec99..ff24fa1 100644
--- a/examples/aom_cx_set_ref.c
+++ b/examples/aom_cx_set_ref.c
@@ -54,7 +54,7 @@
 #include "aom/aomcx.h"
 #include "aom/aom_decoder.h"
 #include "aom/aom_encoder.h"
-
+#include "examples/encoder_util.h"
 #include "./tools_common.h"
 #include "./video_writer.h"
 
@@ -68,128 +68,6 @@
   exit(EXIT_FAILURE);
 }
 
-static int compare_img(const aom_image_t *const img1,
-                       const aom_image_t *const img2) {
-  uint32_t l_w = img1->d_w;
-  uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
-  const uint32_t c_h =
-      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
-  uint32_t i;
-  int match = 1;
-
-  match &= (img1->fmt == img2->fmt);
-  match &= (img1->d_w == img2->d_w);
-  match &= (img1->d_h == img2->d_h);
-
-  for (i = 0; i < img1->d_h; ++i)
-    match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
-                     img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
-                     l_w) == 0);
-
-  for (i = 0; i < c_h; ++i)
-    match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
-                     img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
-                     c_w) == 0);
-
-  for (i = 0; i < c_h; ++i)
-    match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
-                     img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
-                     c_w) == 0);
-
-  return match;
-}
-
-#define mmin(a, b) ((a) < (b) ? (a) : (b))
-static void find_mismatch(const aom_image_t *const img1,
-                          const aom_image_t *const img2, int yloc[4],
-                          int uloc[4], int vloc[4]) {
-  const uint32_t bsize = 64;
-  const uint32_t bsizey = bsize >> img1->y_chroma_shift;
-  const uint32_t bsizex = bsize >> img1->x_chroma_shift;
-  const uint32_t c_w =
-      (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
-  const uint32_t c_h =
-      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
-  int match = 1;
-  uint32_t i, j;
-  yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
-  for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
-    for (j = 0; match && j < img1->d_w; j += bsize) {
-      int k, l;
-      const int si = mmin(i + bsize, img1->d_h) - i;
-      const int sj = mmin(j + bsize, img1->d_w) - j;
-      for (k = 0; match && k < si; ++k) {
-        for (l = 0; match && l < sj; ++l) {
-          if (*(img1->planes[AOM_PLANE_Y] +
-                (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
-              *(img2->planes[AOM_PLANE_Y] +
-                (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
-            yloc[0] = i + k;
-            yloc[1] = j + l;
-            yloc[2] = *(img1->planes[AOM_PLANE_Y] +
-                        (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
-            yloc[3] = *(img2->planes[AOM_PLANE_Y] +
-                        (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
-            match = 0;
-            break;
-          }
-        }
-      }
-    }
-  }
-
-  uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
-  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
-    for (j = 0; match && j < c_w; j += bsizex) {
-      int k, l;
-      const int si = mmin(i + bsizey, c_h - i);
-      const int sj = mmin(j + bsizex, c_w - j);
-      for (k = 0; match && k < si; ++k) {
-        for (l = 0; match && l < sj; ++l) {
-          if (*(img1->planes[AOM_PLANE_U] +
-                (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
-              *(img2->planes[AOM_PLANE_U] +
-                (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
-            uloc[0] = i + k;
-            uloc[1] = j + l;
-            uloc[2] = *(img1->planes[AOM_PLANE_U] +
-                        (i + k) * img1->stride[AOM_PLANE_U] + j + l);
-            uloc[3] = *(img2->planes[AOM_PLANE_U] +
-                        (i + k) * img2->stride[AOM_PLANE_U] + j + l);
-            match = 0;
-            break;
-          }
-        }
-      }
-    }
-  }
-  vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
-  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
-    for (j = 0; match && j < c_w; j += bsizex) {
-      int k, l;
-      const int si = mmin(i + bsizey, c_h - i);
-      const int sj = mmin(j + bsizex, c_w - j);
-      for (k = 0; match && k < si; ++k) {
-        for (l = 0; match && l < sj; ++l) {
-          if (*(img1->planes[AOM_PLANE_V] +
-                (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
-              *(img2->planes[AOM_PLANE_V] +
-                (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
-            vloc[0] = i + k;
-            vloc[1] = j + l;
-            vloc[2] = *(img1->planes[AOM_PLANE_V] +
-                        (i + k) * img1->stride[AOM_PLANE_V] + j + l);
-            vloc[3] = *(img2->planes[AOM_PLANE_V] +
-                        (i + k) * img2->stride[AOM_PLANE_V] + j + l);
-            match = 0;
-            break;
-          }
-        }
-      }
-    }
-  }
-}
-
 static void testing_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
                            unsigned int frame_out, int *mismatch_seen) {
   aom_image_t enc_img, dec_img;
@@ -206,12 +84,12 @@
     die_codec(decoder, "Failed to get decoder reference frame");
   dec_img = ref_dec.img;
 
-  if (!compare_img(&enc_img, &dec_img)) {
+  if (!aom_compare_img(&enc_img, &dec_img)) {
     int y[4], u[4], v[4];
 
     *mismatch_seen = 1;
 
-    find_mismatch(&enc_img, &dec_img, y, u, v);
+    aom_find_mismatch(&enc_img, &dec_img, y, u, v);
     printf(
         "Encode/decode mismatch on frame %d at"
         " Y[%d, %d] {%d/%d},"
diff --git a/examples/encoder_util.c b/examples/encoder_util.c
new file mode 100644
index 0000000..e338fe6
--- /dev/null
+++ b/examples/encoder_util.c
@@ -0,0 +1,243 @@
+/*
+ * Copyright (c) 2017, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+// Utility functions used by encoder binaries.
+
+#include <string.h>
+
+#include "./encoder_util.h"
+#include "aom/aom_integer.h"
+
+#define mmin(a, b) ((a) < (b) ? (a) : (b))
+
+#if CONFIG_HIGHBITDEPTH
+// TODO(urvang): Refactor the highbd and lowbd version.
+void 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]) {
+  uint16_t *plane1, *plane2;
+  uint32_t stride1, stride2;
+  const uint32_t bsize = 64;
+  const uint32_t bsizey = bsize >> img1->y_chroma_shift;
+  const uint32_t bsizex = bsize >> img1->x_chroma_shift;
+  const uint32_t c_w =
+      (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
+  const uint32_t c_h =
+      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
+  int match = 1;
+  uint32_t i, j;
+  yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
+  plane1 = (uint16_t *)img1->planes[AOM_PLANE_Y];
+  plane2 = (uint16_t *)img2->planes[AOM_PLANE_Y];
+  stride1 = img1->stride[AOM_PLANE_Y] / 2;
+  stride2 = img2->stride[AOM_PLANE_Y] / 2;
+  for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
+    for (j = 0; match && j < img1->d_w; j += bsize) {
+      int k, l;
+      const int si = mmin(i + bsize, img1->d_h) - i;
+      const int sj = mmin(j + bsize, img1->d_w) - j;
+      for (k = 0; match && k < si; ++k) {
+        for (l = 0; match && l < sj; ++l) {
+          if (*(plane1 + (i + k) * stride1 + j + l) !=
+              *(plane2 + (i + k) * stride2 + j + l)) {
+            yloc[0] = i + k;
+            yloc[1] = j + l;
+            yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
+            yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
+            match = 0;
+            break;
+          }
+        }
+      }
+    }
+  }
+
+  uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
+  plane1 = (uint16_t *)img1->planes[AOM_PLANE_U];
+  plane2 = (uint16_t *)img2->planes[AOM_PLANE_U];
+  stride1 = img1->stride[AOM_PLANE_U] / 2;
+  stride2 = img2->stride[AOM_PLANE_U] / 2;
+  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
+    for (j = 0; match && j < c_w; j += bsizex) {
+      int k, l;
+      const int si = mmin(i + bsizey, c_h - i);
+      const int sj = mmin(j + bsizex, c_w - j);
+      for (k = 0; match && k < si; ++k) {
+        for (l = 0; match && l < sj; ++l) {
+          if (*(plane1 + (i + k) * stride1 + j + l) !=
+              *(plane2 + (i + k) * stride2 + j + l)) {
+            uloc[0] = i + k;
+            uloc[1] = j + l;
+            uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
+            uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
+            match = 0;
+            break;
+          }
+        }
+      }
+    }
+  }
+
+  vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
+  plane1 = (uint16_t *)img1->planes[AOM_PLANE_V];
+  plane2 = (uint16_t *)img2->planes[AOM_PLANE_V];
+  stride1 = img1->stride[AOM_PLANE_V] / 2;
+  stride2 = img2->stride[AOM_PLANE_V] / 2;
+  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
+    for (j = 0; match && j < c_w; j += bsizex) {
+      int k, l;
+      const int si = mmin(i + bsizey, c_h - i);
+      const int sj = mmin(j + bsizex, c_w - j);
+      for (k = 0; match && k < si; ++k) {
+        for (l = 0; match && l < sj; ++l) {
+          if (*(plane1 + (i + k) * stride1 + j + l) !=
+              *(plane2 + (i + k) * stride2 + j + l)) {
+            vloc[0] = i + k;
+            vloc[1] = j + l;
+            vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
+            vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
+            match = 0;
+            break;
+          }
+        }
+      }
+    }
+  }
+}
+#endif
+
+void aom_find_mismatch(const aom_image_t *const img1,
+                       const aom_image_t *const img2, int yloc[4], int uloc[4],
+                       int vloc[4]) {
+  const uint32_t bsize = 64;
+  const uint32_t bsizey = bsize >> img1->y_chroma_shift;
+  const uint32_t bsizex = bsize >> img1->x_chroma_shift;
+  const uint32_t c_w =
+      (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
+  const uint32_t c_h =
+      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
+  int match = 1;
+  uint32_t i, j;
+  yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
+  for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
+    for (j = 0; match && j < img1->d_w; j += bsize) {
+      int k, l;
+      const int si = mmin(i + bsize, img1->d_h) - i;
+      const int sj = mmin(j + bsize, img1->d_w) - j;
+      for (k = 0; match && k < si; ++k) {
+        for (l = 0; match && l < sj; ++l) {
+          if (*(img1->planes[AOM_PLANE_Y] +
+                (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
+              *(img2->planes[AOM_PLANE_Y] +
+                (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
+            yloc[0] = i + k;
+            yloc[1] = j + l;
+            yloc[2] = *(img1->planes[AOM_PLANE_Y] +
+                        (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
+            yloc[3] = *(img2->planes[AOM_PLANE_Y] +
+                        (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
+            match = 0;
+            break;
+          }
+        }
+      }
+    }
+  }
+
+  uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
+  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
+    for (j = 0; match && j < c_w; j += bsizex) {
+      int k, l;
+      const int si = mmin(i + bsizey, c_h - i);
+      const int sj = mmin(j + bsizex, c_w - j);
+      for (k = 0; match && k < si; ++k) {
+        for (l = 0; match && l < sj; ++l) {
+          if (*(img1->planes[AOM_PLANE_U] +
+                (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
+              *(img2->planes[AOM_PLANE_U] +
+                (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
+            uloc[0] = i + k;
+            uloc[1] = j + l;
+            uloc[2] = *(img1->planes[AOM_PLANE_U] +
+                        (i + k) * img1->stride[AOM_PLANE_U] + j + l);
+            uloc[3] = *(img2->planes[AOM_PLANE_U] +
+                        (i + k) * img2->stride[AOM_PLANE_U] + j + l);
+            match = 0;
+            break;
+          }
+        }
+      }
+    }
+  }
+  // TODO(urvang): Refactor the 3 for loops for Y, U, V?
+  vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
+  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
+    for (j = 0; match && j < c_w; j += bsizex) {
+      int k, l;
+      const int si = mmin(i + bsizey, c_h - i);
+      const int sj = mmin(j + bsizex, c_w - j);
+      for (k = 0; match && k < si; ++k) {
+        for (l = 0; match && l < sj; ++l) {
+          if (*(img1->planes[AOM_PLANE_V] +
+                (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
+              *(img2->planes[AOM_PLANE_V] +
+                (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
+            vloc[0] = i + k;
+            vloc[1] = j + l;
+            vloc[2] = *(img1->planes[AOM_PLANE_V] +
+                        (i + k) * img1->stride[AOM_PLANE_V] + j + l);
+            vloc[3] = *(img2->planes[AOM_PLANE_V] +
+                        (i + k) * img2->stride[AOM_PLANE_V] + j + l);
+            match = 0;
+            break;
+          }
+        }
+      }
+    }
+  }
+}
+
+int aom_compare_img(const aom_image_t *const img1,
+                    const aom_image_t *const img2) {
+  uint32_t l_w = img1->d_w;
+  uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
+  const uint32_t c_h =
+      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
+  uint32_t i;
+  int match = 1;
+
+  match &= (img1->fmt == img2->fmt);
+  match &= (img1->d_w == img2->d_w);
+  match &= (img1->d_h == img2->d_h);
+#if CONFIG_HIGHBITDEPTH
+  if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
+    l_w *= 2;
+    c_w *= 2;
+  }
+#endif
+
+  for (i = 0; i < img1->d_h; ++i)
+    match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
+                     img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
+                     l_w) == 0);
+
+  for (i = 0; i < c_h; ++i)
+    match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
+                     img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
+                     c_w) == 0);
+
+  for (i = 0; i < c_h; ++i)
+    match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
+                     img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
+                     c_w) == 0);
+
+  return match;
+}
diff --git a/examples/encoder_util.h b/examples/encoder_util.h
new file mode 100644
index 0000000..38deef0
--- /dev/null
+++ b/examples/encoder_util.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+// Utility functions used by encoder binaries.
+
+#ifndef EXAMPLES_ENCODER_UTIL_H_
+#define EXAMPLES_ENCODER_UTIL_H_
+
+#include "./aom_config.h"
+#include "aom/aom_image.h"
+
+// Returns mismatch location (?loc[0],?loc[1]) and the values at that location
+// in img1 (?loc[2]) and img2 (?loc[3]).
+#if CONFIG_HIGHBITDEPTH
+void 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]);
+#endif  // CONFIG_HIGHBITDEPTH
+
+void aom_find_mismatch(const aom_image_t *const img1,
+                       const aom_image_t *const img2, int yloc[4], int uloc[4],
+                       int vloc[4]);
+
+// Returns 1 if the two images match.
+int aom_compare_img(const aom_image_t *const img1,
+                    const aom_image_t *const img2);
+
+#endif  // EXAMPLES_ENCODER_UTIL_H_