Move aom_shift_img() to tools_common

Moved aom_shift_img() to tools_common, so that it can be called from
other files.

Change-Id: I3a4fec1056fb2222573a6887883ff92bea0df4dc
diff --git a/apps/aomdec.c b/apps/aomdec.c
index 8e98f0b..2f5f6b0 100644
--- a/apps/aomdec.c
+++ b/apps/aomdec.c
@@ -427,13 +427,6 @@
   }
 }
 
-static int img_shifted_realloc_required(const aom_image_t *img,
-                                        const aom_image_t *shifted,
-                                        aom_img_fmt_t required_fmt) {
-  return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
-         required_fmt != shifted->fmt;
-}
-
 static int main_loop(int argc, const char **argv_) {
   aom_codec_ctx_t decoder;
   char *fn = NULL;
@@ -854,37 +847,8 @@
           output_bit_depth = img->bit_depth;
         }
         // Shift up or down if necessary
-        if (output_bit_depth != 0) {
-          const aom_img_fmt_t shifted_fmt =
-              output_bit_depth == 8 ? img->fmt & ~AOM_IMG_FMT_HIGHBITDEPTH
-                                    : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
-
-          if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
-            if (img_shifted &&
-                img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
-              aom_img_free(img_shifted);
-              img_shifted = NULL;
-            }
-            if (img_shifted) {
-              img_shifted->monochrome = img->monochrome;
-            }
-            if (!img_shifted) {
-              img_shifted =
-                  aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
-              img_shifted->bit_depth = output_bit_depth;
-              img_shifted->monochrome = img->monochrome;
-              img_shifted->csp = img->csp;
-            }
-            if (output_bit_depth > img->bit_depth) {
-              aom_img_upshift(img_shifted, img,
-                              output_bit_depth - img->bit_depth);
-            } else {
-              aom_img_downshift(img_shifted, img,
-                                img->bit_depth - output_bit_depth);
-            }
-            img = img_shifted;
-          }
-        }
+        if (output_bit_depth != 0)
+          aom_shift_img(output_bit_depth, &img, &img_shifted);
 
         aom_input_ctx.width = img->d_w;
         aom_input_ctx.height = img->d_h;
diff --git a/common/tools_common.c b/common/tools_common.c
index 2f21bb3..ef5e9a7 100644
--- a/common/tools_common.c
+++ b/common/tools_common.c
@@ -424,3 +424,44 @@
     lowbd_img_downshift(dst, src, down_shift);
   }
 }
+
+static int img_shifted_realloc_required(const aom_image_t *img,
+                                        const aom_image_t *shifted,
+                                        aom_img_fmt_t required_fmt) {
+  return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
+         required_fmt != shifted->fmt;
+}
+
+void aom_shift_img(unsigned int output_bit_depth, aom_image_t **img_ptr,
+                   aom_image_t **img_shifted_ptr) {
+  aom_image_t *img = *img_ptr;
+  aom_image_t *img_shifted = *img_shifted_ptr;
+
+  const aom_img_fmt_t shifted_fmt = output_bit_depth == 8
+                                        ? img->fmt & ~AOM_IMG_FMT_HIGHBITDEPTH
+                                        : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
+
+  if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
+    if (img_shifted &&
+        img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
+      aom_img_free(img_shifted);
+      img_shifted = NULL;
+    }
+    if (img_shifted) {
+      img_shifted->monochrome = img->monochrome;
+    }
+    if (!img_shifted) {
+      img_shifted = aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
+      img_shifted->bit_depth = output_bit_depth;
+      img_shifted->monochrome = img->monochrome;
+      img_shifted->csp = img->csp;
+    }
+    if (output_bit_depth > img->bit_depth) {
+      aom_img_upshift(img_shifted, img, output_bit_depth - img->bit_depth);
+    } else {
+      aom_img_downshift(img_shifted, img, img->bit_depth - output_bit_depth);
+    }
+    *img_shifted_ptr = img_shifted;
+    *img_ptr = img_shifted;
+  }
+}
diff --git a/common/tools_common.h b/common/tools_common.h
index 4e1d12f..c981361 100644
--- a/common/tools_common.h
+++ b/common/tools_common.h
@@ -155,6 +155,8 @@
 void aom_img_upshift(aom_image_t *dst, const aom_image_t *src, int input_shift);
 void aom_img_downshift(aom_image_t *dst, const aom_image_t *src,
                        int down_shift);
+void aom_shift_img(unsigned int output_bit_depth, aom_image_t **img_ptr,
+                   aom_image_t **img_shifted_ptr);
 void aom_img_truncate_16_to_8(aom_image_t *dst, const aom_image_t *src);
 
 #ifdef __cplusplus