summaryrefslogtreecommitdiff
path: root/ar/.config/mpv/unused_scipts/xrandr.lua
blob: 43ce59f53b5d8f81e38f475197ce212a02f1ba78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
-- use xrandr command to set output to best fitting fps rate
--  when playing videos with mpv.

utils = require 'mp.utils'

-- if you want your display output switched to a certain mode during playback,
--  use e.g. "--script-opts=xrandr-output-mode=1920x1080"
xrandr_output_mode = mp.get_opt("xrandr-output-mode")

xrandr_blacklist = {}
function xrandr_parse_blacklist()
   -- use e.g. "--script-opts=xrandr-blacklist=25" to have xrand.lua not use 25Hz refresh rate

	-- Parse the optional "blacklist" from a string into an array for later use.
	-- For now, we only support a list of rates, since the "mode" is not subject
	--  to automatic change (mpv is better at scaling than most displays) and
	--  this also makes the blacklist option more easy to specify:
	local b = mp.get_opt("xrandr-blacklist")
	if (b == nil) then
		return
	end
	
	local i = 1
	for s in string.gmatch(b, "([^, ]+)") do
		xrandr_blacklist[i] = 0.0 + s
		i = i+1
	end
end
xrandr_parse_blacklist()


function xrandr_check_blacklist(mode, rate)
	-- check if (mode, rate) is black-listed - e.g. because the
	--  computer display output is known to be incompatible with the
	--  display at this specific mode/rate 
	
	for i=1,#xrandr_blacklist do
		r = xrandr_blacklist[i]
		
		if (r == rate) then
			mp.msg.log("v", "will not use mode '" .. mode .. "' with rate " .. rate .. " because option --script-opts=xrandr-blacklist said so")
			return true
		end
	end
	
	return false
end

xrandr_detect_done = false
xrandr_modes = {}
xrandr_connected_outputs = {}
function xrandr_detect_available_rates()
	if (xrandr_detect_done) then
		return
	end
	xrandr_detect_done = true
	
	-- invoke xrandr to find out which fps rates are available on which outputs
	
	local p = {}
	p["cancellable"] = false
	p["args"] = {}
	p["args"][1] = "xrandr"
	p["args"][2] = "-q"
	local res = utils.subprocess(p)
	
	if (res["error"] ~= nil) then
		mp.msg.log("info", "failed to execute 'xrand -q', error message: " .. res["error"])
		return
	end
	
	mp.msg.log("v","xrandr -q\n" .. res["stdout"])

	local output_idx = 1
	for output in string.gmatch(res["stdout"], '\n([^ ]+) connected') do
		
		table.insert(xrandr_connected_outputs, output)
		
		-- the first line with a "*" after the match contains the rates associated with the current mode
		local mls = string.match(res["stdout"], "\n" .. string.gsub(output, "%p", "%%%1") .. " connected.*")
		local r
		local mode = nil
		local old_rate
		local old_mode
		
		-- old_rate = 0 means "no old rate known to switch to after playback"
		old_rate = 0
		
		if (xrandr_output_mode ~= nil) then		
			-- special case: user specified a certain preferred mode to use for playback
			mp.msg.log("v", "looking for refresh rates for user supplied output mode " .. xrandr_output_mode)
			mode, r = string.match(mls, '\n   (' .. xrandr_output_mode .. ') ([^\n]+)')
			
			if (mode == nil) then
				mp.msg.log("info", "user preferred output mode " .. xrandr_output_mode .. " not found for output " .. output .. " - will use current mode")
			else 
				mp.msg.log("info", "using user preferred xrandr_output_mode " .. xrandr_output_mode .. " for output " .. output)
				-- try to find the "old rate" for the other, currently active mode
				local oldr
				old_mode, oldr = string.match(mls, '\n   ([0-9x]+) ([^*\n]*%*[^\n]*)')
				if (oldr ~= nil) then
					for s in string.gmatch(oldr, "([^ ]+)%*") do
						old_rate = s
					end
				end
				mp.msg.log("v", "old_rate=" .. old_rate .. " found for old_mode=" .. tostring(old_mode))
			end
		end
		
		if (mode == nil) then
			-- normal case: use current mode
			mode, r = string.match(mls, '\n   ([0-9x]+) ([^*\n]*%*[^\n]*)')
			old_mode = mode
		end
		
		if (r == nil) then
			-- if no refresh rate is reported active for an output by xrandr,
			-- search for the mode that is "recommended" (marked by "+" in xrandr's output)
			mode, r = string.match(mls, '\n   ([0-9x]+) ([^+\n]*%+[^\n]*)')
			old_mode = mode
			if (r == nil) then 
				-- there is not even a "recommended" mode, so let's just use
				-- whatever first mode line there is
				mode, r = string.match(mls, '\n   ([0-9x]+) ([^+\n]*[^\n]*)')
			old_mode = mode
			end
		else
			-- so "r" contains a hint to the current ("old") rate, let's remember
			--  it for later switching back to it.
			for s in string.gmatch(r, "([^ ]+)%*") do
				old_rate = s
			end
		end
		mp.msg.log("info", "output " .. output .. " mode=" .. mode .. " old rate=" .. old_rate .. " refresh rates = " .. r)
		
		xrandr_modes[output] = { mode = mode, old_mode = old_mode, rates_s = r, rates = {}, old_rate = old_rate }
		local i = 1
		for s in string.gmatch(r, "([^ +*]+)") do
			
			-- check if rate "r" is black-listed - this is checked here because 
			if (not xrandr_check_blacklist(mode, 0.0 + s)) then
				xrandr_modes[output].rates[i] = 0.0 + s
				i = i+1
			end
		end
		
		output_idx = output_idx + 1
	end
	
end

function xrandr_find_best_fitting_rate(fps, output)
	
	local xrandr_rates = xrandr_modes[output].rates
	
  local best_fitting_rate = nil
  local best_fitting_ratio = math.huge
  
	-- try integer multipliers of 1 to 10 (given that high-fps displays exist these days)
	for m=1,10 do
		for i=1,#xrandr_rates do
			local r = xrandr_rates[i]
			local ratio = r / (m * fps)
      if (ratio < 1.0) then
        ratio = 1.0 / ratio
      end
      -- If the ratio is more than "very insignificantly off",
      -- then add a tiny additional score that will prefer faster
      -- over slower display frame rates, because those will cause
      -- shorter "stutters" when the display needs to skip or 
      -- duplicate one source frame.
      -- If the ratio is very close to 1.0, then we rather not
      -- choose the higher of the existing display rates, because
      -- displays performing frame interpolation work better when
      -- presented the actual, non-repeated source material frames.
      if (ratio > 1.0001) then
        ratio = ratio + (0.00000001 * (1000.0 - r))
      end
      -- mp.msg.log("info", "ratio " .. ratio .. " for r == " .. r)
      if (ratio < best_fitting_ratio) then
        best_fitting_ratio = ratio
        -- the xrand -q output may print nearby frequencies as the same
        -- rounded numbers - therefore, if our multiplier is == 1,
        -- we better return the video's frame rate, which xrandr
        -- is then likely to set the best rate for, even if the mode
        -- has some "odd" rate
        if (m == 1) then
          r = fps
        end
        best_fitting_rate = r
			end
		end		
	end
  
  return best_fitting_rate
end


xrandr_active_outputs = {}
function xrandr_set_active_outputs()
	local dn = mp.get_property("display-names")
	
	if (dn ~= nil) then
		mp.msg.log("v","display-names=" .. dn)
		xrandr_active_outputs = {}
		for w in (dn .. ","):gmatch("([^,]*),") do 
			table.insert(xrandr_active_outputs, w)
		end
	end
end

-- last detected non-nil video frame rate:
xrandr_cfps = nil

-- for each output, we remember which refresh rate we set last, so
-- we do not unnecessarily set the same refresh rate again
xrandr_previously_set = {}

function xrandr_set_rate()

	local f = mp.get_property_native("container-fps")
	if (f == nil or f == xrandr_cfps) then
		-- either no change or no frame rate information, so don't set anything
		return
	end
	xrandr_cfps = f

	xrandr_detect_available_rates()
	
	xrandr_set_active_outputs()
	
	local vdpau_hack = false
	local old_vid = nil
	local old_position = nil
	if (mp.get_property("options/vo") == "vdpau" or mp.get_property("options/hwdec") == "vdpau") then
		-- enable wild hack: need to close and re-open video for vdpau,
		-- because vdpau barfs if xrandr is run while it is in use
		
		vdpau_hack = true
		old_position = mp.get_property("time-pos")
		old_vid = mp.get_property("vid")
		mp.set_property("vid", "no")
	end

   -- unless "--script-opts=xrandr-ignore_unknown_oldrate=true" is set, 
	--  xrandr.lua will not touch display outputs for which it cannot
	--  get information on the current refresh rate for - assuming that
	--  such outputs are "disabled" somehow.
	local ignore_unknown_oldrate = mp.get_opt("xrandr-ignore_unknown_oldrate")
	if (ignore_unknown_oldrate == nil) then
		ignore_unknown_oldrate = false
	end

	
	local outs = {}
	if (#xrandr_active_outputs == 0) then
		-- No active outputs - probably because vo (like with vdpau) does
		-- not provide the information which outputs are covered.
		-- As a fall-back, let's assume all connected outputs are relevant.
		mp.msg.log("v","no output is known to be used by mpv, assuming all connected outputs are used.")
		outs = xrandr_connected_outputs
	else
		outs = xrandr_active_outputs
	end
		
	-- iterate over all relevant outputs used by mpv's output:
	for n, output in ipairs(outs) do
		
		if (ignore_unknown_oldrate == false and xrandr_modes[output].old_rate == 0) then
			mp.msg.log("info", "not touching output " .. output .. " because xrandr did not indicate a used refresh rate for it - use --script-opts=xrandr-ignore_unknown_oldrate=true if that is not what you want.")
		else
			local bfr = xrandr_find_best_fitting_rate(xrandr_cfps, output)

			if (bfr == 0.0) then
				mp.msg.log("info", "no non-blacklisted rate available, not invoking xrandr")
			else
				mp.msg.log("info", "container fps is " .. xrandr_cfps .. "Hz, for output " .. output .. " mode " .. xrandr_modes[output].mode .. " the best fitting display rate we will pass to xrandr is " .. bfr .. "Hz")

				if (bfr == xrandr_previously_set[output]) then
					mp.msg.log("v", "output " .. output .. " was already set to " .. bfr .. "Hz before - not changing")
				else 
					-- invoke xrandr to set the best fitting refresh rate for output 
					local p = {}
					p["cancellable"] = false
					p["args"] = {}
					p["args"][1] = "xrandr"
					p["args"][2] = "--output"
					p["args"][3] = output
					p["args"][4] = "--mode"
					p["args"][5] = xrandr_modes[output].mode
					p["args"][6] = "--rate"
					p["args"][7] = tostring(bfr)

					local cmd_as_string = ""
					for k, v in pairs(p["args"]) do
						cmd_as_string = cmd_as_string .. v .. " "
					end
					mp.msg.log("debug", "executing as subprocess: \"" .. cmd_as_string .. "\"")
					local res = utils.subprocess(p)

					if (res["error"] ~= nil) then
						mp.msg.log("error", "failed to set refresh rate for output " .. output .. " using xrandr, error message: " .. res["error"])
					else
						xrandr_previously_set[output] = bfr
					end
				end
			end
		end
	end
	
	if (vdpau_hack) then
		mp.set_property("vid", old_vid)
		if (old_position ~= nil) then
    		mp.commandv("seek", old_position, "absolute", "keyframes")
		else
    		mp.msg.log("v", "old_position is 'nil' - not seeking after vdpau re-initialization")
		end
	end
end


function xrandr_set_old_rate()
	
	local outs = {}
	if (#xrandr_active_outputs == 0) then
		-- No active outputs - probably because vo (like with vdpau) does
		-- not provide the information which outputs are covered.
		-- As a fall-back, let's assume all connected outputs are relevant.
		mp.msg.log("v","no output is known to be used by mpv, assuming all connected outputs are used.")
		outs = xrandr_connected_outputs
	else
		outs = xrandr_active_outputs
	end
		
	-- iterate over all relevant outputs used by mpv's output:
	for n, output in ipairs(outs) do
		
		local old_rate = xrandr_modes[output].old_rate
		
		if (old_rate == 0 or xrandr_previously_set[output] == nil ) then
			mp.msg.log("v", "no previous frame rate known for output " .. output .. " - so no switching back.")
		else

			if (math.abs(old_rate-xrandr_previously_set[output]) < 0.001) then
				mp.msg.log("v", "output " .. output .. " is already set to " .. old_rate .. "Hz - no switching back required")
			else 

				mp.msg.log("info", "switching output " .. output .. " that was set for replay to mode " .. xrandr_modes[output].mode .. " at " .. xrandr_previously_set[output] .. "Hz back to mode " .. xrandr_modes[output].old_mode .. " with refresh rate " .. old_rate .. "Hz")

				-- invoke xrandr to set the best fitting refresh rate for output 
				local p = {}
				p["cancellable"] = false
				p["args"] = {}
				p["args"][1] = "xrandr"
				p["args"][2] = "--output"
				p["args"][3] = output
				p["args"][4] = "--mode"
				p["args"][5] = xrandr_modes[output].old_mode
				p["args"][6] = "--rate"
				p["args"][7] = old_rate

				local res = utils.subprocess(p)

				if (res["error"] ~= nil) then
					mp.msg.log("error", "failed to set refresh rate for output " .. output .. " using xrandr, error message: " .. res["error"])
				else
					xrandr_previously_set[output] = old_rate
				end
			end
		end
		
	end
	
end

-- we'll consider setting refresh rates whenever the video fps or the active outputs change:
mp.observe_property("container-fps", "native", xrandr_set_rate)
mp.observe_property("display-names", "native", xrandr_set_rate)

-- and we'll try to revert the refresh rate when mpv is shut down
mp.register_event("shutdown", xrandr_set_old_rate)