From 2c1c7fe6a22839c1b4df1984e641eccfe5801b70 Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Sun, 24 Nov 2024 00:56:09 -0500 Subject: [PATCH] `skip_frame`: Use str literals instead of Enum --- .github/workflows/smoke.yml | 6 ++--- av/codec/context.pyi | 13 +++------ av/codec/context.pyx | 53 ++++++++++++++++++++++++++++--------- tests/test_codec_context.py | 2 +- 4 files changed, 48 insertions(+), 26 deletions(-) diff --git a/.github/workflows/smoke.yml b/.github/workflows/smoke.yml index ca6a3f15c..352634ff6 100644 --- a/.github/workflows/smoke.yml +++ b/.github/workflows/smoke.yml @@ -37,8 +37,8 @@ jobs: - {os: ubuntu-latest, python: "3.12", ffmpeg: "7.1", extras: true} - {os: ubuntu-latest, python: "3.9", ffmpeg: "7.0.2"} - {os: ubuntu-latest, python: pypy3.9, ffmpeg: "7.1"} - - {os: macos-14, python: "3.9", ffmpeg: "7.1"} - - {os: macos-14, python: "3.9", ffmpeg: "7.0.2"} + - {os: macos-14, python: "3.9", ffmpeg: "7.1"} + - {os: macos-14, python: "3.9", ffmpeg: "7.0.2"} env: PYAV_PYTHON: python${{ matrix.config.python }} @@ -65,7 +65,7 @@ jobs: fi ;; macos-14) - brew install automake libtool nasm pkg-config libpng libvorbis libvpx opus x264 + brew install automake libtool nasm libpng libvorbis libvpx opus x264 ;; esac diff --git a/av/codec/context.pyi b/av/codec/context.pyi index 8b7458597..e58b0879d 100644 --- a/av/codec/context.pyi +++ b/av/codec/context.pyi @@ -15,15 +15,6 @@ class ThreadType(Flag): def __get__(self, i: object | None, owner: type | None = None) -> ThreadType: ... def __set__(self, instance: object, value: int | str | ThreadType) -> None: ... -class SkipType(Enum): - NONE: ClassVar[SkipType] - DEFAULT: ClassVar[SkipType] - NONREF: ClassVar[SkipType] - BIDIR: ClassVar[SkipType] - NONINTRA: ClassVar[SkipType] - NONKEY: ClassVar[SkipType] - ALL: ClassVar[SkipType] - class Flags(EnumFlag): NONE: int UNALIGNED: int @@ -71,7 +62,9 @@ class CodecContext: bit_rate_tolerance: int thread_count: int thread_type: ThreadType - skip_frame: SkipType + skip_frame: Literal[ + "NONE", "DEFAULT", "NONREF", "BIDIR", "NONINTRA", "NONKEY", "ALL" + ] # flags unaligned: bool diff --git a/av/codec/context.pyx b/av/codec/context.pyx index 6e039a4e1..f420b1157 100644 --- a/av/codec/context.pyx +++ b/av/codec/context.pyx @@ -47,14 +47,6 @@ class ThreadType(Flag): SLICE: "Decode more than one part of a single frame at once" = lib.FF_THREAD_SLICE AUTO: "Decode using both FRAME and SLICE methods." = lib.FF_THREAD_SLICE | lib.FF_THREAD_FRAME -class SkipType(Enum): - NONE: "Discard nothing" = lib.AVDISCARD_NONE - DEFAULT: "Discard useless packets like 0 size packets in AVI" = lib.AVDISCARD_DEFAULT - NONREF: "Discard all non reference" = lib.AVDISCARD_NONREF - BIDIR: "Discard all bidirectional frames" = lib.AVDISCARD_BIDIR - NONINTRA: "Discard all non intra frames" = lib.AVDISCARD_NONINTRA - NONKEY: "Discard all frames except keyframes" = lib.AVDISCARD_NONKEY - ALL: "Discard all" = lib.AVDISCARD_ALL Flags = define_enum("Flags", __name__, ( ("NONE", 0), @@ -630,16 +622,53 @@ cdef class CodecContext: @property def skip_frame(self): - """One of :class:`.SkipType`. + """Returns one of the following str literals: - Wraps :ffmpeg:`AVCodecContext.skip_frame`. + "NONE" Discard nothing + "DEFAULT" Discard useless packets like 0 size packets in AVI + "NONREF" Discard all non reference + "BIDIR" Discard all bidirectional frames + "NONINTRA" Discard all non intra frames + "NONKEY Discard all frames except keyframes + "ALL" Discard all + Wraps :ffmpeg:`AVCodecContext.skip_frame`. """ - return SkipType(self.ptr.skip_frame) + value = self.ptr.skip_frame + if value == lib.AVDISCARD_NONE: + return "NONE" + if value == lib.AVDISCARD_DEFAULT: + return "DEFAULT" + if value == lib.AVDISCARD_NONREF: + return "NONREF" + if value == lib.AVDISCARD_BIDIR: + return "BIDIR" + if value == lib.AVDISCARD_NONINTRA: + return "NONINTRA" + if value == lib.AVDISCARD_NONKEY: + return "NONKEY" + if value == lib.AVDISCARD_ALL: + return "ALL" + return f"{value}" @skip_frame.setter def skip_frame(self, value): - self.ptr.skip_frame = value.value + if value == "NONE": + self.ptr.skip_frame = lib.AVDISCARD_NONE + elif value == "DEFAULT": + self.ptr.skip_frame = lib.AVDISCARD_DEFAULT + elif value == "NONREF": + self.ptr.skip_frame = lib.AVDISCARD_NONREF + elif value == "BIDIR": + self.ptr.skip_frame = lib.AVDISCARD_BIDIR + elif value == "NONINTRA": + self.ptr.skip_frame = lib.AVDISCARD_NONINTRA + elif value == "NONKEY": + self.ptr.skip_frame = lib.AVDISCARD_NONKEY + elif value == "ALL": + self.ptr.skip_frame = lib.AVDISCARD_ALL + else: + raise ValueError("Invalid skip_frame type") @property def delay(self): diff --git a/tests/test_codec_context.py b/tests/test_codec_context.py index 6425c4dd1..1173a5865 100644 --- a/tests/test_codec_context.py +++ b/tests/test_codec_context.py @@ -70,7 +70,7 @@ def iter_raw_frames( class TestCodecContext(TestCase): def test_skip_frame_default(self): ctx = Codec("png", "w").create() - assert ctx.skip_frame.name == "DEFAULT" + assert ctx.skip_frame == "DEFAULT" def test_codec_delay(self) -> None: with av.open(fate_suite("mkv/codec_delay_opus.mkv")) as container: