From 5b750c2ff314134140c2b8f3968bee6103ff5dcd Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Wed, 17 May 2023 12:27:50 +0200 Subject: [PATCH] lavc video: prefer libaom-av1 for ARM macs While on x86_64 is libsvtav1 still slightly ahead, on the M1 mac, the AOM AV1 performs significantly better (2x faster). The above holds for native builds, it actually looks like the x86_64 build running SVT AV1 on M1 mac doesn't run correctly at all - it produces just blank picture (green as is zeroed YCbCr buffer). --- src/utils/misc.cpp | 27 +++++++++++++++++++++++++++ src/utils/misc.h | 3 ++- src/video_compress/libavcodec.cpp | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/utils/misc.cpp b/src/utils/misc.cpp index 04ee438f5..7d89b37dd 100644 --- a/src/utils/misc.cpp +++ b/src/utils/misc.cpp @@ -45,6 +45,7 @@ #include #include +#include #include #include #include @@ -57,6 +58,11 @@ #include "utils/misc.h" #include "utils/color_out.h" +#ifdef __APPLE__ +#include +#include +#endif + #define STRERROR_BUF_LEN 1024 using std::invalid_argument; @@ -305,3 +311,24 @@ uint32_t parse_uint32(const char *value_str) noexcept(false) return val; } +/** + * Checks whether host machine is an ARM Mac (native or running x86_64 code with Rosetta2) +*/ +bool is_arm_mac() { +#ifdef __APPLE__ + char machine[256]; + size_t len = sizeof machine; + if (sysctlbyname("hw.machine", &machine, &len, NULL, 0) != 0) { + perror("sysctlbyname hw.machine"); + return false; + } + int proc_translated = -1; // Rosetta2 - 0 off, 1 on; sysctl ENOENT - x86_64 mac + len = sizeof proc_translated; + if (int ret = sysctlbyname("sysctl.proc_translated", &proc_translated, &len, NULL, 0); ret != 0 && errno != ENOENT) { + perror("sysctlbyname sysctl.proc_translated"); + } + return strstr(machine, "arm") != NULL || proc_translated == 1; +#else + return false; +#endif +} diff --git a/src/utils/misc.h b/src/utils/misc.h index d0bb1ece1..cb0817499 100644 --- a/src/utils/misc.h +++ b/src/utils/misc.h @@ -4,7 +4,7 @@ * @author Martin Piatka */ /* - * Copyright (c) 2014-2022 CESNET z.s.p.o. + * Copyright (c) 2014-2023 CESNET z.s.p.o. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -64,6 +64,7 @@ int get_framerate_d(double framerate); const char *ug_strerror(int errnum); void ug_perror(const char *s); int get_cpu_core_count(void); +bool is_arm_mac(void); struct key_val { const char *key; diff --git a/src/video_compress/libavcodec.cpp b/src/video_compress/libavcodec.cpp index 5e04be72a..8db4f1801 100644 --- a/src/video_compress/libavcodec.cpp +++ b/src/video_compress/libavcodec.cpp @@ -212,7 +212,7 @@ static map codec_params = { 502 }}, { AV1, codec_params_t{ - [](bool) { return "libsvtav1"; }, + [](bool) { return is_arm_mac() ? "libaom-av1" : "libsvtav1"; }, 0.1, get_av1_preset, setparam_h264_h265_av1,