Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip_frame: Use str literals instead of Enum #1648

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/smoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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

Expand Down
13 changes: 3 additions & 10 deletions av/codec/context.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 41 additions & 12 deletions av/codec/context.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_codec_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading