History

TOOLS: make autodeint detect telecine in parallel

This avoids having to rewind the video for a second telecine detection pass.

Kevin Mitchell committed 11 years ago

Commit ebdbb4f1 parent 968bd3d1 file · +26 −45Browse files

Showing the changes to TOOLS/lua/autodeint.lua (1 of 1 file). Show all

Unified · Split

TOOLS/lua/autodeint.lua+26−45
@@ -2,21 +2,16 @@ · 1 unmodified line
2-- appropriate deinterlacing filter based on a short section of the2-- appropriate deinterlacing filter based on a short section of the
3-- currently playing video.3-- currently playing video.
4--4--
5-- It registers the key-binding ctrl+d, which when pressed, inserts5-- It registers the key-binding ctrl+d, which when pressed, inserts the filters
6-- the filter vf=lavfi=idet. After 4 second, it examines the results6-- ``vf=lavfi=idet,pullup,vf=lavfi=idet``. After 4 seconds, it removes these
7-- to determine whether the content progressive or interlaced and the7-- filters and decides whether the content is progressive, interlaced, or
8-- interlacing field dominance. It immediately sets the latter within8-- telecined and the interlacing field dominance.
9-- mpv.
10--9--
11-- If the content is judged not to be progressive, it may be either10-- Based on this information, it may set mpv's ``deinterlace`` property (which
12-- interlaced or telecined. To determine which one, the video is11-- usually inserts the yadif filter), or insert the ``pullup`` filter if the
13-- seeked back to the detection starting point with the "pullup"12-- content is telecined. It also sets mpv's ``field-dominance`` property.
14-- filter inserted. After another four seconds, if idet thinks that
15-- the pulled up viedo is "progressive", then pullup is the correct
16-- filter and this script leaves it in places while removing the idet
17-- filter. Otherwise, the content plain interlaced and mpv's deinterlace
18-- property is set. This will usually insert the yadif deinterlacing filter.
19--13--
14-- OPTIONS:
20-- The default detection time may be overridden by adding15-- The default detection time may be overridden by adding
21--16--
22-- --script-opts=autodeint.detect_seconds=<number of seconds>17-- --script-opts=autodeint.detect_seconds=<number of seconds>
@@ -37,6 +32,7 @@ require "mp.msg" · 14 unmodified lines
37script_name = mp.get_script_name()32script_name = mp.get_script_name()
38detect_label = string.format("%s-detect", script_name)33detect_label = string.format("%s-detect", script_name)
39pullup_label = string.format("%s", script_name)34pullup_label = string.format("%s", script_name)
35ivtc_detect_label = string.format("%s-ivtc-detect", script_name)
40 36
41-- number of seconds to gather cropdetect data37-- number of seconds to gather cropdetect data
42detect_seconds = tonumber(mp.get_opt(string.format("%s.detect_seconds", script_name)))38detect_seconds = tonumber(mp.get_opt(string.format("%s.detect_seconds", script_name)))
@@ -69,30 +65,29 @@ function start_detect() · 26 unmodified lines
69 mp.set_property("deinterlace","no")65 mp.set_property("deinterlace","no")
70 del_filter_if_present(pullup_label)66 del_filter_if_present(pullup_label)
71 67
72 percent_pos = mp.get_property("percent-pos")
73
74 -- insert the detection filter68 -- insert the detection filter
75 local cmd = string.format('vf add @%s:lavfi=graph="idet"', detect_label)69 local cmd = string.format('vf add @%s:lavfi=graph="idet",@%s:pullup,@%s:lavfi=graph="idet"',
70 detect_label, pullup_label, ivtc_detect_label)
76 if not mp.command(cmd) then71 if not mp.command(cmd) then
77 mp.msg.error("failed to insert detection filter")72 mp.msg.error("failed to insert detection filters")
78 return73 return
79 end74 end
80 75
81 -- wait to gather data76 -- wait to gather data
82 timer = mp.add_timeout(detect_seconds, judge_field_dominance)77 timer = mp.add_timeout(detect_seconds, select_filter)
83end78end
84 79
85function stop_detect()80function stop_detect()
86 del_filter_if_present(detect_label)81 del_filter_if_present(detect_label)
82 del_filter_if_present(ivtc_detect_label)
87 timer = nil83 timer = nil
88 mp.set_property("playback-pos", percent_pos)
89end84end
90 85
91progressive, interlaced_tff, interlaced_bff, interlaced = 0, 1, 2, 3, 486progressive, interlaced_tff, interlaced_bff, interlaced = 0, 1, 2, 3, 4
92 87
93function judge()88function judge(label)
94 -- get the metadata89 -- get the metadata
95 local result = mp.get_property_native(string.format("vf-metadata/%s", detect_label))90 local result = mp.get_property_native(string.format("vf-metadata/%s", label))
96 num_tff = tonumber(result["lavfi.idet.multiple.tff"])91 num_tff = tonumber(result["lavfi.idet.multiple.tff"])
97 num_bff = tonumber(result["lavfi.idet.multiple.bff"])92 num_bff = tonumber(result["lavfi.idet.multiple.bff"])
98 num_progressive = tonumber(result["lavfi.idet.multiple.progressive"])93 num_progressive = tonumber(result["lavfi.idet.multiple.progressive"])
@@ -100,10 +95,10 @@ function judge() · 1 unmodified line
100 num_interlaced = num_tff + num_bff95 num_interlaced = num_tff + num_bff
101 num_determined = num_interlaced + num_progressive96 num_determined = num_interlaced + num_progressive
102 97
103 mp.msg.verbose("progressive = "..num_progressive)98 mp.msg.verbose(label.." progressive = "..num_progressive)
104 mp.msg.verbose("interlaced-tff = "..num_tff)99 mp.msg.verbose(label.." interlaced-tff = "..num_tff)
105 mp.msg.verbose("interlaced-bff = "..num_bff)100 mp.msg.verbose(label.." interlaced-bff = "..num_bff)
106 mp.msg.verbose("undetermined = "..num_undetermined)101 mp.msg.verbose(label.." undetermined = "..num_undetermined)
107 102
108 if num_determined < num_undetermined then103 if num_determined < num_undetermined then
109 mp.msg.warn("majority undetermined frames")104 mp.msg.warn("majority undetermined frames")
@@ -119,11 +114,12 @@ function judge() · 9 unmodified lines
119 end114 end
120end115end
121 116
122function judge_field_dominance()117function select_filter()
123 verdict = judge()118 -- handle the first detection filter results
119 verdict = judge(detect_label)
124 if verdict == progressive then120 if verdict == progressive then
125 stop_detect()
126 mp.msg.info("progressive: doing nothing")121 mp.msg.info("progressive: doing nothing")
122 stop_detect()
127 return123 return
128 elseif verdict == interlaced_tff then124 elseif verdict == interlaced_tff then
129 mp.set_property("field-dominance", "top")125 mp.set_property("field-dominance", "top")
@@ -133,21 +129,8 @@ function judge_field_dominance() · 3 unmodified lines
133 mp.set_property("field-dominance", "auto")129 mp.set_property("field-dominance", "auto")
134 end130 end
135 131
136 local cmd = string.format("vf pre @%s:pullup", pullup_label)132 -- handle the ivtc detection filter results
137 if not mp.command(cmd) then133 verdict = judge(ivtc_detect_label)
138 mp.msg.error("failed to insert pullup filter")
139 stop_detect()
140 return
141 end
142
143 -- redo the detection with the pullup filter to see if it's telecined
144 mp.set_property("percent-pos", percent_pos)
145 timer = mp.add_timeout(detect_seconds, judge_ivtc)
146end
147
148function judge_ivtc()
149 verdict = judge()
150
151 if verdict == progressive then134 if verdict == progressive then
152 mp.msg.info(string.format("telecinied with %s field dominance: using pullup", mp.get_property("field-dominance")))135 mp.msg.info(string.format("telecinied with %s field dominance: using pullup", mp.get_property("field-dominance")))
153 stop_detect()136 stop_detect()
@@ -157,8 +140,6 @@ function judge_ivtc() · 3 unmodified lines
157 mp.set_property("deinterlace","yes")140 mp.set_property("deinterlace","yes")
158 stop_detect()141 stop_detect()
159 end142 end
160
161 return
162end143end
163 144
164mp.add_key_binding("ctrl+d", script_name, start_detect)145mp.add_key_binding("ctrl+d", script_name, start_detect)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t