| 1 |
import fractions |
| 2 |
import io |
| 3 |
import json |
| 4 |
import struct |
| 5 |
from typing import get_args |
| 6 |
from unittest import SkipTest |
| 7 |
|
| 8 |
import numpy |
| 9 |
|
| 10 |
import av |
| 11 |
|
| 12 |
from .common import fate_suite, sandboxed |
| 13 |
|
| 14 |
|
| 15 |
class TestDataStreams: |
| 16 |
def generate_container_with_data_packets(self): |
| 17 |
file = io.BytesIO() |
| 18 |
packet_datas_expected = dict[fractions.Fraction, bytes]() |
| 19 |
|
| 20 |
container = av.open(file, format="mp4", mode="w") |
| 21 |
stream = container.add_data_stream("bin_data") |
| 22 |
vstream = container.add_stream("h264") |
| 23 |
|
| 24 |
for i in range(10): |
| 25 |
packet_data = json.dumps("This may not work", ensure_ascii=False).encode( |
| 26 |
"utf-8" |
| 27 |
) |
| 28 |
if i % 2 == 0: |
| 29 |
packet_data = b"This works fine" |
| 30 |
packet = av.Packet(packet_data) |
| 31 |
vframe = av.VideoFrame.from_ndarray( |
| 32 |
numpy.random.randint(0, 255, size=(120, 120, 3), dtype=numpy.uint8) |
| 33 |
) |
| 34 |
packet.pts = i |
| 35 |
packet.dts = i |
| 36 |
packet.duration = 1 |
| 37 |
packet.stream = stream |
| 38 |
container.mux(packet) |
| 39 |
container.mux(vstream.encode(vframe)) |
| 40 |
# NOTE test passes if this is used |
| 41 |
# packet_datas_expected[packet.pts * packet.time_base] = bytes(packet_data) |
| 42 |
|
| 43 |
container.close() |
| 44 |
|
| 45 |
container = av.open(file, format="mp4", mode="r") |
| 46 |
return container, packet_datas_expected |
| 47 |
|
| 48 |
def test_data_packet_bytes(self): |
| 49 |
container, packet_datas_expected = self.generate_container_with_data_packets() |
| 50 |
|
| 51 |
packet_datas = dict[fractions.Fraction, bytes]() |
| 52 |
for packet in container.demux(container.streams.data[0]): |
| 53 |
if packet.pts is None: |
| 54 |
continue |
| 55 |
assert bytes(packet) in ( |
| 56 |
b"This works fine", |
| 57 |
json.dumps("This may not work", ensure_ascii=False).encode("utf-8"), |
| 58 |
), bytes(packet) |
| 59 |
# NOTE test passes if this is used |
| 60 |
# packet_datas[packet.pts * packet.time_base] = bytes(packet) |
| 61 |
|
| 62 |
assert packet_datas == packet_datas_expected |
| 63 |
|
| 64 |
|
| 65 |
class TestProperties: |
| 66 |
def test_is_keyframe(self) -> None: |
| 67 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 68 |
stream = container.streams.video[0] |
| 69 |
for i, packet in enumerate(container.demux(stream)): |
| 70 |
if i in (0, 21, 45, 69, 93, 117): |
| 71 |
assert packet.is_keyframe |
| 72 |
else: |
| 73 |
assert not packet.is_keyframe |
| 74 |
|
| 75 |
def test_is_corrupt(self) -> None: |
| 76 |
with av.open(fate_suite("mov/white_zombie_scrunch-part.mov")) as container: |
| 77 |
stream = container.streams.video[0] |
| 78 |
for i, packet in enumerate(container.demux(stream)): |
| 79 |
if i == 65: |
| 80 |
assert packet.is_corrupt |
| 81 |
else: |
| 82 |
assert not packet.is_corrupt |
| 83 |
|
| 84 |
def test_is_discard(self) -> None: |
| 85 |
with av.open(fate_suite("mov/mov-1elist-ends-last-bframe.mov")) as container: |
| 86 |
stream = container.streams.video[0] |
| 87 |
for i, packet in enumerate(container.demux(stream)): |
| 88 |
if i == 46: |
| 89 |
assert packet.is_discard |
| 90 |
else: |
| 91 |
assert not packet.is_discard |
| 92 |
|
| 93 |
def test_is_disposable(self) -> None: |
| 94 |
with av.open(fate_suite("hap/HAPQA_NoSnappy_127x1.mov")) as container: |
| 95 |
stream = container.streams.video[0] |
| 96 |
for i, packet in enumerate(container.demux(stream)): |
| 97 |
if i == 0: |
| 98 |
assert packet.is_disposable |
| 99 |
else: |
| 100 |
assert not packet.is_disposable |
| 101 |
|
| 102 |
def test_set_duration(self) -> None: |
| 103 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 104 |
for packet in container.demux(): |
| 105 |
assert packet.duration is not None |
| 106 |
old_duration = packet.duration |
| 107 |
packet.duration += 10 |
| 108 |
|
| 109 |
assert packet.duration == old_duration + 10 |
| 110 |
|
| 111 |
|
| 112 |
class TestPacketSideData: |
| 113 |
def test_data_types(self) -> None: |
| 114 |
dtypes = get_args(av.packet.PktSideDataT) |
| 115 |
|
| 116 |
if av.ffmpeg_version_info.startswith("n") or av.ffmpeg_version_info.count( |
| 117 |
"." |
| 118 |
) not in (1, 2): |
| 119 |
raise SkipTest(f"Expect version to be SemVar: {av.ffmpeg_version_info}") |
| 120 |
|
| 121 |
ffmpeg_ver = [int(v) for v in av.ffmpeg_version_info.split(".", 2)[:2]] |
| 122 |
for dtype in dtypes: |
| 123 |
av_enum = av.packet.packet_sidedata_type_from_literal(dtype) |
| 124 |
assert dtype == av.packet.packet_sidedata_type_to_literal(av_enum) |
| 125 |
|
| 126 |
if (ffmpeg_ver[0] < 8 and dtype == "lcevc") or ( |
| 127 |
ffmpeg_ver[0] < 9 and dtype == "rtcp_sr" |
| 128 |
): |
| 129 |
break |
| 130 |
|
| 131 |
def test_iter(self) -> None: |
| 132 |
with av.open(fate_suite("h264/extradata-reload-multi-stsd.mov")) as container: |
| 133 |
for pkt in container.demux(): |
| 134 |
for sdata in pkt.iter_sidedata(): |
| 135 |
assert pkt.dts == 2 and sdata.data_type == "new_extradata" |
| 136 |
|
| 137 |
def test_palette(self) -> None: |
| 138 |
with av.open(fate_suite("h264/extradata-reload-multi-stsd.mov")) as container: |
| 139 |
iterpackets = container.demux() |
| 140 |
pkt = next(pkt for pkt in iterpackets if pkt.has_sidedata("new_extradata")) |
| 141 |
|
| 142 |
sdata = pkt.get_sidedata("new_extradata") |
| 143 |
assert sdata.data_type == "new_extradata" |
| 144 |
assert bool(sdata) |
| 145 |
assert sdata.data_size > 0 |
| 146 |
assert sdata.data_desc == "New Extradata" |
| 147 |
|
| 148 |
nxt = next(iterpackets) # has no palette |
| 149 |
|
| 150 |
assert not nxt.has_sidedata("new_extradata") |
| 151 |
|
| 152 |
sdata1 = nxt.get_sidedata("new_extradata") |
| 153 |
assert sdata1.data_type == "new_extradata" |
| 154 |
assert not bool(sdata1) |
| 155 |
assert sdata1.data_size == 0 |
| 156 |
|
| 157 |
nxt.set_sidedata(sdata, move=True) |
| 158 |
assert not bool(sdata) |
| 159 |
|
| 160 |
def test_buffer_protocol(self) -> None: |
| 161 |
with av.open(fate_suite("h264/extradata-reload-multi-stsd.mov")) as container: |
| 162 |
for pkt in container.demux(): |
| 163 |
if pkt.has_sidedata("new_extradata"): |
| 164 |
sdata = pkt.get_sidedata("new_extradata") |
| 165 |
|
| 166 |
raw = bytes(sdata) |
| 167 |
assert len(raw) == sdata.data_size > 0 |
| 168 |
assert sdata.buffer_size == sdata.data_size |
| 169 |
assert sdata.buffer_ptr != 0 |
| 170 |
assert bytes(memoryview(sdata)) == raw |
| 171 |
|
| 172 |
# Modify and verify changes stick |
| 173 |
sdata.update(b"\xde\xad\xbe\xef" + raw[4:]) |
| 174 |
assert bytes(sdata)[:4] == b"\xde\xad\xbe\xef" |
| 175 |
|
| 176 |
pkt.set_sidedata(sdata) |
| 177 |
assert ( |
| 178 |
bytes(pkt.get_sidedata("new_extradata"))[:4] |
| 179 |
== b"\xde\xad\xbe\xef" |
| 180 |
) |
| 181 |
return |
| 182 |
|
| 183 |
raise AssertionError("No packet with new_extradata side data found") |
| 184 |
|
| 185 |
def test_skip_samples_remux(self) -> None: |
| 186 |
# Source file has skip_end=706 on last packet. Setting to 0 should |
| 187 |
# result in 706 more decoded samples. And the file duration reported by |
| 188 |
# the container should also increase. |
| 189 |
output_path = sandboxed("skip_samples_modified.mkv") |
| 190 |
|
| 191 |
with av.open(fate_suite("mkv/codec_delay_opus.mkv")) as c: |
| 192 |
original_samples = sum(f.samples for f in c.decode(c.streams.audio[0])) |
| 193 |
|
| 194 |
with av.open(fate_suite("mkv/codec_delay_opus.mkv")) as inp: |
| 195 |
original_duration = inp.duration |
| 196 |
audio_stream = inp.streams.audio[0] |
| 197 |
with av.open(output_path, "w") as out: |
| 198 |
out_stream = out.add_stream_from_template(audio_stream) |
| 199 |
for pkt in inp.demux(audio_stream): |
| 200 |
if pkt.size == 0: |
| 201 |
continue |
| 202 |
if pkt.has_sidedata("skip_samples"): |
| 203 |
sdata = pkt.get_sidedata("skip_samples") |
| 204 |
raw = bytes(sdata) |
| 205 |
skip_end = struct.unpack("<I", raw[4:8])[0] |
| 206 |
if skip_end > 0: |
| 207 |
assert skip_end == 706 |
| 208 |
sdata.update(raw[:4] + struct.pack("<I", 0) + raw[8:]) |
| 209 |
pkt.set_sidedata(sdata) |
| 210 |
pkt.stream = out_stream |
| 211 |
out.mux(pkt) |
| 212 |
|
| 213 |
with av.open(output_path) as c: |
| 214 |
modified_samples = sum(f.samples for f in c.decode(c.streams.audio[0])) |
| 215 |
modified_duration = c.duration |
| 216 |
|
| 217 |
assert modified_samples - original_samples == 706 |
| 218 |
|
| 219 |
assert original_duration is not None |
| 220 |
assert modified_duration is not None |
| 221 |
assert modified_duration > original_duration |