Skip to content

Commit

Permalink
Sync with upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Dec 6, 2024
1 parent ad7c25d commit 4e22c1f
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion av/about.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "14.0.0"
__version__ = "14.0.1"
2 changes: 1 addition & 1 deletion av/container/output.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ cdef class OutputContainer(Container):
cdef const lib.AVCodec *codec
cdef Codec codec_obj

if template.type == "subtitle":
if template.type != "video":
codec_obj = template.codec_context.codec
else:
codec_obj = Codec(template.codec_context.codec.name, "w")
Expand Down
2 changes: 0 additions & 2 deletions av/stream.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ cdef class Stream:
# Stream attributes.
cdef readonly Container container
cdef readonly dict metadata
cdef readonly int nb_side_data
cdef readonly dict side_data

# CodecContext attributes.
cdef readonly CodecContext codec_context
Expand Down
8 changes: 1 addition & 7 deletions av/stream.pyi
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
from enum import Enum
from fractions import Fraction
from typing import ClassVar, Literal
from typing import Literal

from .codec import Codec, CodecContext
from .container import Container
from .frame import Frame
from .packet import Packet

class SideData(Enum):
DISPLAYMATRIX: ClassVar[SideData]

class Stream:
name: str | None
Expand Down
8 changes: 1 addition & 7 deletions av/stream.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cimport libav as lib
from libc.stdint cimport int32_t

from enum import Enum

Expand All @@ -15,11 +14,6 @@ from av.utils cimport (

cdef object _cinit_bypass_sentinel = object()

# If necessary more can be added from
# https://ffmpeg.org/doxygen/trunk/group__lavc__packet.html#ga9a80bfcacc586b483a973272800edb97
class SideData(Enum):
DISPLAYMATRIX: "Display Matrix" = lib.AV_PKT_DATA_DISPLAYMATRIX

cdef Stream wrap_stream(Container container, lib.AVStream *c_stream, CodecContext codec_context):
"""Build an av.Stream for an existing AVStream.
Expand Down Expand Up @@ -84,7 +78,7 @@ cdef class Stream:
self.codec_context = codec_context
if self.codec_context:
self.codec_context.stream_index = stream.index

self.metadata = avdict_to_dict(
stream.metadata,
encoding=self.container.metadata_encoding,
Expand Down
3 changes: 2 additions & 1 deletion av/video/reformatter.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class Colorspace(IntEnum):
fcc: int
itu601: int
itu624: int
smpte240: int
smpte170m: int
smpte240m: int
default: int

class ColorRange(IntEnum):
Expand Down
24 changes: 19 additions & 5 deletions av/video/reformatter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ class ColorRange(IntEnum):
NB: "Not part of ABI" = lib.AVCOL_RANGE_NB


def _resolve_enum_value(value, enum_class, default):
# Helper function to resolve enum values from different input types.
if value is None:
return default
if isinstance(value, enum_class):
return value.value
if isinstance(value, int):
return value
if isinstance(value, str):
return enum_class[value].value
raise ValueError(f"Cannot convert {value} to {enum_class.__name__}")


cdef class VideoReformatter:
"""An object for reformatting size and pixel format of :class:`.VideoFrame`.
Expand Down Expand Up @@ -83,11 +96,12 @@ cdef class VideoReformatter:
"""

cdef VideoFormat video_format = VideoFormat(format if format is not None else frame.format)
cdef int c_src_colorspace = (Colorspace[src_colorspace].value if src_colorspace is not None else frame.colorspace)
cdef int c_dst_colorspace = (Colorspace[dst_colorspace].value if dst_colorspace is not None else frame.colorspace)
cdef int c_interpolation = (Interpolation[interpolation] if interpolation is not None else Interpolation.BILINEAR).value
cdef int c_src_color_range = (ColorRange[src_color_range].value if src_color_range is not None else 0)
cdef int c_dst_color_range = (ColorRange[dst_color_range].value if dst_color_range is not None else 0)

cdef int c_src_colorspace = _resolve_enum_value(src_colorspace, Colorspace, frame.colorspace)
cdef int c_dst_colorspace = _resolve_enum_value(dst_colorspace, Colorspace, frame.colorspace)
cdef int c_interpolation = _resolve_enum_value(interpolation, Interpolation, int(Interpolation.BILINEAR))
cdef int c_src_color_range = _resolve_enum_value(src_color_range, ColorRange, 0)
cdef int c_dst_color_range = _resolve_enum_value(dst_color_range, ColorRange, 0)

return self._reformat(
frame,
Expand Down
29 changes: 27 additions & 2 deletions tests/test_videoframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import av
from av import VideoFrame
from av.video.reformatter import ColorRange, Colorspace, Interpolation

from .common import (
TestCase,
Expand Down Expand Up @@ -145,6 +146,24 @@ def test_roundtrip(self) -> None:
img.save(self.sandboxed("roundtrip-high.jpg"))
assertImagesAlmostEqual(image, img)

def test_interpolation(self) -> None:
import PIL.Image as Image

image = Image.open(fate_png())
frame = VideoFrame.from_image(image)
assert frame.width == 330 and frame.height == 330

img = frame.to_image(width=200, height=100, interpolation=Interpolation.BICUBIC)
assert img.width == 200 and img.height == 100

img = frame.to_image(width=200, height=100, interpolation="BICUBIC")
assert img.width == 200 and img.height == 100

img = frame.to_image(
width=200, height=100, interpolation=int(Interpolation.BICUBIC)
)
assert img.width == 200 and img.height == 100

def test_to_image_rgb24(self) -> None:
sizes = [(318, 238), (320, 240), (500, 500)]
for width, height in sizes:
Expand Down Expand Up @@ -838,14 +857,20 @@ 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="smpte240m")

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

frame = VideoFrame(640, 480, "yuv420p")
frame.reformat(src_colorspace=None, dst_colorspace="smpte240m")

frame = VideoFrame(640, 480, "rgb24")
frame.colorspace = Colorspace.smpte240m
assert frame.colorspace == int(Colorspace.smpte240m)
assert frame.colorspace == Colorspace.smpte240m


def test_reformat_pixel_format_align() -> None:
height = 480
Expand Down

0 comments on commit 4e22c1f

Please sign in to comment.