| 1 |
import av |
| 2 |
from av.video.reformatter import ( |
| 3 |
ColorPrimaries, |
| 4 |
ColorRange, |
| 5 |
Colorspace, |
| 6 |
ColorTrc, |
| 7 |
) |
| 8 |
|
| 9 |
from .common import fate_suite |
| 10 |
|
| 11 |
|
| 12 |
def test_penguin_joke() -> None: |
| 13 |
container = av.open( |
| 14 |
fate_suite("amv/MTV_high_res_320x240_sample_Penguin_Joke_MTV_from_WMV.amv") |
| 15 |
) |
| 16 |
stream = container.streams.video[0] |
| 17 |
|
| 18 |
assert stream.codec_context.color_range == 2 |
| 19 |
assert stream.codec_context.color_range == ColorRange.JPEG |
| 20 |
|
| 21 |
assert stream.codec_context.color_primaries == 2 |
| 22 |
assert stream.codec_context.color_trc == 2 |
| 23 |
|
| 24 |
assert stream.codec_context.colorspace == 5 |
| 25 |
assert stream.codec_context.colorspace == Colorspace.ITU601 |
| 26 |
|
| 27 |
for frame in container.decode(stream): |
| 28 |
assert frame.color_range == ColorRange.JPEG # a.k.a "pc" |
| 29 |
assert frame.colorspace == Colorspace.ITU601 |
| 30 |
return |
| 31 |
|
| 32 |
|
| 33 |
def test_sky_timelapse() -> None: |
| 34 |
container = av.open( |
| 35 |
av.datasets.curated("pexels/time-lapse-video-of-night-sky-857195.mp4") |
| 36 |
) |
| 37 |
stream = container.streams.video[0] |
| 38 |
|
| 39 |
assert stream.disposition == av.stream.Disposition.default |
| 40 |
|
| 41 |
assert stream.codec_context.color_range == 1 |
| 42 |
assert stream.codec_context.color_range == ColorRange.MPEG |
| 43 |
assert stream.codec_context.color_primaries == 1 |
| 44 |
assert stream.codec_context.color_trc == 1 |
| 45 |
assert stream.codec_context.colorspace == 1 |
| 46 |
|
| 47 |
|
| 48 |
def test_frame_color_trc_property() -> None: |
| 49 |
frame = av.VideoFrame(width=64, height=64, format="rgb24") |
| 50 |
assert frame.color_trc == ColorTrc.UNSPECIFIED |
| 51 |
|
| 52 |
frame.color_trc = ColorTrc.IEC61966_2_4 |
| 53 |
assert frame.color_trc == ColorTrc.IEC61966_2_4 |
| 54 |
|
| 55 |
frame.color_trc = ColorTrc.BT709 |
| 56 |
assert frame.color_trc == ColorTrc.BT709 |
| 57 |
|
| 58 |
|
| 59 |
def test_frame_color_primaries_property() -> None: |
| 60 |
frame = av.VideoFrame(width=64, height=64, format="rgb24") |
| 61 |
assert frame.color_primaries == ColorPrimaries.UNSPECIFIED |
| 62 |
|
| 63 |
frame.color_primaries = ColorPrimaries.BT709 |
| 64 |
assert frame.color_primaries == ColorPrimaries.BT709 |
| 65 |
assert frame.color_primaries == 1 # AVCOL_PRI_BT709 |
| 66 |
|
| 67 |
|
| 68 |
def test_reformat_dst_color_trc() -> None: |
| 69 |
# Reformat a frame and tag it with sRGB transfer characteristic. |
| 70 |
frame = av.VideoFrame(width=64, height=64, format="yuv420p") |
| 71 |
rgb = frame.reformat( |
| 72 |
format="rgb24", |
| 73 |
dst_colorspace=Colorspace.ITU709, |
| 74 |
dst_color_trc=ColorTrc.IEC61966_2_4, |
| 75 |
) |
| 76 |
assert rgb.format.name == "rgb24" |
| 77 |
assert rgb.colorspace == Colorspace.ITU709 |
| 78 |
assert rgb.color_trc == ColorTrc.IEC61966_2_4 |
| 79 |
|
| 80 |
|
| 81 |
def test_reformat_dst_color_primaries() -> None: |
| 82 |
frame = av.VideoFrame(width=64, height=64, format="yuv420p") |
| 83 |
rgb = frame.reformat( |
| 84 |
format="rgb24", |
| 85 |
dst_color_primaries=ColorPrimaries.BT709, |
| 86 |
) |
| 87 |
assert rgb.color_primaries == ColorPrimaries.BT709 |
| 88 |
|
| 89 |
|
| 90 |
def test_reformat_preserves_color_trc() -> None: |
| 91 |
# When dst_color_trc is not specified, the source frame's value is preserved. |
| 92 |
frame = av.VideoFrame(width=64, height=64, format="yuv420p") |
| 93 |
frame.color_trc = ColorTrc.BT709 |
| 94 |
rgb = frame.reformat(format="rgb24") |
| 95 |
assert rgb.color_trc == ColorTrc.BT709 |
| 96 |
|
| 97 |
|
| 98 |
def test_reformat_preserves_color_primaries() -> None: |
| 99 |
# When dst_color_primaries is not specified, the source frame's value is preserved. |
| 100 |
frame = av.VideoFrame(width=64, height=64, format="yuv420p") |
| 101 |
frame.color_primaries = ColorPrimaries.BT709 |
| 102 |
rgb = frame.reformat(format="rgb24") |
| 103 |
assert rgb.color_primaries == ColorPrimaries.BT709 |