| 1 |
import pytest |
| 2 |
|
| 3 |
from av import AudioFormat, Codec, VideoFormat, codecs_available |
| 4 |
from av.codec import find_best_pix_fmt_of_list |
| 5 |
from av.codec.codec import UnknownCodecError |
| 6 |
|
| 7 |
|
| 8 |
def test_codec_bogus() -> None: |
| 9 |
with pytest.raises(UnknownCodecError): |
| 10 |
Codec("bogus123") |
| 11 |
with pytest.raises(UnknownCodecError): |
| 12 |
Codec("bogus123", "w") |
| 13 |
|
| 14 |
|
| 15 |
def test_codec_mpeg4_decoder() -> None: |
| 16 |
c = Codec("mpeg4") |
| 17 |
|
| 18 |
assert c.name == "mpeg4" |
| 19 |
assert c.long_name == "MPEG-4 part 2" |
| 20 |
assert c.type == "video" |
| 21 |
assert c.id in (12, 13) |
| 22 |
assert c.is_decoder |
| 23 |
assert not c.is_encoder |
| 24 |
assert c.delay |
| 25 |
|
| 26 |
assert c.audio_formats is None and c.audio_rates is None |
| 27 |
|
| 28 |
# formats = c.video_formats |
| 29 |
# assert formats |
| 30 |
# assert isinstance(formats[0], VideoFormat) |
| 31 |
# assert any(f.name == "yuv420p" for f in formats) |
| 32 |
|
| 33 |
assert c.frame_rates is None |
| 34 |
|
| 35 |
|
| 36 |
def test_codec_mpeg4_encoder() -> None: |
| 37 |
c = Codec("mpeg4", "w") |
| 38 |
assert c.name == "mpeg4" |
| 39 |
assert c.long_name == "MPEG-4 part 2" |
| 40 |
assert c.type == "video" |
| 41 |
assert c.id in (12, 13) |
| 42 |
assert c.is_encoder |
| 43 |
assert not c.is_decoder |
| 44 |
assert c.delay |
| 45 |
|
| 46 |
assert c.audio_formats is None and c.audio_rates is None |
| 47 |
|
| 48 |
formats = c.video_formats |
| 49 |
assert formats |
| 50 |
assert isinstance(formats[0], VideoFormat) |
| 51 |
assert any(f.name == "yuv420p" for f in formats) |
| 52 |
assert c.frame_rates is None |
| 53 |
|
| 54 |
|
| 55 |
def test_codec_opus_decoder() -> None: |
| 56 |
c = Codec("opus") |
| 57 |
|
| 58 |
assert c.name == "opus" |
| 59 |
assert c.long_name == "Opus" |
| 60 |
assert c.type == "audio" |
| 61 |
assert c.is_decoder |
| 62 |
assert not c.is_encoder |
| 63 |
assert c.delay |
| 64 |
|
| 65 |
assert c.audio_formats is None and c.audio_rates is None |
| 66 |
assert c.video_formats is None and c.frame_rates is None |
| 67 |
|
| 68 |
|
| 69 |
def test_codec_opus_encoder() -> None: |
| 70 |
c = Codec("opus", "w") |
| 71 |
assert c.name in ("opus", "libopus") |
| 72 |
assert c.canonical_name == "opus" |
| 73 |
assert c.long_name in ("Opus", "libopus Opus") |
| 74 |
assert c.type == "audio" |
| 75 |
assert c.is_encoder |
| 76 |
assert not c.is_decoder |
| 77 |
assert c.delay |
| 78 |
|
| 79 |
# audio |
| 80 |
formats = c.audio_formats |
| 81 |
assert formats |
| 82 |
assert isinstance(formats[0], AudioFormat) |
| 83 |
assert any(f.name in ("flt", "fltp") for f in formats) |
| 84 |
|
| 85 |
assert c.audio_rates is not None |
| 86 |
assert 48000 in c.audio_rates |
| 87 |
|
| 88 |
assert c.video_formats is None and c.frame_rates is None |
| 89 |
|
| 90 |
|
| 91 |
def test_codecs_available() -> None: |
| 92 |
assert codecs_available |
| 93 |
|
| 94 |
|
| 95 |
def test_find_best_pix_fmt_of_list_empty() -> None: |
| 96 |
best, loss = find_best_pix_fmt_of_list([], "rgb24") |
| 97 |
assert best is None |
| 98 |
assert loss == 0 |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.parametrize( |
| 102 |
"pix_fmts,src_pix_fmt,expected_best", |
| 103 |
[ |
| 104 |
(["rgb24", "yuv420p"], "rgb24", "rgb24"), |
| 105 |
(["rgb24"], "yuv420p", "rgb24"), |
| 106 |
(["yuv420p"], "rgb24", "yuv420p"), |
| 107 |
([VideoFormat("yuv420p")], VideoFormat("rgb24"), "yuv420p"), |
| 108 |
( |
| 109 |
["yuv420p", "yuv444p", "gray", "rgb24", "rgba", "bgra", "yuyv422"], |
| 110 |
"rgba", |
| 111 |
"rgba", |
| 112 |
), |
| 113 |
], |
| 114 |
) |
| 115 |
def test_find_best_pix_fmt_of_list_best(pix_fmts, src_pix_fmt, expected_best) -> None: |
| 116 |
best, loss = find_best_pix_fmt_of_list(pix_fmts, src_pix_fmt) |
| 117 |
assert best is not None |
| 118 |
assert best.name == expected_best |
| 119 |
assert isinstance(loss, int) |
| 120 |
|
| 121 |
|
| 122 |
@pytest.mark.parametrize( |
| 123 |
"pix_fmts,src_pix_fmt", |
| 124 |
[ |
| 125 |
(["__unknown_pix_fmt"], "rgb24"), |
| 126 |
(["rgb24"], "__unknown_pix_fmt"), |
| 127 |
], |
| 128 |
) |
| 129 |
def test_find_best_pix_fmt_of_list_unknown_pix_fmt(pix_fmts, src_pix_fmt) -> None: |
| 130 |
with pytest.raises(ValueError, match="not a pixel format"): |
| 131 |
find_best_pix_fmt_of_list(pix_fmts, src_pix_fmt) |
| 132 |
|
| 133 |
|
| 134 |
@pytest.mark.parametrize( |
| 135 |
"pix_fmts,src_pix_fmt", |
| 136 |
[ |
| 137 |
(["rgb24", "bgr24", "gray", "yuv420p", "yuv444p", "yuyv422"], "nv12"), |
| 138 |
(["yuv420p", "yuv444p", "gray", "yuv420p"], "rgb24"), |
| 139 |
(["rgb24", "rgba", "bgra", "rgb24", "gray"], "yuv420p"), |
| 140 |
], |
| 141 |
) |
| 142 |
def test_find_best_pix_fmt_of_list_picks_from_list(pix_fmts, src_pix_fmt) -> None: |
| 143 |
best, loss = find_best_pix_fmt_of_list(pix_fmts, src_pix_fmt) |
| 144 |
assert best is not None |
| 145 |
assert best.name in set(pix_fmts) |
| 146 |
assert isinstance(loss, int) |
| 147 |
|
| 148 |
|
| 149 |
def test_find_best_pix_fmt_of_list_alpha_loss_flagged_when_used() -> None: |
| 150 |
best, loss = find_best_pix_fmt_of_list(["rgb24"], "rgba", has_alpha=True) |
| 151 |
assert best is not None |
| 152 |
assert best.name == "rgb24" |
| 153 |
assert loss != 0 |