vmaf.c 6.4 KB

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