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