From c8906362b4a64fa59a5a31d170970d9ad9a81a6d Mon Sep 17 00:00:00 2001 From: Moonsik Park Date: Wed, 26 Jun 2024 11:45:11 +0900 Subject: [PATCH] Remove circular reference between `Graph` and `FilterContext` --- av/filter/context.pxd | 2 +- av/filter/context.pyi | 3 +++ av/filter/context.pyx | 11 ++++++++++- av/filter/graph.pxd | 2 ++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/av/filter/context.pxd b/av/filter/context.pxd index 18954fbdd..ae9f27c99 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 0d70c0095..7c00087a9 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 54ed710ab..b820d3d18 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 b3bf352a3..2e52bd6ec 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