Skip to content

Commit

Permalink
Use IntEnums
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Nov 8, 2024
1 parent 0564862 commit cffe076
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/smoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, python: "3.9", ffmpeg: "7.1", extras: true}
- {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"}
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default: build


build:
$(PIP) install --upgrade cython
$(PIP) install -U cython setuptools
CFLAGS=$(CFLAGS) LDFLAGS=$(LDFLAGS) $(PYTHON) setup.py build_ext --inplace --debug

clean:
Expand Down
77 changes: 38 additions & 39 deletions av/video/reformatter.pyx
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
cimport libav as lib
from libc.stdint cimport uint8_t

from av.enum cimport define_enum
from av.error cimport err_check
from av.video.format cimport VideoFormat
from av.video.frame cimport alloc_video_frame

Interpolation = define_enum("Interpolation", __name__, (
("FAST_BILINEAR", lib.SWS_FAST_BILINEAR, "Fast bilinear"),
("BILINEAR", lib.SWS_BILINEAR, "Bilinear"),
("BICUBIC", lib.SWS_BICUBIC, "Bicubic"),
("X", lib.SWS_X, "Experimental"),
("POINT", lib.SWS_POINT, "Nearest neighbor / point"),
("AREA", lib.SWS_AREA, "Area averaging"),
("BICUBLIN", lib.SWS_BICUBLIN, "Luma bicubic / chroma bilinear"),
("GAUSS", lib.SWS_GAUSS, "Gaussian"),
("SINC", lib.SWS_SINC, "Sinc"),
("LANCZOS", lib.SWS_LANCZOS, "Lanczos"),
("SPLINE", lib.SWS_SPLINE, "Bicubic spline"),
))

Colorspace = define_enum("Colorspace", __name__, (
("ITU709", lib.SWS_CS_ITU709),
("FCC", lib.SWS_CS_FCC),
("ITU601", lib.SWS_CS_ITU601),
("ITU624", lib.SWS_CS_ITU624),
("SMPTE170M", lib.SWS_CS_SMPTE170M),
("SMPTE240M", lib.SWS_CS_SMPTE240M),
("DEFAULT", lib.SWS_CS_DEFAULT),

from enum import IntEnum


class Interpolation(IntEnum):
FAST_BILINEAR: "Fast bilinear" = lib.SWS_FAST_BILINEAR
BILINEAR: "Bilinear" = lib.SWS_BILINEAR
BICUBIC: "Bicubic" = lib.SWS_BICUBIC
X: "Experimental" = lib.SWS_X
POINT: "Nearest neighbor / point" = lib.SWS_POINT
AREA: "Area averaging" = lib.SWS_AREA
BICUBLIN: "Luma bicubic / chroma bilinear" = lib.SWS_BICUBLIN
GAUSS: "Gaussian" = lib.SWS_GAUSS
SINC: "Sinc" = lib.SWS_SINC
LANCZOS: "Bicubic spline" = lib.SWS_LANCZOS


class Colorspace(IntEnum):
ITU709 = lib.SWS_CS_ITU709
FCC = lib.SWS_CS_FCC
ITU601 = lib.SWS_CS_ITU601
ITU624 = lib.SWS_CS_ITU624
SMPTE170M = lib.SWS_CS_SMPTE170M
SMPTE240M = lib.SWS_CS_SMPTE240M
DEFAULT = lib.SWS_CS_DEFAULT
# Lowercase for b/c.
("itu709", lib.SWS_CS_ITU709),
("fcc", lib.SWS_CS_FCC),
("itu601", lib.SWS_CS_ITU601),
("itu624", lib.SWS_CS_SMPTE170M),
("smpte240", lib.SWS_CS_SMPTE240M),
("default", lib.SWS_CS_DEFAULT),

))

ColorRange = define_enum("ColorRange", __name__, (
("UNSPECIFIED", lib.AVCOL_RANGE_UNSPECIFIED, "Unspecified"),
("MPEG", lib.AVCOL_RANGE_MPEG, "MPEG (limited) YUV range, 219*2^(n-8)"),
("JPEG", lib.AVCOL_RANGE_JPEG, "JPEG (full) YUV range, 2^n-1"),
("NB", lib.AVCOL_RANGE_NB, "Not part of ABI"),
))
itu709 = lib.SWS_CS_ITU709
fcc = lib.SWS_CS_FCC
itu601 = lib.SWS_CS_ITU601
itu624 = lib.SWS_CS_SMPTE170M
smpte170m = lib.SWS_CS_SMPTE170M
smpte240m = lib.SWS_CS_SMPTE240M
default = lib.SWS_CS_DEFAULT

class ColorRange(IntEnum):
UNSPECIFIED: "Unspecified" = lib.AVCOL_RANGE_UNSPECIFIED
MPEG: "MPEG (limited) YUV range, 219*2^(n-8)" = lib.AVCOL_RANGE_MPEG
JPEG: "JPEG (full) YUV range, 2^n-1" = lib.AVCOL_RANGE_JPEG
NB: "Not part of ABI" = lib.AVCOL_RANGE_NB


cdef class VideoReformatter:
"""An object for reformatting size and pixel format of :class:`.VideoFrame`.
Expand Down
14 changes: 6 additions & 8 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@

SPHINXOPTS =
SPHINXBUILD = sphinx-build
BUILDDIR = _build
FFMPEGDIR = _ffmpeg

PYAV_PIP ?= pip
PIP := $(PYAV_PIP)

ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(SPHINXOPTS) .

.PHONY: clean html open upload default

default: html


TAGFILE := _build/doxygen/tagfile.xml
$(TAGFILE) :
git clone --depth=1 git://source.ffmpeg.org/ffmpeg.git $(FFMPEGDIR)
./generate-tagfile --library $(FFMPEGDIR) -o $(TAGFILE)

$(TAGFILE):
@if [ ! -d "$(FFMPEGDIR)" ]; then \
git clone --depth=1 git://source.ffmpeg.org/ffmpeg.git $(FFMPEGDIR); \
fi
./generate-tagfile --library $(FFMPEGDIR) -o $(TAGFILE)

TEMPLATES := $(wildcard api/*.py development/*.py)
RENDERED := $(TEMPLATES:%.py=_build/rst/%.rst)

_build/rst/%.rst: %.py $(TAGFILE) $(shell find ../include ../av -name '*.pyx' -or -name '*.pxd')
@ mkdir -p $(@D)
python $< > $@.tmp
mv $@.tmp $@


clean:
rm -rf $(BUILDDIR) $(FFMPEGDIR)

Expand Down
1 change: 0 additions & 1 deletion docs/api/codec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ Contexts

.. automethod:: CodecContext.create
.. automethod:: CodecContext.open
.. automethod:: CodecContext.close

Attributes
~~~~~~~~~~
Expand Down
6 changes: 6 additions & 0 deletions docs/api/video.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,9 @@ Enums

.. enumtable:: av.video.reformatter.Colorspace

.. autoclass:: av.video.reformatter.ColorRange

Wraps the ``AVCOL*`` flags.

.. enumtable:: av.video.reformatter.ColorRange

54 changes: 39 additions & 15 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,24 +238,48 @@ def makerow(*texts):
)

seen = set()

for name, item in enum._by_name.items():
if name.lower() in seen:
continue
seen.add(name.lower())

try:
attr = properties[item]
except KeyError:
if cls:
if hasattr(enum, "_by_name"): # Our custom enum class
enum_items = enum._by_name.items()
for name, item in enum_items:
if name.lower() in seen:
continue
seen.add(name.lower())

try:
attr = properties[item]
except KeyError:
if cls:
continue
attr = None

value = f"0x{item.value:X}"
doc = item.__doc__ or "-"
tbody += makerow(attr, name, value, doc)

return [table]
else: # Standard IntEnum
enum_items = [
(name, item)
for name, item in vars(enum).items()
if isinstance(item, enum)
]
for name, item in enum_items:
if name.lower() in seen:
continue
attr = None
seen.add(name.lower())

try:
attr = properties[item]
except KeyError:
if cls:
continue
attr = None

value = f"0x{item.value:X}"
doc = item.__doc__ or "-"
tbody += makerow(attr, name, value, doc)
value = f"0x{item.value:X}"
doc = enum.__annotations__.get(name, "---")[1:-1]
tbody += makerow(attr, name, value, doc)

return [table]
return [table]


doxylink = {}
Expand Down
2 changes: 1 addition & 1 deletion scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ which ffmpeg || exit 2
ffmpeg -version || exit 3
echo

$PYAV_PIP install -U cython 2> /dev/null
$PYAV_PIP install -U cython setuptools 2> /dev/null
"$PYAV_PYTHON" scripts/comptime.py
"$PYAV_PYTHON" setup.py config build_ext --inplace || exit 1
4 changes: 2 additions & 2 deletions tests/test_videoframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,11 @@ def test_reformat_identity() -> None:
def test_reformat_colorspace() -> None:
# This is allowed.
frame = VideoFrame(640, 480, "rgb24")
frame.reformat(src_colorspace=None, dst_colorspace="smpte240")
frame.reformat(src_colorspace=None, dst_colorspace="smpte240m")

# I thought this was not allowed, but it seems to be.
frame = VideoFrame(640, 480, "yuv420p")
frame.reformat(src_colorspace=None, dst_colorspace="smpte240")
frame.reformat(src_colorspace=None, dst_colorspace="smpte240m")


def test_reformat_pixel_format_align() -> None:
Expand Down

0 comments on commit cffe076

Please sign in to comment.