Skip to content

Commit 89d131c

Browse files
committed
Set timebases in open
1 parent 2dfa0c0 commit 89d131c

File tree

5 files changed

+21
-33
lines changed

5 files changed

+21
-33
lines changed

av/audio/codeccontext.pyx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@ cdef class AudioCodecContext(CodecContext):
1111
cdef _init(self, lib.AVCodecContext *ptr, const lib.AVCodec *codec):
1212
CodecContext._init(self, ptr, codec)
1313

14-
cdef _set_default_time_base(self):
15-
self.ptr.time_base.num = 1
16-
self.ptr.time_base.den = self.ptr.sample_rate
17-
1814
cdef _prepare_frames_for_encode(self, Frame input_frame):
1915

2016
cdef AudioFrame frame = input_frame
21-
2217
cdef bint allow_var_frame_size = self.ptr.codec.capabilities & lib.AV_CODEC_CAP_VARIABLE_FRAME_SIZE
2318

2419
# Note that the resampler will simply return an input frame if there is

av/codec/context.pxd

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ from av.packet cimport Packet
88

99

1010
cdef class CodecContext:
11-
1211
cdef lib.AVCodecContext *ptr
1312

1413
# Whether AVCodecContext.extradata should be de-allocated upon destruction.
1514
cdef bint extradata_set
16-
cdef bint _is_open
1715

1816
# Used as a signal that this is within a stream, and also for us to access
1917
# that stream. This is set "manually" by the stream after constructing
@@ -30,8 +28,7 @@ cdef class CodecContext:
3028

3129
# Public API.
3230
cpdef open(self, bint strict=?)
33-
34-
cdef _set_default_time_base(self)
31+
cdef readonly bint is_open
3532

3633
# Wraps both versions of the transcode API, returning lists.
3734
cpdef encode(self, Frame frame=?)

av/codec/context.pyi

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ class Flags2(EnumFlag):
5757
class CodecContext:
5858
extradata: bytes | None
5959
extradata_size: int
60-
is_open: bool
61-
is_encoder: bool
62-
is_decoder: bool
6360
name: str
6461
codec: Codec
6562
options: dict[str, str]
@@ -98,6 +95,12 @@ class CodecContext:
9895
closed_gop: bool
9996
delay: bool
10097

98+
@property
99+
def is_open(self) -> bool: ...
100+
@property
101+
def is_encoder(self) -> bool: ...
102+
@property
103+
def is_decoder(self) -> bool: ...
101104
def open(self, strict: bool = True) -> None: ...
102105
@staticmethod
103106
def create(

av/codec/context.pyx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ Flags2 = define_enum("Flags2", __name__, (
117117

118118

119119
cdef class CodecContext:
120-
121120
@staticmethod
122121
def create(codec, mode=None):
123122
cdef Codec cy_codec = codec if isinstance(codec, Codec) else Codec(codec, mode)
@@ -130,7 +129,7 @@ cdef class CodecContext:
130129

131130
self.options = {}
132131
self.stream_index = -1 # This is set by the container immediately.
133-
self._is_open = False
132+
self.is_open = False
134133

135134
cdef _init(self, lib.AVCodecContext *ptr, const lib.AVCodec *codec):
136135
self.ptr = ptr
@@ -217,10 +216,6 @@ cdef class CodecContext:
217216
def extradata_size(self):
218217
return self.ptr.extradata_size
219218

220-
@property
221-
def is_open(self):
222-
return self._is_open
223-
224219
@property
225220
def is_encoder(self):
226221
if self.ptr is NULL:
@@ -234,27 +229,29 @@ cdef class CodecContext:
234229
return lib.av_codec_is_decoder(self.ptr.codec)
235230

236231
cpdef open(self, bint strict=True):
237-
if self._is_open:
232+
if self.is_open:
238233
if strict:
239234
raise ValueError("CodecContext is already open.")
240235
return
241236

242237
cdef _Dictionary options = Dictionary()
243238
options.update(self.options or {})
244239

245-
# Assert we have a time_base for encoders.
246240
if not self.ptr.time_base.num and self.is_encoder:
247-
self._set_default_time_base()
241+
if self.type == "video":
242+
self.ptr.time_base.num = self.ptr.framerate.den or 1
243+
self.ptr.time_base.den = self.ptr.framerate.num or lib.AV_TIME_BASE
244+
elif self.type == "audio":
245+
self.ptr.time_base.num = 1
246+
self.ptr.time_base.den = self.ptr.sample_rate
247+
else:
248+
self.ptr.time_base.num = 1
249+
self.ptr.time_base.den = lib.AV_TIME_BASE
248250

249251
err_check(lib.avcodec_open2(self.ptr, self.codec.ptr, &options.ptr))
250-
self._is_open = True
252+
self.is_open = True
251253
self.options = dict(options)
252254

253-
cdef _set_default_time_base(self):
254-
self.ptr.time_base.num = 1
255-
self.ptr.time_base.den = lib.AV_TIME_BASE
256-
257-
258255
def __dealloc__(self):
259256
if self.ptr and self.extradata_set:
260257
lib.av_freep(&self.ptr.extradata)
@@ -564,7 +561,7 @@ cdef class CodecContext:
564561

565562
@thread_count.setter
566563
def thread_count(self, int value):
567-
if self._is_open:
564+
if self.is_open:
568565
raise RuntimeError("Cannot change thread_count after codec is open.")
569566
self.ptr.thread_count = value
570567

@@ -579,7 +576,7 @@ cdef class CodecContext:
579576

580577
@thread_type.setter
581578
def thread_type(self, value):
582-
if self._is_open:
579+
if self.is_open:
583580
raise RuntimeError("Cannot change thread_type after codec is open.")
584581
self.ptr.thread_type = ThreadType[value].value
585582

av/video/codeccontext.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ cdef class VideoCodecContext(CodecContext):
2020
self._build_format()
2121
self.encoded_frame_count = 0
2222

23-
cdef _set_default_time_base(self):
24-
self.ptr.time_base.num = self.ptr.framerate.den or 1
25-
self.ptr.time_base.den = self.ptr.framerate.num or lib.AV_TIME_BASE
26-
2723
cdef _prepare_frames_for_encode(self, Frame input):
2824
if not input:
2925
return [None]

0 commit comments

Comments
 (0)