Skip to content

Commit

Permalink
Remove circular reference between Graph and FilterContext
Browse files Browse the repository at this point in the history
  • Loading branch information
moonsikpark committed Jun 26, 2024
1 parent ff65d9e commit c890636
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion av/filter/context.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ from av.filter.graph cimport Graph
cdef class FilterContext:

cdef lib.AVFilterContext *ptr
cdef readonly Graph graph
cdef readonly object _graph
cdef readonly Filter filter

cdef object _inputs
Expand Down
3 changes: 3 additions & 0 deletions av/filter/context.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from av.filter import Graph
from av.frame import Frame

from .pad import FilterContextPad
Expand All @@ -11,5 +12,7 @@ class FilterContext:
def link_to(
self, input_: FilterContext, output_idx: int = 0, input_idx: int = 0
) -> None: ...
@property
def graph(self) -> Graph: ...
def push(self, frame: Frame) -> None: ...
def pull(self) -> Frame: ...
11 changes: 10 additions & 1 deletion av/filter/context.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import weakref

from av.audio.frame cimport alloc_audio_frame
from av.dictionary cimport _Dictionary
from av.dictionary import Dictionary
Expand All @@ -13,7 +15,7 @@ cdef object _cinit_sentinel = object()

cdef FilterContext wrap_filter_context(Graph graph, Filter filter, lib.AVFilterContext *ptr):
cdef FilterContext self = FilterContext(_cinit_sentinel)
self.graph = graph
self._graph = weakref.ref(graph)
self.filter = filter
self.ptr = ptr
return self
Expand Down Expand Up @@ -72,6 +74,13 @@ cdef class FilterContext:

def link_to(self, FilterContext input_, int output_idx=0, int input_idx=0):
err_check(lib.avfilter_link(self.ptr, output_idx, input_.ptr, input_idx))

@property
def graph(self):
if (graph := self._graph()):
return graph
else:
raise RuntimeError("graph is unallocated")

def push(self, Frame frame):
cdef int res
Expand Down
2 changes: 2 additions & 0 deletions av/filter/graph.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ from av.filter.context cimport FilterContext


cdef class Graph:
cdef object __weakref__

cdef lib.AVFilterGraph *ptr

cdef readonly bint configured
Expand Down

0 comments on commit c890636

Please sign in to comment.