@@ -130,6 +130,7 @@ cdef class CodecContext:
130130
131131 self .options = {}
132132 self .stream_index = - 1 # This is set by the container immediately.
133+ self ._is_open = False
133134
134135 cdef _init(self , lib.AVCodecContext * ptr, const lib.AVCodec * codec):
135136 self .ptr = ptr
@@ -217,7 +218,7 @@ cdef class CodecContext:
217218
218219 @property
219220 def is_open (self ):
220- return lib.avcodec_is_open( self .ptr)
221+ return self ._is_open
221222
222223 @property
223224 def is_encoder (self ):
@@ -228,7 +229,7 @@ cdef class CodecContext:
228229 return lib.av_codec_is_decoder(self .ptr.codec)
229230
230231 cpdef open (self , bint strict = True ):
231- if lib.avcodec_is_open( self .ptr) :
232+ if self ._is_open :
232233 if strict:
233234 raise ValueError (" CodecContext is already open." )
234235 return
@@ -241,25 +242,25 @@ cdef class CodecContext:
241242 self ._set_default_time_base()
242243
243244 err_check(lib.avcodec_open2(self .ptr, self .codec.ptr, & options.ptr))
244-
245+ self ._is_open = True
245246 self .options = dict (options)
246247
247248 cdef _set_default_time_base(self ):
248249 self .ptr.time_base.num = 1
249250 self .ptr.time_base.den = lib.AV_TIME_BASE
250251
251252 cpdef close(self , bint strict = True ):
252- if not lib.avcodec_is_open( self .ptr) :
253+ if not self ._is_open :
253254 if strict:
254255 raise ValueError (" CodecContext is already closed." )
255256 return
256- err_check(lib.avcodec_close(self .ptr))
257+ self ._is_open = False
258+ lib.avcodec_free_context(& self .ptr)
257259
258260 def __dealloc__ (self ):
259261 if self .ptr and self .extradata_set:
260262 lib.av_freep(& self .ptr.extradata)
261263 if self .ptr:
262- lib.avcodec_close(self .ptr)
263264 lib.avcodec_free_context(& self .ptr)
264265 if self .parser:
265266 lib.av_parser_close(self .parser)
@@ -565,7 +566,7 @@ cdef class CodecContext:
565566
566567 @thread_count.setter
567568 def thread_count (self , int value ):
568- if lib.avcodec_is_open( self .ptr) :
569+ if self ._is_open :
569570 raise RuntimeError (" Cannot change thread_count after codec is open." )
570571 self .ptr.thread_count = value
571572
@@ -580,7 +581,7 @@ cdef class CodecContext:
580581
581582 @thread_type.setter
582583 def thread_type (self , value ):
583- if lib.avcodec_is_open( self .ptr) :
584+ if self ._is_open :
584585 raise RuntimeError (" Cannot change thread_type after codec is open." )
585586 self .ptr.thread_type = ThreadType[value].value
586587
0 commit comments