1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-02 21:12:23 +00:00

TOOLS: make autodeint detect telecine in parallel

This avoids having to rewind the video for a second telecine detection pass.
This commit is contained in:
Kevin Mitchell 2015-07-16 16:12:58 -07:00
parent 968bd3df3b
commit ebdbb4f24c

View File

@ -2,21 +2,16 @@
-- appropriate deinterlacing filter based on a short section of the -- appropriate deinterlacing filter based on a short section of the
-- currently playing video. -- currently playing video.
-- --
-- It registers the key-binding ctrl+d, which when pressed, inserts -- It registers the key-binding ctrl+d, which when pressed, inserts the filters
-- the filter vf=lavfi=idet. After 4 second, it examines the results -- ``vf=lavfi=idet,pullup,vf=lavfi=idet``. After 4 seconds, it removes these
-- to determine whether the content progressive or interlaced and the -- filters and decides whether the content is progressive, interlaced, or
-- interlacing field dominance. It immediately sets the latter within -- telecined and the interlacing field dominance.
-- mpv.
-- --
-- If the content is judged not to be progressive, it may be either -- Based on this information, it may set mpv's ``deinterlace`` property (which
-- interlaced or telecined. To determine which one, the video is -- usually inserts the yadif filter), or insert the ``pullup`` filter if the
-- seeked back to the detection starting point with the "pullup" -- content is telecined. It also sets mpv's ``field-dominance`` property.
-- filter inserted. After another four seconds, if idet thinks that
-- the pulled up viedo is "progressive", then pullup is the correct
-- filter and this script leaves it in places while removing the idet
-- filter. Otherwise, the content plain interlaced and mpv's deinterlace
-- property is set. This will usually insert the yadif deinterlacing filter.
-- --
-- OPTIONS:
-- The default detection time may be overridden by adding -- The default detection time may be overridden by adding
-- --
-- --script-opts=autodeint.detect_seconds=<number of seconds> -- --script-opts=autodeint.detect_seconds=<number of seconds>
@ -37,6 +32,7 @@ require "mp.msg"
script_name = mp.get_script_name() script_name = mp.get_script_name()
detect_label = string.format("%s-detect", script_name) detect_label = string.format("%s-detect", script_name)
pullup_label = string.format("%s", script_name) pullup_label = string.format("%s", script_name)
ivtc_detect_label = string.format("%s-ivtc-detect", script_name)
-- number of seconds to gather cropdetect data -- number of seconds to gather cropdetect data
detect_seconds = tonumber(mp.get_opt(string.format("%s.detect_seconds", script_name))) detect_seconds = tonumber(mp.get_opt(string.format("%s.detect_seconds", script_name)))
@ -69,30 +65,29 @@ function start_detect()
mp.set_property("deinterlace","no") mp.set_property("deinterlace","no")
del_filter_if_present(pullup_label) del_filter_if_present(pullup_label)
percent_pos = mp.get_property("percent-pos")
-- insert the detection filter -- insert the detection filter
local cmd = string.format('vf add @%s:lavfi=graph="idet"', detect_label) local cmd = string.format('vf add @%s:lavfi=graph="idet",@%s:pullup,@%s:lavfi=graph="idet"',
detect_label, pullup_label, ivtc_detect_label)
if not mp.command(cmd) then if not mp.command(cmd) then
mp.msg.error("failed to insert detection filter") mp.msg.error("failed to insert detection filters")
return return
end end
-- wait to gather data -- wait to gather data
timer = mp.add_timeout(detect_seconds, judge_field_dominance) timer = mp.add_timeout(detect_seconds, select_filter)
end end
function stop_detect() function stop_detect()
del_filter_if_present(detect_label) del_filter_if_present(detect_label)
del_filter_if_present(ivtc_detect_label)
timer = nil timer = nil
mp.set_property("playback-pos", percent_pos)
end end
progressive, interlaced_tff, interlaced_bff, interlaced = 0, 1, 2, 3, 4 progressive, interlaced_tff, interlaced_bff, interlaced = 0, 1, 2, 3, 4
function judge() function judge(label)
-- get the metadata -- get the metadata
local result = mp.get_property_native(string.format("vf-metadata/%s", detect_label)) local result = mp.get_property_native(string.format("vf-metadata/%s", label))
num_tff = tonumber(result["lavfi.idet.multiple.tff"]) num_tff = tonumber(result["lavfi.idet.multiple.tff"])
num_bff = tonumber(result["lavfi.idet.multiple.bff"]) num_bff = tonumber(result["lavfi.idet.multiple.bff"])
num_progressive = tonumber(result["lavfi.idet.multiple.progressive"]) num_progressive = tonumber(result["lavfi.idet.multiple.progressive"])
@ -100,10 +95,10 @@ function judge()
num_interlaced = num_tff + num_bff num_interlaced = num_tff + num_bff
num_determined = num_interlaced + num_progressive num_determined = num_interlaced + num_progressive
mp.msg.verbose("progressive = "..num_progressive) mp.msg.verbose(label.." progressive = "..num_progressive)
mp.msg.verbose("interlaced-tff = "..num_tff) mp.msg.verbose(label.." interlaced-tff = "..num_tff)
mp.msg.verbose("interlaced-bff = "..num_bff) mp.msg.verbose(label.." interlaced-bff = "..num_bff)
mp.msg.verbose("undetermined = "..num_undetermined) mp.msg.verbose(label.." undetermined = "..num_undetermined)
if num_determined < num_undetermined then if num_determined < num_undetermined then
mp.msg.warn("majority undetermined frames") mp.msg.warn("majority undetermined frames")
@ -119,11 +114,12 @@ function judge()
end end
end end
function judge_field_dominance() function select_filter()
verdict = judge() -- handle the first detection filter results
verdict = judge(detect_label)
if verdict == progressive then if verdict == progressive then
stop_detect()
mp.msg.info("progressive: doing nothing") mp.msg.info("progressive: doing nothing")
stop_detect()
return return
elseif verdict == interlaced_tff then elseif verdict == interlaced_tff then
mp.set_property("field-dominance", "top") mp.set_property("field-dominance", "top")
@ -133,21 +129,8 @@ function judge_field_dominance()
mp.set_property("field-dominance", "auto") mp.set_property("field-dominance", "auto")
end end
local cmd = string.format("vf pre @%s:pullup", pullup_label) -- handle the ivtc detection filter results
if not mp.command(cmd) then verdict = judge(ivtc_detect_label)
mp.msg.error("failed to insert pullup filter")
stop_detect()
return
end
-- redo the detection with the pullup filter to see if it's telecined
mp.set_property("percent-pos", percent_pos)
timer = mp.add_timeout(detect_seconds, judge_ivtc)
end
function judge_ivtc()
verdict = judge()
if verdict == progressive then if verdict == progressive then
mp.msg.info(string.format("telecinied with %s field dominance: using pullup", mp.get_property("field-dominance"))) mp.msg.info(string.format("telecinied with %s field dominance: using pullup", mp.get_property("field-dominance")))
stop_detect() stop_detect()
@ -157,8 +140,6 @@ function judge_ivtc()
mp.set_property("deinterlace","yes") mp.set_property("deinterlace","yes")
stop_detect() stop_detect()
end end
return
end end
mp.add_key_binding("ctrl+d", script_name, start_detect) mp.add_key_binding("ctrl+d", script_name, start_detect)