Replace the butteraugli api

Replace the butteraugli api with the official release 0.3.2

BUG=aomedia:2965

Change-Id: I93d98e2f9d24b547fac999bf6262b511e6b77c75
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 02f20a6..f182576 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -88,6 +88,7 @@
             "${AOM_ROOT}/third_party/libyuv/include/libyuv/row.h"
             "${AOM_ROOT}/third_party/libyuv/include/libyuv/scale.h"
             "${AOM_ROOT}/third_party/libyuv/include/libyuv/scale_row.h"
+            "${AOM_ROOT}/third_party/libyuv/source/convert_argb.cc"
             "${AOM_ROOT}/third_party/libyuv/source/cpu_id.cc"
             "${AOM_ROOT}/third_party/libyuv/source/planar_functions.cc"
             "${AOM_ROOT}/third_party/libyuv/source/row_any.cc"
@@ -381,6 +382,14 @@
   list(APPEND AOM_APP_TARGETS ${AOM_DECODER_EXAMPLE_TARGETS})
 endif()
 
+if(CONFIG_LIBYUV OR CONFIG_TUNE_BUTTERAUGLI)
+  add_library(yuv OBJECT ${AOM_LIBYUV_SOURCES})
+  if(NOT MSVC)
+    target_compile_options(yuv PRIVATE -Wno-unused-parameter)
+  endif()
+  include_directories("${AOM_ROOT}/third_party/libyuv/include")
+endif()
+
 if(CONFIG_AV1_ENCODER)
   if(ENABLE_EXAMPLES)
     add_executable(aomenc "${AOM_ROOT}/apps/aomenc.c"
@@ -438,32 +447,40 @@
               ${AOM_ENCODER_TOOL_TARGETS})
 
   if(CONFIG_TUNE_BUTTERAUGLI)
-    find_package(PkgConfig)
-    pkg_check_modules(LIBJXL REQUIRED libjxl)
-    target_link_libraries(aom PRIVATE ${LIBJXL_LDFLAGS} ${LIBJXL_LIBRARIES})
+    find_library(LIBJXL_LIBRARIES libjxl.a)
+    find_library(LIBHWY_LIBRARIES libhwy.a)
+    find_library(LIBSKCMS_LIBRARIES libskcms.a)
+    find_path(LIBJXL_INCLUDE_DIRS butteraugli.h PATH_SUFFIXES jxl)
+    if(LIBJXL_LIBRARIES
+       AND LIBHWY_LIBRARIES
+       AND LIBSKCMS_LIBRARIES
+       AND LIBJXL_INCLUDE_DIRS)
+      message(STATUS "Found JXL library: ${LIBJXL_LIBRARIES} "
+                     "${LIBHWY_LIBRARIES} ${LIBJXLT_LIBRARIES} "
+                     "${LIBSKCMS_LIBRARIES}")
+      message(STATUS "Found JXL include: ${LIBJXL_INCLUDE_DIRS}")
+    else()
+      message(FATAL_ERROR "JXL library not found.")
+    endif()
+    target_link_libraries(aom
+                          PRIVATE ${LIBJXL_LDFLAGS} ${LIBJXL_LIBRARIES}
+                                  ${LIBHWY_LIBRARIES} ${LIBJXLT_LIBRARIES}
+                                  ${LIBSKCMS_LIBRARIES})
     target_include_directories(aom PRIVATE ${LIBJXL_INCLUDE_DIRS})
     if(LIBJXL_CFLAGS)
       append_compiler_flag("${LIBJXL_CFLAGS}")
     endif()
 
-    pkg_check_modules(LIBHWY REQUIRED libhwy)
-    target_link_libraries(aom PRIVATE ${LIBHWY_LDFLAGS} ${LIBHWY_LIBRARIES})
-    target_include_directories(aom PRIVATE ${LIBLIBHWY_INCLUDE_DIRS})
-    if(LIBHWY_CFLAGS)
-      append_compiler_flag("${LIBHWY_CFLAGS}")
-    endif()
-
-    pkg_check_modules(LIBJXLT REQUIRED libjxl_threads)
-    target_link_libraries(aom PRIVATE ${LIBJXLT_LDFLAGS} ${LIBJXLT_LIBRARIES})
-    target_include_directories(aom PRIVATE ${LIBJXLT_INCLUDE_DIRS})
-    if(LIBJXLT_CFLAGS)
-      append_compiler_flag("${LIBJXLT_CFLAGS}")
-    endif()
-
     set_target_properties(aom PROPERTIES LINKER_LANGUAGE CXX)
     if(BUILD_SHARED_LIBS)
       set_target_properties(aom_static PROPERTIES LINKER_LANGUAGE CXX)
     endif()
+
+    list(APPEND AOM_LIB_TARGETS yuv)
+    target_sources(aom PRIVATE $<TARGET_OBJECTS:yuv>)
+    if(BUILD_SHARED_LIBS)
+      target_sources(aom_static PRIVATE $<TARGET_OBJECTS:yuv>)
+    endif()
   endif()
 
   if(CONFIG_USE_VMAF_RC AND NOT CONFIG_TUNE_VMAF)
@@ -588,12 +605,6 @@
 
 if(ENABLE_EXAMPLES OR ENABLE_TESTS OR ENABLE_TOOLS)
   if(CONFIG_LIBYUV)
-    add_library(yuv OBJECT ${AOM_LIBYUV_SOURCES})
-    if(NOT MSVC)
-      target_compile_options(yuv PRIVATE -Wno-unused-parameter)
-    endif()
-    include_directories("${AOM_ROOT}/third_party/libyuv/include")
-
     # Add to existing targets.
     foreach(aom_app ${AOM_APP_TARGETS})
       target_sources(${aom_app} PRIVATE $<TARGET_OBJECTS:yuv>)
diff --git a/aom_dsp/butteraugli.c b/aom_dsp/butteraugli.c
index 7ba38a9..1b47aba 100644
--- a/aom_dsp/butteraugli.c
+++ b/aom_dsp/butteraugli.c
@@ -9,29 +9,53 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */
 
-// TODO(sdeng): update the jxl api.
 #include <assert.h>
-#include <jxl/encode.h>
+#include <jxl/butteraugli.h>
 
 #include "aom_dsp/butteraugli.h"
+#include "aom_mem/aom_mem.h"
+#include "third_party/libyuv/include/libyuv/convert_argb.h"
 
 void aom_calc_butteraugli(const YV12_BUFFER_CONFIG *source,
                           const YV12_BUFFER_CONFIG *distorted, int bit_depth,
                           float *dist_map) {
+  (void)bit_depth;
   assert(bit_depth == 8);
   assert(source->y_width == source->uv_width * 2);
-  uint8_t *src_y = source->y_buffer;
-  uint8_t *src_u = source->u_buffer;
-  uint8_t *src_v = source->v_buffer;
-  uint8_t *distorted_y = distorted->y_buffer;
-  uint8_t *distorted_u = distorted->u_buffer;
-  uint8_t *distorted_v = distorted->v_buffer;
   const int width = source->y_width;
   const int height = source->y_height;
-  double butteraugli_diffvalue;
-  JxlCalcButteraugliYuv420(width, height, src_y, source->y_stride, src_u, src_v,
-                           source->uv_stride, distorted_y, distorted->y_stride,
-                           distorted_u, distorted_v, distorted->uv_stride,
-                           dist_map, &butteraugli_diffvalue);
-  (void)bit_depth;
+
+  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);
+  // TODO(sdeng): Convert them to sRGB.
+  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);
+
+  JxlPixelFormat pixel_format = { 3, 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);
+
+  const float *distmap;
+  uint32_t row_stride;
+  JxlButteraugliResultGetDistmap(result, &distmap, &row_stride);
+
+  for (int j = 0; j < height; ++j) {
+    for (int i = 0; i < width; ++i) {
+      dist_map[j * width + i] = distmap[j * row_stride + i];
+    }
+  }
+
+  JxlButteraugliResultDestroy(result);
+  aom_free(src_rgb);
+  aom_free(distorted_rgb);
 }
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index fab9092..38c022d 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -619,7 +619,13 @@
 
 #if CONFIG_TUNE_BUTTERAUGLI
   if (img->x_chroma_shift != 1 || img->y_chroma_shift != 1) {
-    ERROR("Only I420 images supported in tune=butteraugli mode.");
+    ERROR("Only YV12/I420 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) ||
+      (img->mc != 0 && img->mc != AOM_CICP_MC_BT_709)) {
+    ERROR("Only BT.709 images supported in tune=butteraugli mode.");
   }
 #endif