diff --git a/av/filter/context.pxd b/av/filter/context.pxd index 18954fbd..ae9f27c9 100644 --- a/av/filter/context.pxd +++ b/av/filter/context.pxd @@ -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 diff --git a/av/filter/context.pyi b/av/filter/context.pyi index 0d70c009..7c00087a 100644 --- a/av/filter/context.pyi +++ b/av/filter/context.pyi @@ -1,3 +1,4 @@ +from av.filter import Graph from av.frame import Frame from .pad import FilterContextPad @@ -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: ... diff --git a/av/filter/context.pyx b/av/filter/context.pyx index 54ed710a..b820d3d1 100644 --- a/av/filter/context.pyx +++ b/av/filter/context.pyx @@ -1,3 +1,5 @@ +import weakref + from av.audio.frame cimport alloc_audio_frame from av.dictionary cimport _Dictionary from av.dictionary import Dictionary @@ -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 @@ -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 diff --git a/av/filter/graph.pxd b/av/filter/graph.pxd index b3bf352a..2e52bd6e 100644 --- a/av/filter/graph.pxd +++ b/av/filter/graph.pxd @@ -4,6 +4,8 @@ from av.filter.context cimport FilterContext cdef class Graph: + cdef object __weakref__ + cdef lib.AVFilterGraph *ptr cdef readonly bint configured