Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Features:
- Support ``yuv420p10le`` in ``VideoFrame.to_ndarray`` and ``VideoFrame.from_ndarray`` by :gh-user:`WyattBlue` (:issue:`1981`).
- Add ``at`` parameter to ``Graph.push`` and ``Graph.vpush`` to push a frame to a single buffer source by index, for multi-input filters like ``overlay`` by :gh-user:`WyattBlue`.
- ``find_best_pix_fmt_of_list`` now returns the loss as a ``PixFmtLoss`` ``enum.IntFlag`` instead of a plain ``int`` by :gh-user:`WyattBlue` (:issue:`2300`).
- Add ``Colorspace.BT2020`` by :gh-user:`mark-oshea`.

Fixes:

Expand Down
1 change: 1 addition & 0 deletions av/video/reformatter.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cdef extern from "libswscale/swscale.h" nogil:
cdef int SWS_CS_SMPTE170M
cdef int SWS_CS_SMPTE240M
cdef int SWS_CS_DEFAULT
cdef int SWS_CS_BT2020

cdef SwsContext *sws_alloc_context()
cdef void sws_free_context(SwsContext **ctx)
Expand Down
4 changes: 4 additions & 0 deletions av/video/reformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Colorspace(IntEnum):
SMPTE170M = SWS_CS_SMPTE170M
SMPTE240M = SWS_CS_SMPTE240M
DEFAULT = SWS_CS_DEFAULT
BT2020 = SWS_CS_BT2020
# Lowercase for b/c.
itu709 = SWS_CS_ITU709
fcc = SWS_CS_FCC
Expand All @@ -44,6 +45,7 @@ class Colorspace(IntEnum):
smpte170m = SWS_CS_SMPTE170M
smpte240m = SWS_CS_SMPTE240M
default = SWS_CS_DEFAULT
bt2020 = SWS_CS_BT2020


class ColorRange(IntEnum):
Expand Down Expand Up @@ -146,6 +148,8 @@ def _set_frame_colorspace(
frame.colorspace = lib.AVCOL_SPC_SMPTE170M
elif colorspace == SWS_CS_SMPTE240M:
frame.colorspace = lib.AVCOL_SPC_SMPTE240M
elif colorspace == SWS_CS_BT2020:
frame.colorspace = lib.AVCOL_SPC_BT2020_NCL


@cython.final
Expand Down
2 changes: 2 additions & 0 deletions av/video/reformatter.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ class Colorspace(IntEnum):
SMPTE170M = cast(int, ...)
SMPTE240M = cast(int, ...)
DEFAULT = cast(int, ...)
BT2020 = cast(int, ...)
itu709 = cast(int, ...)
fcc = cast(int, ...)
itu601 = cast(int, ...)
itu624 = cast(int, ...)
smpte170m = cast(int, ...)
smpte240m = cast(int, ...)
default = cast(int, ...)
bt2020 = cast(int, ...)

class ColorRange(IntEnum):
UNSPECIFIED = 0
Expand Down
20 changes: 20 additions & 0 deletions tests/test_colorspace.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import av
from av.video.reformatter import (
ColorPrimaries,
Expand Down Expand Up @@ -101,3 +103,21 @@ def test_reformat_preserves_color_primaries() -> None:
frame.color_primaries = ColorPrimaries.BT709
rgb = frame.reformat(format="rgb24")
assert rgb.color_primaries == ColorPrimaries.BT709


@pytest.mark.parametrize(
("colorspace", "expected"),
[
(Colorspace.ITU709, Colorspace.ITU709),
(Colorspace.FCC, Colorspace.FCC),
(Colorspace.ITU601, 6), # AVCOL_SPC_SMPTE170M
(Colorspace.SMPTE240M, Colorspace.SMPTE240M),
(Colorspace.BT2020, Colorspace.BT2020),
],
)
def test_reformat_dst_colorspace_metadata(
colorspace: Colorspace, expected: Colorspace | int
) -> None:
frame = av.VideoFrame(width=64, height=64, format="yuv420p")
rgb = frame.reformat(format="rgb24", dst_colorspace=colorspace)
assert rgb.colorspace == expected
Loading