Skip to content

Commit 9636aac

Browse files
committed
Make VideoCodecContext 12 bytes smaller
1 parent 4afc506 commit 9636aac

5 files changed

Lines changed: 8 additions & 19 deletions

File tree

av/codec/context.pxd

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cimport libav as lib
2-
from libc.stdint cimport int64_t
2+
from libc.stdint cimport int64_t, uint8_t
33

44
from av.buffer cimport ByteSource
55
from av.codec.codec cimport Codec
@@ -11,13 +11,6 @@ from av.packet cimport Packet
1111
cdef class CodecContext:
1212
cdef lib.AVCodecContext *ptr
1313

14-
# Whether AVCodecContext.extradata should be de-allocated upon destruction.
15-
cdef bint extradata_set
16-
17-
# True when created via add_stream_from_template(); start_encoding() skips
18-
# avcodec_open2() and lets encode()/decode() open the codec lazily if needed.
19-
cdef readonly bint _template_initialized
20-
2114
# Used as a signal that this is within a stream, and also for us to access that
2215
# stream. This is set "manually" by the stream after constructing this object.
2316
cdef int stream_index
@@ -40,6 +33,10 @@ cdef class CodecContext:
4033
# Used by hardware-accelerated decode.
4134
cdef HWAccel hwaccel_ctx
4235

36+
cdef uint8_t _ctxflags # ctxEnum: template_initialized
37+
# True when created via add_stream_from_template(); start_encoding() skips
38+
# avcodec_open2() and lets encode()/decode() open the codec lazily if needed.
39+
4340
# Used by both transcode APIs to setup user-land objects.
4441
# TODO: Remove the `Packet` from `_setup_decoded_frame` (because flushing packets
4542
# are bogus). It should take all info it needs from the context and/or stream.

av/codec/context.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ def extradata(self, data):
206206
raise MemoryError("Cannot allocate extradata")
207207
memcpy(self.ptr.extradata, source.ptr, source.length)
208208
self.ptr.extradata_size = source.length
209-
self.extradata_set = True
210209

211210
@property
212211
def extradata_size(self):
@@ -253,9 +252,8 @@ def open(self, strict: cython.bint = True):
253252
self.options = dict(options)
254253

255254
def __dealloc__(self):
256-
if self.ptr and self.extradata_set:
257-
lib.av_freep(cython.address(self.ptr.extradata))
258255
if self.ptr:
256+
lib.av_freep(cython.address(self.ptr.extradata))
259257
lib.avcodec_free_context(cython.address(self.ptr))
260258
if self.parser:
261259
lib.av_parser_close(self.parser)

av/container/output.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def add_stream_from_template(
275275

276276
# Construct the user-land stream
277277
py_codec_context: CodecContext = wrap_codec_context(ctx, codec, None)
278-
py_codec_context._template_initialized = True
278+
py_codec_context._ctxflags |= 1 # _template_initialized = True
279279
py_stream: Stream = wrap_stream(self, stream, py_codec_context)
280280
self.streams.add_stream(py_stream)
281281

@@ -446,7 +446,7 @@ def start_encoding(self):
446446
for k, v in self.options.items():
447447
ctx.options.setdefault(k, v)
448448

449-
if not ctx._template_initialized:
449+
if not (ctx._ctxflags & 1): # template_initialized
450450
ctx.open()
451451

452452
# Track option consumption.

av/video/codeccontext.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ cdef class VideoCodecContext(CodecContext):
1919
cdef AVCodecPrivateData _private_data
2020
cdef VideoFormat _format
2121
cdef _build_format(self)
22-
cdef int last_w
23-
cdef int last_h
2422
cdef readonly VideoReformatter reformatter
2523
cdef readonly int encoded_frame_count
2624
cdef VideoFrame next_frame

av/video/codeccontext.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ def _get_hw_format(
3939

4040
@cython.cclass
4141
class VideoCodecContext(CodecContext):
42-
def __cinit__(self, *args, **kwargs):
43-
self.last_w = 0
44-
self.last_h = 0
45-
4642
@cython.cfunc
4743
def _init(
4844
self,

0 commit comments

Comments
 (0)