| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import av |
| 6 |
from av import Packet |
| 7 |
from av.bitstream import BitStreamFilterContext, bitstream_filters_available |
| 8 |
|
| 9 |
from .common import fate_suite |
| 10 |
|
| 11 |
|
| 12 |
def is_annexb(packet: Packet | bytes | None) -> bool: |
| 13 |
if packet is None: |
| 14 |
return False |
| 15 |
|
| 16 |
data = bytes(packet) |
| 17 |
return data[:3] == b"\0\0\x01" or data[:4] == b"\0\0\0\x01" |
| 18 |
|
| 19 |
|
| 20 |
def test_filters_available() -> None: |
| 21 |
assert "h264_mp4toannexb" in bitstream_filters_available |
| 22 |
|
| 23 |
|
| 24 |
def test_filter_chomp() -> None: |
| 25 |
ctx = BitStreamFilterContext("chomp") |
| 26 |
|
| 27 |
src_packets: tuple[Packet, None] = (Packet(b"\x0012345\0\0\0"), None) |
| 28 |
assert bytes(src_packets[0]) == b"\x0012345\0\0\0" |
| 29 |
|
| 30 |
result_packets = [] |
| 31 |
for p in src_packets: |
| 32 |
result_packets.extend(ctx.filter(p)) |
| 33 |
|
| 34 |
assert len(result_packets) == 1 |
| 35 |
assert bytes(result_packets[0]) == b"\x0012345" |
| 36 |
|
| 37 |
|
| 38 |
def test_filter_setts() -> None: |
| 39 |
ctx = BitStreamFilterContext("setts=pts=N") |
| 40 |
|
| 41 |
ctx2 = BitStreamFilterContext(b"setts=pts=N") |
| 42 |
del ctx2 |
| 43 |
|
| 44 |
p1 = Packet(b"\0") |
| 45 |
p1.pts = 42 |
| 46 |
p2 = Packet(b"\0") |
| 47 |
p2.pts = 50 |
| 48 |
src_packets = [p1, p2, None] |
| 49 |
|
| 50 |
result_packets: list[Packet] = [] |
| 51 |
for p in src_packets: |
| 52 |
result_packets.extend(ctx.filter(p)) |
| 53 |
|
| 54 |
assert len(result_packets) == 2 |
| 55 |
assert result_packets[0].pts == 0 |
| 56 |
assert result_packets[1].pts == 1 |
| 57 |
|
| 58 |
|
| 59 |
def test_filter_h264_mp4toannexb() -> None: |
| 60 |
with av.open(fate_suite("h264/interlaced_crop.mp4"), "r") as container: |
| 61 |
stream = container.streams.video[0] |
| 62 |
ctx = BitStreamFilterContext("h264_mp4toannexb", stream) |
| 63 |
|
| 64 |
res_packets = [] |
| 65 |
for p in container.demux(stream): |
| 66 |
assert not is_annexb(p) |
| 67 |
res_packets.extend(ctx.filter(p)) |
| 68 |
|
| 69 |
assert len(res_packets) == stream.frames |
| 70 |
|
| 71 |
for p in res_packets: |
| 72 |
assert is_annexb(p) |
| 73 |
|
| 74 |
|
| 75 |
def test_filter_output_parameters() -> None: |
| 76 |
with av.open(fate_suite("h264/interlaced_crop.mp4"), "r") as container: |
| 77 |
stream = container.streams.video[0] |
| 78 |
|
| 79 |
assert not is_annexb(stream.codec_context.extradata) |
| 80 |
ctx = BitStreamFilterContext("h264_mp4toannexb", stream) |
| 81 |
assert not is_annexb(stream.codec_context.extradata) |
| 82 |
del ctx |
| 83 |
|
| 84 |
_ = BitStreamFilterContext("h264_mp4toannexb", stream, out_stream=stream) |
| 85 |
assert is_annexb(stream.codec_context.extradata) |
| 86 |
|
| 87 |
|
| 88 |
def test_filter_flush() -> None: |
| 89 |
with av.open(fate_suite("h264/interlaced_crop.mp4"), "r") as container: |
| 90 |
stream = container.streams.video[0] |
| 91 |
ctx = BitStreamFilterContext("h264_mp4toannexb", stream) |
| 92 |
|
| 93 |
res_packets = [] |
| 94 |
for p in container.demux(stream): |
| 95 |
res_packets.extend(ctx.filter(p)) |
| 96 |
assert len(res_packets) == stream.frames |
| 97 |
|
| 98 |
container.seek(0) |
| 99 |
# Without flushing, we expect to get an error: "A non-NULL packet sent after an EOF." |
| 100 |
with pytest.raises(av.ArgumentError): |
| 101 |
for p in container.demux(stream): |
| 102 |
ctx.filter(p) |
| 103 |
|
| 104 |
ctx.flush() |
| 105 |
container.seek(0) |
| 106 |
for p in container.demux(stream): |
| 107 |
res_packets.extend(ctx.filter(p)) |
| 108 |
|
| 109 |
assert len(res_packets) == stream.frames * 2 |