diff --git a/av/filter/link.pxd b/av/filter/link.pxd index 08771362b..7f6aad2aa 100644 --- a/av/filter/link.pxd +++ b/av/filter/link.pxd @@ -7,7 +7,7 @@ from av.filter.link cimport FilterContextPad, FilterLink cdef class FilterLink: - cdef readonly Graph graph + cdef object _graph cdef lib.AVFilterLink *ptr cdef FilterContextPad _input diff --git a/av/filter/link.py b/av/filter/link.py index 09a078d5a..0e5b680ce 100644 --- a/av/filter/link.py +++ b/av/filter/link.py @@ -1,3 +1,5 @@ +import weakref + import cython import cython.cimports.libav as lib from cython.cimports.av.filter.graph import Graph @@ -12,6 +14,13 @@ def __cinit__(self, sentinel): if sentinel is not _cinit_sentinel: raise RuntimeError("cannot instantiate FilterLink") + @property + def graph(self) -> Graph: + if g := self._graph(): + return g + else: + raise RuntimeError("graph is unallocated") + @property def input(self): if self._input: @@ -23,7 +32,8 @@ def input(self): break else: # nobreak raise RuntimeError("could not find link in context") - ctx = self.graph._context_by_ptr[cython.cast(cython.long, cctx)] + graph: Graph = self.graph + ctx = graph._context_by_ptr[cython.cast(cython.long, cctx)] self._input = ctx.outputs[i] return self._input @@ -39,7 +49,8 @@ def output(self): else: raise RuntimeError("could not find link in context") try: - ctx = self.graph._context_by_ptr[cython.cast(cython.long, cctx)] + graph: Graph = self.graph + ctx = graph._context_by_ptr[cython.cast(cython.long, cctx)] except KeyError: raise RuntimeError( "could not find context in graph", (cctx.name, cctx.filter.name) @@ -51,7 +62,7 @@ def output(self): @cython.cfunc def wrap_filter_link(graph: Graph, ptr: cython.pointer[lib.AVFilterLink]) -> FilterLink: link: FilterLink = FilterLink(_cinit_sentinel) - link.graph = graph + link._graph = weakref.ref(graph) link.ptr = ptr return link