aomdec: respect chroma-sample-position

BUG=aomedia:2096

Change-Id: I278c0ddd3acb5a4fb514bce5d6acd2d302c7807c
diff --git a/apps/aomdec.c b/apps/aomdec.c
index 839a948..35a598a 100644
--- a/apps/aomdec.c
+++ b/apps/aomdec.c
@@ -818,6 +818,7 @@
                 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
             scaled_img->bit_depth = img->bit_depth;
             scaled_img->monochrome = img->monochrome;
+            scaled_img->csp = img->csp;
           }
 
           if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
@@ -859,6 +860,7 @@
                   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,
@@ -884,7 +886,7 @@
               len = y4m_write_file_header(
                   y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
                   aom_input_ctx.height, &aom_input_ctx.framerate,
-                  img->monochrome, img->fmt, img->bit_depth);
+                  img->monochrome, img->csp, img->fmt, img->bit_depth);
               if (do_md5) {
                 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
               } else {
diff --git a/common/y4menc.c b/common/y4menc.c
index 2daee87..585d221 100644
--- a/common/y4menc.c
+++ b/common/y4menc.c
@@ -26,15 +26,27 @@
   }
 }
 
+// Return the Y4M name of the 8-bit colorspace, given the chroma position and
+// image format.
+const char *colorspace8(aom_chroma_sample_position_t csp, aom_img_fmt_t fmt) {
+  switch (fmt) {
+    case AOM_IMG_FMT_444A: return "C444alpha";
+    case AOM_IMG_FMT_I444: return "C444";
+    case AOM_IMG_FMT_I422: return "C422";
+    default:
+      if (csp == AOM_CSP_VERTICAL) {
+        return "C420mpeg2 XYSCSS=420MPEG2";
+      } else {
+        return "C420jpeg";
+      }
+  }
+}
+
 // Return the Y4M name of the colorspace, given the bit depth and image format.
-const char *colorspace(unsigned int bit_depth, aom_img_fmt_t fmt) {
+const char *colorspace(unsigned int bit_depth, aom_chroma_sample_position_t csp,
+                       aom_img_fmt_t fmt) {
   switch (bit_depth) {
-    case 8:
-      return fmt == AOM_IMG_FMT_444A
-                 ? "C444alpha"
-                 : fmt == AOM_IMG_FMT_I444
-                       ? "C444"
-                       : fmt == AOM_IMG_FMT_I422 ? "C422" : "C420jpeg";
+    case 8: return colorspace8(csp, fmt);
     case 9:
       return fmt == AOM_IMG_FMT_I44416
                  ? "C444p9 XYSCSS=444P9"
@@ -66,9 +78,10 @@
 
 int y4m_write_file_header(char *buf, size_t len, int width, int height,
                           const struct AvxRational *framerate, int monochrome,
-                          aom_img_fmt_t fmt, unsigned int bit_depth) {
+                          aom_chroma_sample_position_t csp, aom_img_fmt_t fmt,
+                          unsigned int bit_depth) {
   const char *color = monochrome ? monochrome_colorspace(bit_depth)
-                                 : colorspace(bit_depth, fmt);
+                                 : colorspace(bit_depth, csp, fmt);
   return snprintf(buf, len, "YUV4MPEG2 W%u H%u F%u:%u I%c %s\n", width, height,
                   framerate->numerator, framerate->denominator, 'p', color);
 }
diff --git a/common/y4menc.h b/common/y4menc.h
index 4020eb4..f6d5fd8 100644
--- a/common/y4menc.h
+++ b/common/y4menc.h
@@ -24,7 +24,8 @@
 
 int y4m_write_file_header(char *buf, size_t len, int width, int height,
                           const struct AvxRational *framerate, int monochrome,
-                          aom_img_fmt_t fmt, unsigned int bit_depth);
+                          aom_chroma_sample_position_t csp, aom_img_fmt_t fmt,
+                          unsigned int bit_depth);
 int y4m_write_frame_header(char *buf, size_t len);
 void y4m_write_image_file(const aom_image_t *img, const int *planes,
                           FILE *file);
diff --git a/test/test-data.sha1 b/test/test-data.sha1
index 67b9454..b6ee347 100644
--- a/test/test-data.sha1
+++ b/test/test-data.sha1
@@ -504,3 +504,4 @@
 1889da5ee1708007e47bb887470ac477e1d7ba01 *av1-1-b8-03-sizeup.mkv
 8de81b170635d456602dc8923a8b39c534d01fa8 *av1-1-b8-03-sizeup.mkv.md5
 d3ed7de0aa8c155fe35e0f5f4203240710d31383 *park_joy_90p_8_420_monochrome.y4m
+5b3f0907407b809aa66b62cb080feda8c92454ca *park_joy_90p_8_420_vertical_csp.y4m
diff --git a/test/test_data_util.cmake b/test/test_data_util.cmake
index 28b08a8..45c9514 100644
--- a/test/test_data_util.cmake
+++ b/test/test_data_util.cmake
@@ -35,6 +35,7 @@
             "park_joy_90p_8_420_a10-1.y4m"
             "park_joy_90p_8_420.y4m"
             "park_joy_90p_8_420_monochrome.y4m"
+            "park_joy_90p_8_420_vertical_csp.y4m"
             "park_joy_90p_8_422.y4m"
             "park_joy_90p_8_444.y4m"
             "desktop_credits.y4m"
diff --git a/test/y4m_test.cc b/test/y4m_test.cc
index de24128..6cc75ef 100644
--- a/test/y4m_test.cc
+++ b/test/y4m_test.cc
@@ -39,6 +39,8 @@
     "e5406275b9fc6bb3436c31d4a05c1cab" },
   { "park_joy_90p_8_420_monochrome.y4m", 8, AOM_IMG_FMT_I420,
     "95ef5bf6218580588be24a5271bb6a7f" },
+  { "park_joy_90p_8_420_vertical_csp.y4m", 8, AOM_IMG_FMT_I420,
+    "f53a40fec15254ac312527339d9c686b" },
   { "park_joy_90p_8_422.y4m", 8, AOM_IMG_FMT_I422,
     "284a47a47133b12884ec3a14e959a0b6" },
   { "park_joy_90p_8_444.y4m", 8, AOM_IMG_FMT_I444,
@@ -147,7 +149,8 @@
     tmpfile_ = new libaom_test::TempOutFile;
     ASSERT_TRUE(tmpfile_->file() != NULL);
     y4m_write_file_header(buf, sizeof(buf), kWidth, kHeight, &framerate,
-                          img()->monochrome, y4m_.aom_fmt, y4m_.bit_depth);
+                          img()->monochrome, img()->csp, y4m_.aom_fmt,
+                          y4m_.bit_depth);
     fputs(buf, tmpfile_->file());
     for (unsigned int i = start_; i < limit_; i++) {
       y4m_write_frame_header(buf, sizeof(buf));