summaryrefslogtreecommitdiff
path: root/mpv/lua-modules/auto-profiles-functions.lua
diff options
context:
space:
mode:
authorilotterytea <ilotterytea@proton.me>2024-05-26 20:10:26 +0500
committerilotterytea <ilotterytea@proton.me>2024-05-26 20:10:26 +0500
commit036c889c4a4f7f59d1e1a592586b54c5c5e93005 (patch)
treeaa76d678790abc79f24edf83c17a564eb2c6f65d /mpv/lua-modules/auto-profiles-functions.lua
initial commitHEADmaster
Diffstat (limited to 'mpv/lua-modules/auto-profiles-functions.lua')
-rw-r--r--mpv/lua-modules/auto-profiles-functions.lua122
1 files changed, 122 insertions, 0 deletions
diff --git a/mpv/lua-modules/auto-profiles-functions.lua b/mpv/lua-modules/auto-profiles-functions.lua
new file mode 100644
index 0000000..539a84d
--- /dev/null
+++ b/mpv/lua-modules/auto-profiles-functions.lua
@@ -0,0 +1,122 @@
+local utils = require 'mp.utils'
+local msg = require 'mp.msg'
+
+-- Quality levels
+local HIGH = "High Quality"
+local MID = "Mid Quality"
+local LOW = "Low Quality"
+-- Platform
+local is_linux, is_osx, is_windows = false, false, false
+local exec_cache = {}
+
+
+local function exec(process, force_exec)
+ local key = table.concat(process, " ")
+ if force_exec or exec_cache[key] == nil or exec_cache[key].error then
+ local p_ret = utils.subprocess({args = process, playback_only = false})
+ exec_cache[key] = p_ret
+ if p_ret.error and p_ret.error == "init" then
+ msg.error("executable not found: " .. key)
+ end
+ return p_ret
+ else
+ return exec_cache[key]
+ end
+end
+
+
+local function get_platform()
+ local is_linux = false
+ local is_osx = false
+ local is_windows = type(package) == 'table' and type(package.config) == 'string' and string.sub(package.config, 1, 1) == '\\'
+ if not is_windows then
+ uname = exec({"uname"})
+ is_linux = uname.stdout == "Linux\n"
+ is_osx = uname.stdout == "Darwin\n"
+ end
+ return is_linux, is_osx, is_windows
+end
+
+
+function on_battery()
+ if is_osx then
+ local bat = exec({"/usr/bin/pmset", "-g", "batt"}, true)
+ return string.match(bat.stdout, "Now drawing from 'Battery Power'")
+ elseif is_linux then
+ local res = exec({"/bin/cat", "/sys/class/power_supply/AC0/online"}, true)
+ return res.stdout == "0\n"
+ elseif is_windows then
+ msg.warn("on_battery() not implemented on windows. PRs welcome")
+ end
+ msg.warn("assuming AC power")
+ return false
+end
+
+
+-- This is a crude attempt to figure out if a (beefy) dedicated GPU is present.
+-- Can't identify the actually used GPU but works when we assume that an existing
+-- dedicated GPU can/will be used in case we are drawing power from AC.
+ -- local r = exec({"lshw", "-C", 'display'})
+ -- local r = exec({"nvidia-smi"})
+ -- r.stdout = string.lower(r.stdout)
+ -- return string.find(r.stdout, "amd") ~= nil or string.find(r.stdout, "nvidia") ~= nil
+ -- return string.find(r.stdout, "mpv") ~= nil
+function dedicated_gpu()
+ if is_osx then
+ local r = exec({"system_profiler", "SPDisplaysDataType"})
+ return string.find(r.stdout, "Chipset Model: Radeon") ~= nil or string.find(r.stdout, "Chipset Model: NVIDIA GeForce") ~= nil
+ -- Untested
+ elseif is_linux then
+ local nv = os.getenv("__NV_PRIME_RENDER_OFFLOAD")
+ return nv ~= nil
+ elseif is_windows then
+ msg.warn("dedicated_gpu() not implemented on windows. PRs welcome")
+ end
+ msg.warn("assuming dedicated GPU")
+ return true
+end
+
+
+local function determine_level(width, height, fps)
+
+ if on_battery() then
+ return LOW
+ end
+
+ if dedicated_gpu() then
+ if width > 4096 then
+ return MID
+ end
+ if width > 1920 and fps > 61 then
+ return MID
+ end
+ return HIGH
+ else
+ if width > 1919 then
+ return LOW
+ end
+ if fps > 58 then
+ return LOW
+ end
+ return MID
+ end
+
+ msg.error("could not determine profile")
+ msg.warn("assuming HIGH")
+ return HIGH
+end
+
+
+local function is_level(level)
+ return function(width, height, fps)
+ local l = determine_level(width, height, fps)
+ return l == level
+ end
+end
+
+
+is_high = is_level(HIGH)
+is_mid = is_level(MID)
+is_low = is_level(LOW)
+
+is_linux, is_osx, is_windows = get_platform()