Add YUV444 and YUV422 support for tune=butteraugli

BD-rate change of tune=butteraugli in the following formats:
baseline tune=psnr
               PSNR      SSIM    Butteraugli
Hdrplus_420    4.02     -2.58    -18.30
Hdrplus_422    3.38     -3.76    -18.30
Hdrplus_444    3.12     -4.54    -17.33

Change-Id: I1cfbca1c6c58c1038bd1d7e6cd56ed53e2211908
diff --git a/aom_dsp/butteraugli.c b/aom_dsp/butteraugli.c
index 7ce2324..dff9fed 100644
--- a/aom_dsp/butteraugli.c
+++ b/aom_dsp/butteraugli.c
@@ -21,34 +21,58 @@
                          float *dist_map) {
   (void)bit_depth;
   assert(bit_depth == 8);
-  assert(source->y_width == source->uv_width * 2);
   const int width = source->y_crop_width;
   const int height = source->y_crop_height;
+  const int ss_x = source->subsampling_x;
+  const int ss_y = source->subsampling_y;
 
-  size_t buffer_size = width * height * 3;
-  uint8_t *src_rgb = (uint8_t *)aom_malloc(buffer_size);
-  uint8_t *distorted_rgb = (uint8_t *)aom_malloc(buffer_size);
-  if (!src_rgb || !distorted_rgb) {
-    aom_free(src_rgb);
-    aom_free(distorted_rgb);
+  const size_t stride_argb = width * 4;
+  const size_t buffer_size = height * stride_argb;
+  uint8_t *src_argb = (uint8_t *)aom_malloc(buffer_size);
+  uint8_t *distorted_argb = (uint8_t *)aom_malloc(buffer_size);
+  if (!src_argb || !distorted_argb) {
+    aom_free(src_argb);
+    aom_free(distorted_argb);
     return 0;
   }
 
-  I420ToRGB24Matrix(source->y_buffer, source->y_stride, source->u_buffer,
-                    source->uv_stride, source->v_buffer, source->uv_stride,
-                    src_rgb, width * 3, &kYuvH709Constants, width, height);
-  I420ToRGB24Matrix(distorted->y_buffer, distorted->y_stride,
-                    distorted->u_buffer, distorted->uv_stride,
-                    distorted->v_buffer, distorted->uv_stride, distorted_rgb,
-                    width * 3, &kYuvH709Constants, width, height);
+  if (ss_x == 1 && ss_y == 1) {
+    I420ToARGBMatrix(source->y_buffer, source->y_stride, source->u_buffer,
+                     source->uv_stride, source->v_buffer, source->uv_stride,
+                     src_argb, stride_argb, &kYuvH709Constants, width, height);
+    I420ToARGBMatrix(distorted->y_buffer, distorted->y_stride,
+                     distorted->u_buffer, distorted->uv_stride,
+                     distorted->v_buffer, distorted->uv_stride, distorted_argb,
+                     stride_argb, &kYuvH709Constants, width, height);
+  } else if (ss_x == 1 && ss_y == 0) {
+    I422ToARGBMatrix(source->y_buffer, source->y_stride, source->u_buffer,
+                     source->uv_stride, source->v_buffer, source->uv_stride,
+                     src_argb, stride_argb, &kYuvH709Constants, width, height);
+    I422ToARGBMatrix(distorted->y_buffer, distorted->y_stride,
+                     distorted->u_buffer, distorted->uv_stride,
+                     distorted->v_buffer, distorted->uv_stride, distorted_argb,
+                     stride_argb, &kYuvH709Constants, width, height);
+  } else if (ss_x == 0 && ss_y == 0) {
+    I444ToARGBMatrix(source->y_buffer, source->y_stride, source->u_buffer,
+                     source->uv_stride, source->v_buffer, source->uv_stride,
+                     src_argb, stride_argb, &kYuvH709Constants, width, height);
+    I444ToARGBMatrix(distorted->y_buffer, distorted->y_stride,
+                     distorted->u_buffer, distorted->uv_stride,
+                     distorted->v_buffer, distorted->uv_stride, distorted_argb,
+                     stride_argb, &kYuvH709Constants, width, height);
+  } else {
+    aom_free(src_argb);
+    aom_free(distorted_argb);
+    return 0;
+  }
 
-  JxlPixelFormat pixel_format = { 3, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0 };
+  JxlPixelFormat pixel_format = { 4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0 };
   JxlButteraugliApi *api = JxlButteraugliApiCreate(NULL);
   JxlButteraugliApiSetHFAsymmetry(api, 0.8f);
 
   JxlButteraugliResult *result = JxlButteraugliCompute(
-      api, width, height, &pixel_format, src_rgb, buffer_size, &pixel_format,
-      distorted_rgb, buffer_size);
+      api, width, height, &pixel_format, src_argb, buffer_size, &pixel_format,
+      distorted_argb, buffer_size);
 
   const float *distmap = NULL;
   uint32_t row_stride;
@@ -56,8 +80,8 @@
   if (distmap == NULL) {
     JxlButteraugliApiDestroy(api);
     JxlButteraugliResultDestroy(result);
-    aom_free(src_rgb);
-    aom_free(distorted_rgb);
+    aom_free(src_argb);
+    aom_free(distorted_argb);
     return 0;
   }
 
@@ -69,7 +93,7 @@
 
   JxlButteraugliApiDestroy(api);
   JxlButteraugliResultDestroy(result);
-  aom_free(src_rgb);
-  aom_free(distorted_rgb);
+  aom_free(src_argb);
+  aom_free(distorted_argb);
   return 1;
 }
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 0c2d90c..5cfde3a 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -619,8 +619,8 @@
 
 #if CONFIG_TUNE_BUTTERAUGLI
   if (ctx->extra_cfg.tuning == AOM_TUNE_BUTTERAUGLI) {
-    if (img->x_chroma_shift != 1 || img->y_chroma_shift != 1) {
-      ERROR("Only YV12/I420 images supported in tune=butteraugli mode.");
+    if (img->bit_depth > 8) {
+      ERROR("Only 8 bit depth images supported in tune=butteraugli mode.");
     }
     if ((img->cp != 0 && img->cp != AOM_CICP_CP_BT_709) ||
         (img->tc != 0 && img->tc != AOM_CICP_TC_BT_709) ||
diff --git a/av1/encoder/tune_butteraugli.c b/av1/encoder/tune_butteraugli.c
index 39940e8..620468d 100644
--- a/av1/encoder/tune_butteraugli.c
+++ b/av1/encoder/tune_butteraugli.c
@@ -28,6 +28,8 @@
   const int bit_depth = cpi->td.mb.e_mbd.bd;
   const int width = source->y_crop_width;
   const int height = source->y_crop_height;
+  const int ss_x = source->subsampling_x;
+  const int ss_y = source->subsampling_y;
 
   float *diffmap;
   CHECK_MEM_ERROR(cm, diffmap, aom_malloc(width * height * sizeof(*diffmap)));
@@ -55,6 +57,7 @@
       const int x_start = col * block_w;
       float dbutteraugli = 0.0f;
       float dmse = 0.0f;
+      float px_count = 0.0f;
 
       // Loop through each pixel.
       for (int y = y_start; y < y_start + block_h && y < height; y++) {
@@ -63,22 +66,28 @@
           float px_diff = source->y_buffer[y * source->y_stride + x] -
                           recon->y_buffer[y * recon->y_stride + x];
           dmse += px_diff * px_diff;
+          px_count += 1.0f;
         }
       }
-      for (int y = y_start; y < y_start + block_h && y < height; y += 2) {
-        for (int x = x_start; x < x_start + block_w && x < width; x += 2) {
-          const int src_px_index = y / 2 * source->uv_stride + x / 2;
-          const int recon_px_index = y / 2 * recon->uv_stride + x / 2;
+      const int y_end = AOMMIN((y_start >> ss_y) + (block_h >> ss_y),
+                               (height + ss_y) >> ss_y);
+      for (int y = y_start >> ss_y; y < y_end; y++) {
+        const int x_end = AOMMIN((x_start >> ss_x) + (block_w >> ss_x),
+                                 (width + ss_x) >> ss_x);
+        for (int x = x_start >> ss_x; x < x_end; x++) {
+          const int src_px_index = y * source->uv_stride + x;
+          const int recon_px_index = y * recon->uv_stride + x;
           const float px_diff_u = (float)(source->u_buffer[src_px_index] -
                                           recon->u_buffer[recon_px_index]);
           const float px_diff_v = (float)(source->v_buffer[src_px_index] -
                                           recon->v_buffer[recon_px_index]);
           dmse += px_diff_u * px_diff_u + px_diff_v * px_diff_v;
+          px_count += 2.0f;
         }
       }
 
       dbutteraugli = powf(dbutteraugli, 1.0f / 12.0f);
-      dmse = dmse / (2.0f * (float)block_w * (float)block_h);
+      dmse = dmse / px_count;
       // 'K' is used to balance the rate-distortion distribution between PSNR
       // and Butteraugli.
       const double K = 0.4;
@@ -166,10 +175,12 @@
                      int width, int height) {
   copy_plane(src->y_buffer, src->y_stride, dst->y_buffer, dst->y_stride, width,
              height);
+  const int width_uv = (width + src->subsampling_x) >> src->subsampling_x;
+  const int height_uv = (height + src->subsampling_y) >> src->subsampling_y;
   copy_plane(src->u_buffer, src->uv_stride, dst->u_buffer, dst->uv_stride,
-             width / 2, height / 2);
+             width_uv, height_uv);
   copy_plane(src->v_buffer, src->uv_stride, dst->v_buffer, dst->uv_stride,
-             width / 2, height / 2);
+             width_uv, height_uv);
 }
 
 static void zero_plane(uint8_t *dst, int dst_stride, int h) {
@@ -192,9 +203,11 @@
   const int width = cpi->source->y_crop_width;
   const int height = cpi->source->y_crop_height;
   const int bit_depth = cpi->td.mb.e_mbd.bd;
+  const int ss_x = cpi->source->subsampling_x;
+  const int ss_y = cpi->source->subsampling_y;
   if (dst->buffer_alloc_sz == 0) {
     aom_alloc_frame_buffer(
-        dst, width, height, 1, 1, cm->seq_params.use_highbitdepth,
+        dst, width, height, ss_x, ss_y, cm->seq_params.use_highbitdepth,
         cpi->oxcf.border_in_pixels, cm->features.byte_alignment);
   }
   av1_copy_and_extend_frame(cpi->source, dst);
@@ -202,7 +215,7 @@
   YV12_BUFFER_CONFIG *const resized_dst = &cpi->butteraugli_info.resized_source;
   if (resized_dst->buffer_alloc_sz == 0) {
     aom_alloc_frame_buffer(
-        resized_dst, width / resize_factor, height / resize_factor, 1, 1,
+        resized_dst, width / resize_factor, height / resize_factor, ss_x, ss_y,
         cm->seq_params.use_highbitdepth, cpi->oxcf.border_in_pixels,
         cm->features.byte_alignment);
   }
@@ -221,11 +234,13 @@
   AV1_COMMON *const cm = &cpi->common;
   const int width = cpi->source->y_crop_width;
   const int height = cpi->source->y_crop_height;
+  const int ss_x = cpi->source->subsampling_x;
+  const int ss_y = cpi->source->subsampling_y;
 
   YV12_BUFFER_CONFIG resized_recon;
   memset(&resized_recon, 0, sizeof(resized_recon));
   aom_alloc_frame_buffer(
-      &resized_recon, width / resize_factor, height / resize_factor, 1, 1,
+      &resized_recon, width / resize_factor, height / resize_factor, ss_x, ss_y,
       cm->seq_params.use_highbitdepth, cpi->oxcf.border_in_pixels,
       cm->features.byte_alignment);
   copy_img(&cpi->common.cur_frame->buf, &resized_recon, width / resize_factor,