Signal color-range in Y4M output

Only set if the color-range is full.

BUG=aomedia:2876

Change-Id: I3882bcbc7440f83a99268292531557c2859e3522
diff --git a/apps/aomdec.c b/apps/aomdec.c
index 60805aa..b1929a6 100644
--- a/apps/aomdec.c
+++ b/apps/aomdec.c
@@ -885,7 +885,8 @@
               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->csp, img->fmt, img->bit_depth);
+                  img->monochrome, img->csp, img->fmt, img->bit_depth,
+                  img->range);
               if (img->csp == AOM_CSP_COLOCATED) {
                 fprintf(stderr,
                         "Warning: Y4M lacks a colorspace for colocated "
diff --git a/apps/aomenc.c b/apps/aomenc.c
index f69ffd0..2756a4f 100644
--- a/apps/aomenc.c
+++ b/apps/aomenc.c
@@ -488,6 +488,7 @@
 #if CONFIG_TUNE_VMAF
   const char *vmaf_model_path;
 #endif
+  aom_color_range_t color_range;
 };
 
 struct stream_state {
@@ -703,6 +704,7 @@
       input->framerate.denominator = input->y4m.fps_d;
       input->fmt = input->y4m.aom_fmt;
       input->bit_depth = input->y4m.bit_depth;
+      input->color_range = input->y4m.color_range;
     } else
       fatal("Unsupported Y4M stream.");
   } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
@@ -1342,6 +1344,8 @@
     AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1E_SET_FILM_GRAIN_TABLE,
                                   stream->config.film_grain_filename);
   }
+  AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1E_SET_COLOR_RANGE,
+                                stream->config.color_range);
 
 #if CONFIG_AV1_DECODER
   if (global->test_decode != TEST_DECODE_OFF) {
@@ -1946,6 +1950,7 @@
 
     FOREACH_STREAM(stream, streams) {
       set_stream_dimensions(stream, input.width, input.height);
+      stream->config.color_range = input.color_range;
     }
     FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
 
diff --git a/common/tools_common.h b/common/tools_common.h
index 873a720..025bca2 100644
--- a/common/tools_common.h
+++ b/common/tools_common.h
@@ -116,6 +116,7 @@
 #if CONFIG_AV1_ENCODER
   y4m_input y4m;
 #endif
+  aom_color_range_t color_range;
 };
 
 #ifdef __cplusplus
diff --git a/common/y4menc.c b/common/y4menc.c
index d228eaa..eaeedba 100644
--- a/common/y4menc.c
+++ b/common/y4menc.c
@@ -83,11 +83,16 @@
 int y4m_write_file_header(char *buf, size_t len, int width, int height,
                           const struct AvxRational *framerate, int monochrome,
                           aom_chroma_sample_position_t csp, aom_img_fmt_t fmt,
-                          unsigned int bit_depth) {
+                          unsigned int bit_depth, aom_color_range_t range) {
   const char *color = monochrome ? monochrome_colorspace(bit_depth)
                                  : colorspace(bit_depth, csp, fmt);
-  return snprintf(buf, len, "YUV4MPEG2 W%d H%d F%d:%d I%c %s\n", width, height,
-                  framerate->numerator, framerate->denominator, 'p', color);
+  const char *color_range = "";  // Default assumption is studio range.
+  if (range == AOM_CR_FULL_RANGE) {
+    color_range = " XCOLORRANGE=FULL";
+  }
+  return snprintf(buf, len, "YUV4MPEG2 W%d H%d F%d:%d Ip %s%s\n", width, height,
+                  framerate->numerator, framerate->denominator, color,
+                  color_range);
 }
 
 int y4m_write_frame_header(char *buf, size_t len) {
diff --git a/common/y4menc.h b/common/y4menc.h
index f6d5fd8..6484efc 100644
--- a/common/y4menc.h
+++ b/common/y4menc.h
@@ -20,12 +20,12 @@
 extern "C" {
 #endif
 
-#define Y4M_BUFFER_SIZE 128
+#define Y4M_BUFFER_SIZE 256
 
 int y4m_write_file_header(char *buf, size_t len, int width, int height,
                           const struct AvxRational *framerate, int monochrome,
                           aom_chroma_sample_position_t csp, aom_img_fmt_t fmt,
-                          unsigned int bit_depth);
+                          unsigned int bit_depth, aom_color_range_t range);
 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/common/y4minput.c b/common/y4minput.c
index 9d83505..46632bd 100644
--- a/common/y4minput.c
+++ b/common/y4minput.c
@@ -53,6 +53,28 @@
   return len == size;
 }
 
+// Stores the color range in 'y4m_ctx', returning 1 if successfully parsed,
+// 0 otherwise.
+static int parse_color_range(y4m_input *y4m_ctx, const char *buf) {
+  // Note that default is studio range.
+  if (strcmp(buf, "LIMITED") == 0) {
+    return 1;
+  }
+  if (strcmp(buf, "FULL") == 0) {
+    y4m_ctx->color_range = AOM_CR_FULL_RANGE;
+    return 1;
+  }
+  fprintf(stderr, "Unknown color range value: %s\n", buf);
+  return 0;
+}
+
+static int parse_metadata(y4m_input *y4m_ctx, const char *buf) {
+  if (strncmp(buf, "COLORRANGE=", 11) == 0) {
+    return parse_color_range(y4m_ctx, buf + 11);
+  }
+  return 1;  // No support for other metadata, just ignore them.
+}
+
 static int y4m_parse_tags(y4m_input *_y4m, char *_tags) {
   char *p;
   char *q;
@@ -90,7 +112,10 @@
         memcpy(_y4m->chroma_type, p + 1, q - p - 1);
         _y4m->chroma_type[q - p - 1] = '\0';
       } break;
-        /*Ignore unknown tags.*/
+      case 'X': {
+        if (!parse_metadata(_y4m, p + 1)) return -1;
+      } break;
+      default: break; /*Ignore unknown tags.*/
     }
   }
   return 0;
@@ -146,6 +171,7 @@
   y4m_ctx->par_n = 0;
   y4m_ctx->par_d = 0;
   y4m_ctx->interlace = '?';
+  y4m_ctx->color_range = AOM_CR_STUDIO_RANGE;
   snprintf(y4m_ctx->chroma_type, sizeof(y4m_ctx->chroma_type), "420");
 
   // Find one tag at a time.
@@ -840,8 +866,6 @@
 }
 
 static const char TAG[] = "YUV4MPEG2";
-// Temporary until arbitrary header parsing submitted.
-#define Y4M_HEADER_BUF_SIZE 200
 
 int y4m_input_open(y4m_input *y4m_ctx, FILE *file, char *skip_buffer,
                    int num_skip, aom_chroma_sample_position_t csp,
diff --git a/common/y4minput.h b/common/y4minput.h
index 6a6b20e..2472007 100644
--- a/common/y4minput.h
+++ b/common/y4minput.h
@@ -55,6 +55,7 @@
   enum aom_img_fmt aom_fmt;
   int bps;
   unsigned int bit_depth;
+  aom_color_range_t color_range;
 };
 
 /**
diff --git a/test/y4m_test.cc b/test/y4m_test.cc
index e2a28e7..690e524 100644
--- a/test/y4m_test.cc
+++ b/test/y4m_test.cc
@@ -150,7 +150,7 @@
     ASSERT_TRUE(tmpfile_->file() != NULL);
     y4m_write_file_header(buf, sizeof(buf), kWidth, kHeight, &framerate,
                           img()->monochrome, img()->csp, y4m_.aom_fmt,
-                          y4m_.bit_depth);
+                          y4m_.bit_depth, AOM_CR_STUDIO_RANGE);
     fputs(buf, tmpfile_->file());
     for (unsigned int i = start_; i < limit_; i++) {
       y4m_write_frame_header(buf, sizeof(buf));
@@ -198,6 +198,7 @@
   EXPECT_EQ(y4m.fps_n, 30);
   EXPECT_EQ(y4m.fps_d, 1);
   EXPECT_EQ(y4m.interlace, 'p');
+  EXPECT_EQ(y4m.color_range, AOM_CR_STUDIO_RANGE);
   EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
   y4m_input_close(&y4m);
 }
@@ -225,8 +226,58 @@
   EXPECT_EQ(y4m.fps_n, 30);
   EXPECT_EQ(y4m.fps_d, 1);
   EXPECT_EQ(y4m.interlace, 'p');
+  EXPECT_EQ(y4m.color_range, AOM_CR_STUDIO_RANGE);
   EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
   y4m_input_close(&y4m);
 }
 
+static const char kY4MFullRangeHeader[] =
+    "YUV4MPEG2 W4 H4 F30:1 Ip A0:0 C420jpeg XYSCSS=420JPEG XCOLORRANGE=FULL\n"
+    "FRAME\n"
+    "012345678912345601230123";
+
+TEST(Y4MHeaderTest, FullRangeHeader) {
+  libaom_test::TempOutFile tmpfile;
+  FILE *f = tmpfile.file();
+  fwrite(kY4MFullRangeHeader, 1, sizeof(kY4MFullRangeHeader), f);
+  fflush(f);
+  EXPECT_EQ(fseek(f, 0, 0), 0);
+
+  y4m_input y4m;
+  EXPECT_EQ(y4m_input_open(&y4m, f, NULL, 0, AOM_CSP_UNKNOWN,
+                           /*only_420=*/0),
+            0);
+  EXPECT_EQ(y4m.pic_w, 4);
+  EXPECT_EQ(y4m.pic_h, 4);
+  EXPECT_EQ(y4m.fps_n, 30);
+  EXPECT_EQ(y4m.fps_d, 1);
+  EXPECT_EQ(y4m.interlace, 'p');
+  EXPECT_EQ(strcmp("420jpeg", y4m.chroma_type), 0);
+  EXPECT_EQ(y4m.color_range, AOM_CR_FULL_RANGE);
+  y4m_input_close(&y4m);
+}
+
+TEST(Y4MHeaderTest, WriteStudioColorRange) {
+  char buf[128];
+  struct AvxRational framerate = { .numerator = 30, .denominator = 1 };
+  EXPECT_GE(y4m_write_file_header(
+                buf, /*len=*/128, /*width=*/4, /*height=*/5, &framerate,
+                /*monochrome=*/0, AOM_CSP_UNKNOWN, AOM_IMG_FMT_I420,
+                /*bit_depth=*/8, AOM_CR_STUDIO_RANGE),
+            0);
+  EXPECT_EQ(strcmp("YUV4MPEG2 W4 H5 F30:1 Ip C420jpeg\n", buf), 0);
+}
+
+TEST(Y4MHeaderTest, WriteFullColorRange) {
+  char buf[128];
+  struct AvxRational framerate = { .numerator = 30, .denominator = 1 };
+  EXPECT_GE(y4m_write_file_header(
+                buf, /*len=*/128, /*width=*/4, /*height=*/5, &framerate,
+                /*monochrome=*/0, AOM_CSP_UNKNOWN, AOM_IMG_FMT_I420,
+                /*bit_depth=*/8, AOM_CR_FULL_RANGE),
+            0);
+  EXPECT_EQ(strcmp("YUV4MPEG2 W4 H5 F30:1 Ip C420jpeg XCOLORRANGE=FULL\n", buf),
+            0);
+}
+
 }  // namespace