Skip to content

Commit

Permalink
Convert deprecated cython extension class properties to new syntax pa…
Browse files Browse the repository at this point in the history
…rt 4
  • Loading branch information
JoeSchiff authored and WyattBlue committed Feb 25, 2024
1 parent 4407056 commit 1e263c0
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 311 deletions.
22 changes: 11 additions & 11 deletions av/codec/codec.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,17 @@ cdef class Codec:
from .context import CodecContext
return CodecContext.create(self)

property is_decoder:
def __get__(self):
return not self.is_encoder
@property
def is_decoder(self):
return not self.is_encoder

property descriptor:
def __get__(self): return wrap_avclass(self.ptr.priv_class)
@property
def descriptor(self): return wrap_avclass(self.ptr.priv_class)

property name:
def __get__(self): return self.ptr.name or ""
property long_name:
def __get__(self): return self.ptr.long_name or ""
@property
def name(self): return self.ptr.name or ""
@property
def long_name(self): return self.ptr.long_name or ""

@property
def type(self):
Expand All @@ -231,8 +231,8 @@ cdef class Codec:
"""
return lib.av_get_media_type_string(self.ptr.type)

property id:
def __get__(self): return self.ptr.id
@property
def id(self): return self.ptr.id

@property
def frame_rates(self):
Expand Down
266 changes: 135 additions & 131 deletions av/codec/context.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -215,44 +215,45 @@ cdef class CodecContext:
skip_manual = flags2.flag_property("SKIP_MANUAL")
ro_flush_noop = flags2.flag_property("RO_FLUSH_NOOP")

property extradata:
def __get__(self):
if self.ptr.extradata_size > 0:
return <bytes>(<uint8_t*>self.ptr.extradata)[:self.ptr.extradata_size]
else:
return None

def __set__(self, data):
if not self.is_decoder:
raise ValueError("Can only set extradata for decoders.")

if data is None:
lib.av_freep(&self.ptr.extradata)
self.ptr.extradata_size = 0
else:
source = bytesource(data)
self.ptr.extradata = <uint8_t*>lib.av_realloc(self.ptr.extradata, source.length + lib.AV_INPUT_BUFFER_PADDING_SIZE)
if not self.ptr.extradata:
raise MemoryError("Cannot allocate extradata")
memcpy(self.ptr.extradata, source.ptr, source.length)
self.ptr.extradata_size = source.length
self.extradata_set = True

property extradata_size:
def __get__(self):
return self.ptr.extradata_size

property is_open:
def __get__(self):
return lib.avcodec_is_open(self.ptr)

property is_encoder:
def __get__(self):
return lib.av_codec_is_encoder(self.ptr.codec)

property is_decoder:
def __get__(self):
return lib.av_codec_is_decoder(self.ptr.codec)
@property
def extradata(self):
if self.ptr.extradata_size > 0:
return <bytes>(<uint8_t*>self.ptr.extradata)[:self.ptr.extradata_size]
else:
return None

@extradata.setter
def extradata(self, data):
if not self.is_decoder:
raise ValueError("Can only set extradata for decoders.")

if data is None:
lib.av_freep(&self.ptr.extradata)
self.ptr.extradata_size = 0
else:
source = bytesource(data)
self.ptr.extradata = <uint8_t*>lib.av_realloc(self.ptr.extradata, source.length + lib.AV_INPUT_BUFFER_PADDING_SIZE)
if not self.ptr.extradata:
raise MemoryError("Cannot allocate extradata")
memcpy(self.ptr.extradata, source.ptr, source.length)
self.ptr.extradata_size = source.length
self.extradata_set = True

@property
def extradata_size(self):
return self.ptr.extradata_size

@property
def is_open(self):
return lib.avcodec_is_open(self.ptr)

@property
def is_encoder(self):
return lib.av_codec_is_encoder(self.ptr.codec)

@property
def is_decoder(self):
return lib.av_codec_is_decoder(self.ptr.codec)

cpdef open(self, bint strict=True):
if lib.avcodec_is_open(self.ptr):
Expand Down Expand Up @@ -521,122 +522,125 @@ cdef class CodecContext:

frame.index = self.ptr.frame_number - 1

property name:
def __get__(self):
return self.codec.name

property type:
def __get__(self):
return self.codec.type

property profile:
def __get__(self):
if self.ptr.codec and lib.av_get_profile_name(self.ptr.codec, self.ptr.profile):
return lib.av_get_profile_name(self.ptr.codec, self.ptr.profile)

property time_base:
def __get__(self):
if self.is_decoder:
warnings.warn(
"Using CodecContext.time_base for decoders is deprecated.",
AVDeprecationWarning
)
return avrational_to_fraction(&self.ptr.time_base)

def __set__(self, value):
if self.is_decoder:
warnings.warn(
"Using CodecContext.time_base for decoders is deprecated.",
AVDeprecationWarning
)
to_avrational(value, &self.ptr.time_base)

property codec_tag:
def __get__(self):
return self.ptr.codec_tag.to_bytes(4, byteorder="little", signed=False).decode(
encoding="ascii")

def __set__(self, value):
if isinstance(value, str) and len(value) == 4:
self.ptr.codec_tag = int.from_bytes(value.encode(encoding="ascii"),
byteorder="little", signed=False)
else:
raise ValueError("Codec tag should be a 4 character string.")

property ticks_per_frame:
def __get__(self):
return self.ptr.ticks_per_frame

property bit_rate:
def __get__(self):
return self.ptr.bit_rate if self.ptr.bit_rate > 0 else None

def __set__(self, int value):
self.ptr.bit_rate = value

property max_bit_rate:
def __get__(self):
if self.ptr.rc_max_rate > 0:
return self.ptr.rc_max_rate
else:
return None

property bit_rate_tolerance:
def __get__(self):
self.ptr.bit_rate_tolerance

def __set__(self, int value):
self.ptr.bit_rate_tolerance = value

property thread_count:
@property
def name(self):
return self.codec.name

@property
def type(self):
return self.codec.type

@property
def profile(self):
if self.ptr.codec and lib.av_get_profile_name(self.ptr.codec, self.ptr.profile):
return lib.av_get_profile_name(self.ptr.codec, self.ptr.profile)

@property
def time_base(self):
if self.is_decoder:
warnings.warn(
"Using CodecContext.time_base for decoders is deprecated.",
AVDeprecationWarning
)
return avrational_to_fraction(&self.ptr.time_base)

@time_base.setter
def time_base(self, value):
if self.is_decoder:
warnings.warn(
"Using CodecContext.time_base for decoders is deprecated.",
AVDeprecationWarning
)
to_avrational(value, &self.ptr.time_base)

@property
def codec_tag(self):
return self.ptr.codec_tag.to_bytes(4, byteorder="little", signed=False).decode(
encoding="ascii")

@codec_tag.setter
def codec_tag(self, value):
if isinstance(value, str) and len(value) == 4:
self.ptr.codec_tag = int.from_bytes(value.encode(encoding="ascii"),
byteorder="little", signed=False)
else:
raise ValueError("Codec tag should be a 4 character string.")

@property
def ticks_per_frame(self):
return self.ptr.ticks_per_frame

@property
def bit_rate(self):
return self.ptr.bit_rate if self.ptr.bit_rate > 0 else None

@bit_rate.setter
def bit_rate(self, int value):
self.ptr.bit_rate = value

@property
def max_bit_rate(self):
if self.ptr.rc_max_rate > 0:
return self.ptr.rc_max_rate
else:
return None

@property
def bit_rate_tolerance(self):
self.ptr.bit_rate_tolerance

@bit_rate_tolerance.setter
def bit_rate_tolerance(self, int value):
self.ptr.bit_rate_tolerance = value

@property
def thread_count(self):
"""How many threads to use; 0 means auto.
Wraps :ffmpeg:`AVCodecContext.thread_count`.
"""
return self.ptr.thread_count

def __get__(self):
return self.ptr.thread_count

def __set__(self, int value):
if lib.avcodec_is_open(self.ptr):
raise RuntimeError("Cannot change thread_count after codec is open.")
self.ptr.thread_count = value
@thread_count.setter
def thread_count(self, int value):
if lib.avcodec_is_open(self.ptr):
raise RuntimeError("Cannot change thread_count after codec is open.")
self.ptr.thread_count = value

property thread_type:
@property
def thread_type(self):
"""One of :class:`.ThreadType`.
Wraps :ffmpeg:`AVCodecContext.thread_type`.
"""
return ThreadType.get(self.ptr.thread_type, create=True)

def __get__(self):
return ThreadType.get(self.ptr.thread_type, create=True)

def __set__(self, value):
if lib.avcodec_is_open(self.ptr):
raise RuntimeError("Cannot change thread_type after codec is open.")
self.ptr.thread_type = ThreadType[value].value
@thread_type.setter
def thread_type(self, value):
if lib.avcodec_is_open(self.ptr):
raise RuntimeError("Cannot change thread_type after codec is open.")
self.ptr.thread_type = ThreadType[value].value

property skip_frame:
@property
def skip_frame(self):
"""One of :class:`.SkipType`.
Wraps ffmpeg:`AVCodecContext.skip_frame`.
"""
return SkipType._get(self.ptr.skip_frame, create=True)

def __get__(self):
return SkipType._get(self.ptr.skip_frame, create=True)

def __set__(self, value):
self.ptr.skip_frame = SkipType[value].value
@skip_frame.setter
def skip_frame(self, value):
self.ptr.skip_frame = SkipType[value].value

property delay:
@property
def delay(self):
"""Codec delay.
Wraps :ffmpeg:`AVCodecContext.delay`.
"""

def __get__(self):
return self.ptr.delay
return self.ptr.delay
42 changes: 21 additions & 21 deletions av/container/input.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,27 @@ cdef class InputContainer(Container):
def __dealloc__(self):
close_input(self)

property start_time:
def __get__(self):
self._assert_open()
if self.ptr.start_time != lib.AV_NOPTS_VALUE:
return self.ptr.start_time

property duration:
def __get__(self):
self._assert_open()
if self.ptr.duration != lib.AV_NOPTS_VALUE:
return self.ptr.duration

property bit_rate:
def __get__(self):
self._assert_open()
return self.ptr.bit_rate

property size:
def __get__(self):
self._assert_open()
return lib.avio_size(self.ptr.pb)
@property
def start_time(self):
self._assert_open()
if self.ptr.start_time != lib.AV_NOPTS_VALUE:
return self.ptr.start_time

@property
def duration(self):
self._assert_open()
if self.ptr.duration != lib.AV_NOPTS_VALUE:
return self.ptr.duration

@property
def bit_rate(self):
self._assert_open()
return self.ptr.bit_rate

@property
def size(self):
self._assert_open()
return lib.avio_size(self.ptr.pb)

def close(self):
close_input(self)
Expand Down
Loading

0 comments on commit 1e263c0

Please sign in to comment.