File: test_codec.py

package info (click to toggle)
python-av 16.1.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,728 kB
  • sloc: python: 8,059; sh: 181; ansic: 174; makefile: 140
file content (153 lines) | stat: -rw-r--r-- 4,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pytest

from av import AudioFormat, Codec, VideoFormat, codecs_available
from av.codec import find_best_pix_fmt_of_list
from av.codec.codec import UnknownCodecError


def test_codec_bogus() -> None:
    with pytest.raises(UnknownCodecError):
        Codec("bogus123")
    with pytest.raises(UnknownCodecError):
        Codec("bogus123", "w")


def test_codec_mpeg4_decoder() -> None:
    c = Codec("mpeg4")

    assert c.name == "mpeg4"
    assert c.long_name == "MPEG-4 part 2"
    assert c.type == "video"
    assert c.id in (12, 13)
    assert c.is_decoder
    assert not c.is_encoder
    assert c.delay

    assert c.audio_formats is None and c.audio_rates is None

    # formats = c.video_formats
    # assert formats
    # assert isinstance(formats[0], VideoFormat)
    # assert any(f.name == "yuv420p" for f in formats)

    assert c.frame_rates is None


def test_codec_mpeg4_encoder() -> None:
    c = Codec("mpeg4", "w")
    assert c.name == "mpeg4"
    assert c.long_name == "MPEG-4 part 2"
    assert c.type == "video"
    assert c.id in (12, 13)
    assert c.is_encoder
    assert not c.is_decoder
    assert c.delay

    assert c.audio_formats is None and c.audio_rates is None

    formats = c.video_formats
    assert formats
    assert isinstance(formats[0], VideoFormat)
    assert any(f.name == "yuv420p" for f in formats)
    assert c.frame_rates is None


def test_codec_opus_decoder() -> None:
    c = Codec("opus")

    assert c.name == "opus"
    assert c.long_name == "Opus"
    assert c.type == "audio"
    assert c.is_decoder
    assert not c.is_encoder
    assert c.delay

    assert c.audio_formats is None and c.audio_rates is None
    assert c.video_formats is None and c.frame_rates is None


def test_codec_opus_encoder() -> None:
    c = Codec("opus", "w")
    assert c.name in ("opus", "libopus")
    assert c.canonical_name == "opus"
    assert c.long_name in ("Opus", "libopus Opus")
    assert c.type == "audio"
    assert c.is_encoder
    assert not c.is_decoder
    assert c.delay

    # audio
    formats = c.audio_formats
    assert formats
    assert isinstance(formats[0], AudioFormat)
    assert any(f.name in ("flt", "fltp") for f in formats)

    assert c.audio_rates is not None
    assert 48000 in c.audio_rates

    assert c.video_formats is None and c.frame_rates is None


def test_codecs_available() -> None:
    assert codecs_available


def test_find_best_pix_fmt_of_list_empty() -> None:
    best, loss = find_best_pix_fmt_of_list([], "rgb24")
    assert best is None
    assert loss == 0


@pytest.mark.parametrize(
    "pix_fmts,src_pix_fmt,expected_best",
    [
        (["rgb24", "yuv420p"], "rgb24", "rgb24"),
        (["rgb24"], "yuv420p", "rgb24"),
        (["yuv420p"], "rgb24", "yuv420p"),
        ([VideoFormat("yuv420p")], VideoFormat("rgb24"), "yuv420p"),
        (
            ["yuv420p", "yuv444p", "gray", "rgb24", "rgba", "bgra", "yuyv422"],
            "rgba",
            "rgba",
        ),
    ],
)
def test_find_best_pix_fmt_of_list_best(pix_fmts, src_pix_fmt, expected_best) -> None:
    best, loss = find_best_pix_fmt_of_list(pix_fmts, src_pix_fmt)
    assert best is not None
    assert best.name == expected_best
    assert isinstance(loss, int)


@pytest.mark.parametrize(
    "pix_fmts,src_pix_fmt",
    [
        (["__unknown_pix_fmt"], "rgb24"),
        (["rgb24"], "__unknown_pix_fmt"),
    ],
)
def test_find_best_pix_fmt_of_list_unknown_pix_fmt(pix_fmts, src_pix_fmt) -> None:
    with pytest.raises(ValueError, match="not a pixel format"):
        find_best_pix_fmt_of_list(pix_fmts, src_pix_fmt)


@pytest.mark.parametrize(
    "pix_fmts,src_pix_fmt",
    [
        (["rgb24", "bgr24", "gray", "yuv420p", "yuv444p", "yuyv422"], "nv12"),
        (["yuv420p", "yuv444p", "gray", "yuv420p"], "rgb24"),
        (["rgb24", "rgba", "bgra", "rgb24", "gray"], "yuv420p"),
    ],
)
def test_find_best_pix_fmt_of_list_picks_from_list(pix_fmts, src_pix_fmt) -> None:
    best, loss = find_best_pix_fmt_of_list(pix_fmts, src_pix_fmt)
    assert best is not None
    assert best.name in set(pix_fmts)
    assert isinstance(loss, int)


def test_find_best_pix_fmt_of_list_alpha_loss_flagged_when_used() -> None:
    best, loss = find_best_pix_fmt_of_list(["rgb24"], "rgba", has_alpha=True)
    assert best is not None
    assert best.name == "rgb24"
    assert loss != 0