Skip to content

Instantly share code, notes, and snippets.

@1480c1
Last active July 10, 2020 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1480c1/a91a5f03fda5492669979c79a1e060e9 to your computer and use it in GitHub Desktop.
Save 1480c1/a91a5f03fda5492669979c79a1e060e9 to your computer and use it in GitHub Desktop.
Use at your own risk. Makes aomec.exe look at the executable's directory for the vmaf model files on windows
From e2187913064634450c0f2e89007e085b9e62f3bf Mon Sep 17 00:00:00 2001
From: Christopher Degawa <ccom@randomderp.com>
Date: Thu, 16 Jan 2020 18:20:57 +0000
Subject: [PATCH] idk
---
aom/aomcx.h | 2 +-
apps/aomenc.c | 2 +-
av1/av1_cx_iface.c | 142 ++++++++++++++++++++++++++++++------------
av1/encoder/encoder.h | 2 +-
4 files changed, 106 insertions(+), 42 deletions(-)
diff --git a/aom/aomcx.h b/aom/aomcx.h
index fc42243ff..b98ae5f55 100644
--- a/aom/aomcx.h
+++ b/aom/aomcx.h
@@ -1697,7 +1697,7 @@ AOM_CTRL_USE_TYPE(AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, unsigned int)
AOM_CTRL_USE_TYPE(AV1E_ENABLE_EXT_TILE_DEBUG, unsigned int)
#define AOM_CTRL_AV1E_ENABLE_EXT_TILE_DEBUG
-AOM_CTRL_USE_TYPE(AV1E_SET_VMAF_MODEL_PATH, const char *)
+AOM_CTRL_USE_TYPE(AV1E_SET_VMAF_MODEL_PATH, char *)
#define AOM_CTRL_AV1E_SET_VMAF_MODEL_PATH
AOM_CTRL_USE_TYPE(AV1E_SET_FILM_GRAIN_TEST_VECTOR, int)
diff --git a/apps/aomenc.c b/apps/aomenc.c
index 76ed03ada..2039a3966 100644
--- a/apps/aomenc.c
+++ b/apps/aomenc.c
@@ -1103,7 +1103,7 @@ struct stream_config {
// whether to use 16bit internal buffers
int use_16bit_internal;
#if CONFIG_TUNE_VMAF
- const char *vmaf_model_path;
+ char *vmaf_model_path;
#endif
};
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 0ef0fd877..0e7e987a4 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -48,7 +48,7 @@ struct av1_extracfg {
unsigned int gf_min_pyr_height;
unsigned int gf_max_pyr_height;
aom_tune_metric tuning;
- const char *vmaf_model_path;
+ char *vmaf_model_path;
unsigned int cq_level; // constrained quality level
unsigned int rc_max_intra_bitrate_pct;
unsigned int rc_max_inter_bitrate_pct;
@@ -154,43 +154,43 @@ struct av1_extracfg {
};
static struct av1_extracfg default_extra_cfg = {
- 0, // cpu_used
- 1, // enable_auto_alt_ref
- 0, // enable_auto_bwd_ref
- 0, // noise_sensitivity
- 0, // sharpness
- 0, // static_thresh
- 1, // row_mt
- 0, // tile_columns
- 0, // tile_rows
- 1, // enable_tpl_model
- 1, // enable_keyframe_filtering
- 7, // arnr_max_frames
- 5, // arnr_strength
- 0, // min_gf_interval; 0 -> default decision
- 0, // max_gf_interval; 0 -> default decision
- 0, // gf_min_pyr_height
- 5, // gf_max_pyr_height
- AOM_TUNE_PSNR, // tuning
- "/usr/local/share/model/vmaf_v0.6.1.pkl", // VMAF model path
- 10, // cq_level
- 0, // rc_max_intra_bitrate_pct
- 0, // rc_max_inter_bitrate_pct
- 0, // gf_cbr_boost_pct
- 0, // lossless
- 1, // enable_cdef
- 1, // enable_restoration
- 0, // force_video_mode
- 1, // enable_obmc
- 3, // disable_trellis_quant
- 0, // enable_qm
- DEFAULT_QM_Y, // qm_y
- DEFAULT_QM_U, // qm_u
- DEFAULT_QM_V, // qm_v
- DEFAULT_QM_FIRST, // qm_min
- DEFAULT_QM_LAST, // qm_max
- 1, // max number of tile groups
- 0, // mtu_size
+ 0, // cpu_used
+ 1, // enable_auto_alt_ref
+ 0, // enable_auto_bwd_ref
+ 0, // noise_sensitivity
+ 0, // sharpness
+ 0, // static_thresh
+ 1, // row_mt
+ 0, // tile_columns
+ 0, // tile_rows
+ 1, // enable_tpl_model
+ 1, // enable_keyframe_filtering
+ 7, // arnr_max_frames
+ 5, // arnr_strength
+ 0, // min_gf_interval; 0 -> default decision
+ 0, // max_gf_interval; 0 -> default decision
+ 0, // gf_min_pyr_height
+ 5, // gf_max_pyr_height
+ AOM_TUNE_PSNR, // tuning
+ NULL, // VMAF model path
+ 10, // cq_level
+ 0, // rc_max_intra_bitrate_pct
+ 0, // rc_max_inter_bitrate_pct
+ 0, // gf_cbr_boost_pct
+ 0, // lossless
+ 1, // enable_cdef
+ 1, // enable_restoration
+ 0, // force_video_mode
+ 1, // enable_obmc
+ 3, // disable_trellis_quant
+ 0, // enable_qm
+ DEFAULT_QM_Y, // qm_y
+ DEFAULT_QM_U, // qm_u
+ DEFAULT_QM_V, // qm_v
+ DEFAULT_QM_FIRST, // qm_min
+ DEFAULT_QM_LAST, // qm_max
+ 1, // max number of tile groups
+ 0, // mtu_size
AOM_TIMING_UNSPECIFIED, // No picture timing signaling in bitstream
0, // frame_parallel_decoding_mode
1, // enable dual filter
@@ -310,6 +310,66 @@ struct aom_codec_alg_priv {
STATS_BUFFER_CTX stats_buf_context;
};
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef _WIN32
+#include <io.h>
+#else
+#define __USE_XOPEN2K
+#include <unistd.h>
+#endif
+
+/* From: https://github.com/msys2/MINGW-packages/blob/master/mingw-w64-pathtools/pathtools.c */
+static int get_executable_path(char const *argv0, char *result,
+ signed long max_size) {
+ char *system_result = malloc(sizeof(char) * max_size);
+ signed long system_result_size = -1;
+ signed long result_size = -1;
+
+#ifdef _WIN32
+ unsigned long bufsize = (unsigned long)max_size;
+ system_result_size = GetModuleFileNameA(NULL, system_result, bufsize);
+ if (!system_result_size || system_result_size == (ssize_t)bufsize)
+ /* Error, possibly not enough space. */
+ system_result_size = -1;
+ else {
+ /* Early conversion to unix slashes instead of more changes
+ everywhere else .. */
+ char *winslash;
+ system_result[system_result_size] = '\0';
+ while ((winslash = strchr(system_result, '\\'))) *winslash = '/';
+ }
+#elif defined(__APPLE__)
+ uint32_t bufsize = (uint32_t)max_size;
+ if (!_NSGetExecutablePath(system_result, &bufsize))
+ system_result_size = (ssize_t)bufsize;
+#else
+ system_result_size = readlink("/proc/self/exe", system_result, max_size);
+#endif
+ /* Use argv0 as a default in-case of failure */
+ if (system_result_size != -1) {
+ strncpy(result, system_result, system_result_size);
+ result[system_result_size] = '\0';
+ } else {
+ if (argv0) {
+ strncpy(result, argv0, max_size);
+ result[max_size - 1] = '\0';
+ } else
+ result[0] = '\0';
+ }
+ result_size = strlen(result);
+ free(system_result);
+ return result_size;
+}
+
+static void get_vmaf_model_path(char *vmaf_path) {
+ get_executable_path(NULL, vmaf_path, 256);
+ strncpy(strrchr(vmaf_path, '/') + 1, "vmaf_v0.6.1.pkl", 16);
+ if (access(vmaf_path, 0) == -1)
+ strncpy(vmaf_path, "/usr/local/share/model/vmaf_v0.6.1.pkl", 39);
+}
+
static INLINE int gcd(int64_t a, int b) {
int remainder;
while (b > 0) {
@@ -1918,7 +1978,10 @@ static aom_codec_err_t encoder_init(aom_codec_ctx_t *ctx) {
priv->extra_cfg = default_extra_cfg;
aom_once(av1_initialize_enc);
-
+ if (!priv->extra_cfg.vmaf_model_path) {
+ priv->extra_cfg.vmaf_model_path = malloc(sizeof(char) * 256);
+ get_vmaf_model_path(priv->extra_cfg.vmaf_model_path);
+ }
res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
if (res == AOM_CODEC_OK) {
@@ -1990,6 +2053,7 @@ static void destroy_stats_buffer(STATS_BUFFER_CTX *stats_buf_context,
static aom_codec_err_t encoder_destroy(aom_codec_alg_priv_t *ctx) {
free(ctx->cx_data);
+ if (ctx->extra_cfg.vmaf_model_path) free(ctx->extra_cfg.vmaf_model_path);
destroy_context_and_bufferpool(ctx->cpi, ctx->buffer_pool);
if (ctx->cpi_lap) {
// As both cpi and cpi_lap have the same lookahead_ctx, it is already freed
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 1e1b7a983..a15205187 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -395,7 +395,7 @@ typedef struct AV1EncoderConfig {
aom_fixed_buf_t two_pass_stats_in;
aom_tune_metric tuning;
- const char *vmaf_model_path;
+ char *vmaf_model_path;
aom_tune_content content;
int use_highbitdepth;
aom_color_primaries_t color_primaries;
--
2.25.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment