|
1 | 1 | import pytest |
2 | 2 |
|
3 | 3 | from av import AudioFormat, Codec, VideoFormat, codecs_available |
| 4 | +from av.codec import find_best_pix_fmt_of_list |
4 | 5 | from av.codec.codec import UnknownCodecError |
5 | 6 |
|
6 | 7 |
|
@@ -89,3 +90,64 @@ def test_codec_opus_encoder() -> None: |
89 | 90 |
|
90 | 91 | def test_codecs_available() -> None: |
91 | 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 |
0 commit comments