• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <assert.h>
13 #include <jxl/butteraugli.h>
14 
15 #include "aom_dsp/butteraugli.h"
16 #include "aom_mem/aom_mem.h"
17 #include "third_party/libyuv/include/libyuv/convert_argb.h"
18 
aom_calc_butteraugli(const YV12_BUFFER_CONFIG * source,const YV12_BUFFER_CONFIG * distorted,int bit_depth,aom_matrix_coefficients_t matrix_coefficients,aom_color_range_t color_range,float * dist_map)19 int aom_calc_butteraugli(const YV12_BUFFER_CONFIG *source,
20                          const YV12_BUFFER_CONFIG *distorted, int bit_depth,
21                          aom_matrix_coefficients_t matrix_coefficients,
22                          aom_color_range_t color_range, float *dist_map) {
23   (void)bit_depth;
24   assert(bit_depth == 8);
25   const int width = source->y_crop_width;
26   const int height = source->y_crop_height;
27   const int ss_x = source->subsampling_x;
28   const int ss_y = source->subsampling_y;
29 
30   const struct YuvConstants *yuv_constants;
31   if (matrix_coefficients == AOM_CICP_MC_BT_709) {
32     if (color_range == AOM_CR_FULL_RANGE) return 0;
33     yuv_constants = &kYuvH709Constants;
34   } else {
35     yuv_constants = color_range == AOM_CR_FULL_RANGE ? &kYuvJPEGConstants
36                                                      : &kYuvI601Constants;
37   }
38 
39   const int stride_argb = width * 4;
40   const size_t buffer_size = (size_t)height * stride_argb;
41   uint8_t *src_argb = (uint8_t *)aom_malloc(buffer_size);
42   uint8_t *distorted_argb = (uint8_t *)aom_malloc(buffer_size);
43   if (!src_argb || !distorted_argb) {
44     aom_free(src_argb);
45     aom_free(distorted_argb);
46     return 0;
47   }
48 
49   if (ss_x == 1 && ss_y == 1) {
50     I420ToARGBMatrix(source->y_buffer, source->y_stride, source->u_buffer,
51                      source->uv_stride, source->v_buffer, source->uv_stride,
52                      src_argb, stride_argb, yuv_constants, width, height);
53     I420ToARGBMatrix(distorted->y_buffer, distorted->y_stride,
54                      distorted->u_buffer, distorted->uv_stride,
55                      distorted->v_buffer, distorted->uv_stride, distorted_argb,
56                      stride_argb, yuv_constants, width, height);
57   } else if (ss_x == 1 && ss_y == 0) {
58     I422ToARGBMatrix(source->y_buffer, source->y_stride, source->u_buffer,
59                      source->uv_stride, source->v_buffer, source->uv_stride,
60                      src_argb, stride_argb, yuv_constants, width, height);
61     I422ToARGBMatrix(distorted->y_buffer, distorted->y_stride,
62                      distorted->u_buffer, distorted->uv_stride,
63                      distorted->v_buffer, distorted->uv_stride, distorted_argb,
64                      stride_argb, yuv_constants, width, height);
65   } else if (ss_x == 0 && ss_y == 0) {
66     I444ToARGBMatrix(source->y_buffer, source->y_stride, source->u_buffer,
67                      source->uv_stride, source->v_buffer, source->uv_stride,
68                      src_argb, stride_argb, yuv_constants, width, height);
69     I444ToARGBMatrix(distorted->y_buffer, distorted->y_stride,
70                      distorted->u_buffer, distorted->uv_stride,
71                      distorted->v_buffer, distorted->uv_stride, distorted_argb,
72                      stride_argb, yuv_constants, width, height);
73   } else {
74     aom_free(src_argb);
75     aom_free(distorted_argb);
76     return 0;
77   }
78 
79   JxlPixelFormat pixel_format = { 4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0 };
80   JxlButteraugliApi *api = JxlButteraugliApiCreate(NULL);
81   JxlButteraugliApiSetHFAsymmetry(api, 0.8f);
82 
83   JxlButteraugliResult *result = JxlButteraugliCompute(
84       api, width, height, &pixel_format, src_argb, buffer_size, &pixel_format,
85       distorted_argb, buffer_size);
86 
87   const float *distmap = NULL;
88   uint32_t row_stride;
89   JxlButteraugliResultGetDistmap(result, &distmap, &row_stride);
90   if (distmap == NULL) {
91     JxlButteraugliApiDestroy(api);
92     JxlButteraugliResultDestroy(result);
93     aom_free(src_argb);
94     aom_free(distorted_argb);
95     return 0;
96   }
97 
98   for (int j = 0; j < height; ++j) {
99     for (int i = 0; i < width; ++i) {
100       dist_map[j * width + i] = distmap[j * row_stride + i];
101     }
102   }
103 
104   JxlButteraugliApiDestroy(api);
105   JxlButteraugliResultDestroy(result);
106   aom_free(src_argb);
107   aom_free(distorted_argb);
108   return 1;
109 }
110