Skip to content

Commit

Permalink
Make sure input clock ticks at least once per output clock tick to cl…
Browse files Browse the repository at this point in the history
…ear buffers (#3605)
  • Loading branch information
toots committed Dec 23, 2023
1 parent eaf104b commit c025294
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
34 changes: 32 additions & 2 deletions src/core/builtins/builtins_ffmpeg_filters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type outputs =
type graph = {
mutable config : Avfilter.config option;
mutable failed : bool;
mutable need_child_tick : bool;
input_inits : (unit -> bool) Queue.t;
graph_inputs : Source.source Queue.t;
graph_outputs : Source.source Queue.t;
Expand All @@ -81,7 +82,26 @@ type graph = {

let init_graph graph =
if Queue.fold (fun b v -> b && v ()) true graph.input_inits then (
try Queue.iter Lazy.force graph.init
try
Queue.iter Lazy.force graph.init;

match
(Queue.peek_opt graph.graph_inputs, Queue.peek_opt graph.graph_outputs)
with
| Some i, Some o ->
(* Make sure input clock has at least one tick per output clock
tick. *)
let input_clock = Source.Clock_variables.get i#clock in
let output_clock = Source.Clock_variables.get o#clock in
let rec on_before_output () =
graph.need_child_tick <- true;
output_clock#on_after_output on_after_output
and on_after_output () =
if graph.need_child_tick then input_clock#end_tick;
output_clock#on_before_output on_before_output
in
output_clock#on_before_output on_before_output
| _ -> ()
with exn ->
let bt = Printexc.get_raw_backtrace () in
graph.failed <- true;
Expand All @@ -91,12 +111,20 @@ let initialized graph =
Queue.fold (fun cur q -> cur && Lazy.is_val q) true graph.init

let is_ready graph =
(match (initialized graph, Queue.peek_opt graph.graph_inputs) with
| false, Some s -> (Source.Clock_variables.get s#clock)#end_tick
| _ -> ());
(not graph.failed)
&& Queue.fold
(fun cur (s : Source.source) -> cur && s#is_ready ())
true graph.graph_inputs

let pull graph = (Clock.get (Queue.peek graph.graph_inputs)#clock)#end_tick
let pull graph =
match Queue.peek_opt graph.graph_inputs with
| Some s ->
graph.need_child_tick <- false;
(Clock.get s#clock)#end_tick
| None -> ()

let self_sync_type graph =
Lazy.from_fun (fun () ->
Expand Down Expand Up @@ -865,6 +893,7 @@ let _ =
{
config = Some config;
failed = false;
need_child_tick = true;
input_inits = Queue.create ();
graph_inputs = Queue.create ();
graph_outputs = Queue.create ();
Expand All @@ -888,6 +917,7 @@ let _ =
Clock.create_unknown ~sources:[] ~sub_clocks:[input_clock] ()
in
unify_clocks ~clock:output_clock graph.graph_outputs;

Queue.add
(lazy
(log#info "Initializing graph";
Expand Down
2 changes: 1 addition & 1 deletion tests/media/ffmpeg_complex_filter.liq
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ audio = filter(source.tracks(s).audio, source.tracks(s').audio)
s = source({audio=audio, metadata=track.metadata(audio)})
s.on_metadata(fun (m) -> if m["tag"] == "test" then test.pass() end)

#clock.assign_new(sync="none", [s])
clock.assign_new(sync="none", [s])
enc = %ffmpeg(%audio.raw(codec = "aac"))
filename = file.temp("tmp", ".aac")
on_cleanup({file.remove(filename)})
Expand Down
3 changes: 2 additions & 1 deletion tests/media/ffmpeg_filter.liq
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ s.on_track(on_track)

done = ref(false)

#clock.assign_new(id='test_clock', sync='none', [s])
clock.assign_new(id='test_clock', sync='none', [s])

def on_close(filename) =
if
!started and not done()
Expand Down
3 changes: 2 additions & 1 deletion tests/media/ffmpeg_transparency_filter.liq
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ s.on_track(on_track)

done = ref(false)

#clock.assign_new(id='test_clock', sync='none', [s])
clock.assign_new(id='test_clock', sync='none', [s])

def on_close(filename) =
if
not done() and !started
Expand Down

0 comments on commit c025294

Please sign in to comment.