• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019, 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 "aom_dsp/vmaf.h"
13 
14 #include <assert.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #ifdef _WIN32
19 #include <process.h>
20 #else
21 #include <unistd.h>
22 #endif
23 
24 #include <libvmaf/libvmaf.h>
25 #include "aom_dsp/blend.h"
26 
vmaf_fatal_error(const char * message)27 static void vmaf_fatal_error(const char *message) {
28   fprintf(stderr, "Fatal error: %s\n", message);
29   exit(EXIT_FAILURE);
30 }
31 
aom_init_vmaf_model(VmafModel ** vmaf_model,const char * model_path)32 void aom_init_vmaf_model(VmafModel **vmaf_model, const char *model_path) {
33   if (*vmaf_model != NULL) return;
34   VmafModelConfig model_cfg;
35   model_cfg.flags = VMAF_MODEL_FLAG_DISABLE_CLIP;
36   model_cfg.name = "vmaf";
37 
38   if (vmaf_model_load_from_path(vmaf_model, &model_cfg, model_path)) {
39     vmaf_fatal_error("Failed to load VMAF model.");
40   }
41 }
42 
aom_close_vmaf_model(VmafModel * vmaf_model)43 void aom_close_vmaf_model(VmafModel *vmaf_model) {
44   vmaf_model_destroy(vmaf_model);
45 }
46 
copy_picture(const int bit_depth,const YV12_BUFFER_CONFIG * src,VmafPicture * dst)47 static void copy_picture(const int bit_depth, const YV12_BUFFER_CONFIG *src,
48                          VmafPicture *dst) {
49   const int width = src->y_width;
50   const int height = src->y_height;
51 
52   if (bit_depth > 8) {
53     uint16_t *src_ptr = CONVERT_TO_SHORTPTR(src->y_buffer);
54     uint16_t *dst_ptr = dst->data[0];
55 
56     for (int row = 0; row < height; ++row) {
57       memcpy(dst_ptr, src_ptr, width * sizeof(dst_ptr[0]));
58       src_ptr += src->y_stride;
59       dst_ptr += dst->stride[0] / 2;
60     }
61   } else {
62     uint8_t *src_ptr = src->y_buffer;
63     uint8_t *dst_ptr = (uint8_t *)dst->data[0];
64 
65     for (int row = 0; row < height; ++row) {
66       memcpy(dst_ptr, src_ptr, width * sizeof(dst_ptr[0]));
67       src_ptr += src->y_stride;
68       dst_ptr += dst->stride[0];
69     }
70   }
71 }
72 
aom_init_vmaf_context(VmafContext ** vmaf_context,VmafModel * vmaf_model,bool cal_vmaf_neg)73 void aom_init_vmaf_context(VmafContext **vmaf_context, VmafModel *vmaf_model,
74                            bool cal_vmaf_neg) {
75   // TODO(sdeng): make them CLI arguments.
76   VmafConfiguration cfg;
77   cfg.log_level = VMAF_LOG_LEVEL_NONE;
78   cfg.n_threads = 0;
79   cfg.n_subsample = 0;
80   cfg.cpumask = 0;
81 
82   if (vmaf_init(vmaf_context, cfg)) {
83     vmaf_fatal_error("Failed to init VMAF context.");
84   }
85 
86   if (cal_vmaf_neg) {
87     VmafFeatureDictionary *vif_feature = NULL;
88     if (vmaf_feature_dictionary_set(&vif_feature, "vif_enhn_gain_limit",
89                                     "1.0")) {
90       vmaf_fatal_error("Failed to set vif_enhn_gain_limit.");
91     }
92     if (vmaf_model_feature_overload(vmaf_model, "float_vif", vif_feature)) {
93       vmaf_fatal_error("Failed to use feature float_vif.");
94     }
95 
96     VmafFeatureDictionary *adm_feature = NULL;
97     if (vmaf_feature_dictionary_set(&adm_feature, "adm_enhn_gain_limit",
98                                     "1.0")) {
99       vmaf_fatal_error("Failed to set adm_enhn_gain_limit.");
100     }
101     if (vmaf_model_feature_overload(vmaf_model, "adm", adm_feature)) {
102       vmaf_fatal_error("Failed to use feature float_adm.");
103     }
104   }
105 
106   VmafFeatureDictionary *motion_force_zero = NULL;
107   if (vmaf_feature_dictionary_set(&motion_force_zero, "motion_force_zero",
108                                   "1")) {
109     vmaf_fatal_error("Failed to set motion_force_zero.");
110   }
111   if (vmaf_model_feature_overload(vmaf_model, "float_motion",
112                                   motion_force_zero)) {
113     vmaf_fatal_error("Failed to use feature float_motion.");
114   }
115 
116   if (vmaf_use_features_from_model(*vmaf_context, vmaf_model)) {
117     vmaf_fatal_error("Failed to load feature extractors from VMAF model.");
118   }
119 }
120 
aom_close_vmaf_context(VmafContext * vmaf_context)121 void aom_close_vmaf_context(VmafContext *vmaf_context) {
122   if (vmaf_close(vmaf_context)) {
123     vmaf_fatal_error("Failed to close VMAF context.");
124   }
125 }
126 
aom_calc_vmaf(VmafModel * vmaf_model,const YV12_BUFFER_CONFIG * source,const YV12_BUFFER_CONFIG * distorted,int bit_depth,bool cal_vmaf_neg,double * vmaf)127 void aom_calc_vmaf(VmafModel *vmaf_model, const YV12_BUFFER_CONFIG *source,
128                    const YV12_BUFFER_CONFIG *distorted, int bit_depth,
129                    bool cal_vmaf_neg, double *vmaf) {
130   VmafContext *vmaf_context;
131   aom_init_vmaf_context(&vmaf_context, vmaf_model, cal_vmaf_neg);
132   const int frame_index = 0;
133   VmafPicture ref, dist;
134   if (vmaf_picture_alloc(&ref, VMAF_PIX_FMT_YUV420P, bit_depth, source->y_width,
135                          source->y_height) ||
136       vmaf_picture_alloc(&dist, VMAF_PIX_FMT_YUV420P, bit_depth,
137                          source->y_width, source->y_height)) {
138     vmaf_fatal_error("Failed to alloc VMAF pictures.");
139   }
140   copy_picture(bit_depth, source, &ref);
141   copy_picture(bit_depth, distorted, &dist);
142   if (vmaf_read_pictures(vmaf_context, &ref, &dist,
143                          /*picture index=*/frame_index)) {
144     vmaf_fatal_error("Failed to read VMAF pictures.");
145   }
146 
147   if (vmaf_read_pictures(vmaf_context, NULL, NULL, 0)) {
148     vmaf_fatal_error("Failed to flush context.");
149   }
150 
151   vmaf_picture_unref(&ref);
152   vmaf_picture_unref(&dist);
153 
154   vmaf_score_at_index(vmaf_context, vmaf_model, vmaf, frame_index);
155   aom_close_vmaf_context(vmaf_context);
156 }
157 
aom_read_vmaf_image(VmafContext * vmaf_context,const YV12_BUFFER_CONFIG * source,const YV12_BUFFER_CONFIG * distorted,int bit_depth,int frame_index)158 void aom_read_vmaf_image(VmafContext *vmaf_context,
159                          const YV12_BUFFER_CONFIG *source,
160                          const YV12_BUFFER_CONFIG *distorted, int bit_depth,
161                          int frame_index) {
162   VmafPicture ref, dist;
163   if (vmaf_picture_alloc(&ref, VMAF_PIX_FMT_YUV420P, bit_depth, source->y_width,
164                          source->y_height) ||
165       vmaf_picture_alloc(&dist, VMAF_PIX_FMT_YUV420P, bit_depth,
166                          source->y_width, source->y_height)) {
167     vmaf_fatal_error("Failed to alloc VMAF pictures.");
168   }
169   copy_picture(bit_depth, source, &ref);
170   copy_picture(bit_depth, distorted, &dist);
171   if (vmaf_read_pictures(vmaf_context, &ref, &dist,
172                          /*picture index=*/frame_index)) {
173     vmaf_fatal_error("Failed to read VMAF pictures.");
174   }
175 
176   vmaf_picture_unref(&ref);
177   vmaf_picture_unref(&dist);
178 }
179 
aom_calc_vmaf_at_index(VmafContext * vmaf_context,VmafModel * vmaf_model,int frame_index)180 double aom_calc_vmaf_at_index(VmafContext *vmaf_context, VmafModel *vmaf_model,
181                               int frame_index) {
182   double vmaf;
183   if (vmaf_score_at_index(vmaf_context, vmaf_model, &vmaf, frame_index)) {
184     vmaf_fatal_error("Failed to calc VMAF scores.");
185   }
186   return vmaf;
187 }
188 
aom_flush_vmaf_context(VmafContext * vmaf_context)189 void aom_flush_vmaf_context(VmafContext *vmaf_context) {
190   if (vmaf_read_pictures(vmaf_context, NULL, NULL, 0)) {
191     vmaf_fatal_error("Failed to flush context.");
192   }
193 }
194