| 1 |
import functools |
| 2 |
import os |
| 3 |
import pathlib |
| 4 |
from fractions import Fraction |
| 5 |
from typing import cast |
| 6 |
|
| 7 |
import numpy as np |
| 8 |
import pytest |
| 9 |
|
| 10 |
import av |
| 11 |
from av.sidedata.encparams import VideoEncParams |
| 12 |
from av.subtitles.subtitle import SubtitleSet |
| 13 |
|
| 14 |
from .common import TestCase, fate_suite |
| 15 |
|
| 16 |
|
| 17 |
@functools.cache |
| 18 |
def make_h264_test_video(path: str) -> None: |
| 19 |
"""Generates a black H264 test video with two streams for testing hardware decoding.""" |
| 20 |
|
| 21 |
# We generate a file here that's designed to be as compatible as possible with hardware |
| 22 |
# encoders. Hardware encoders are sometimes very picky and the errors we get are often |
| 23 |
# opaque, so there is nothing much we (PyAV) can do. The user needs to figure that out |
| 24 |
# if they want to use hwaccel. We only want to test the PyAV plumbing here. |
| 25 |
# Our video is H264, 1280x720p (note that some decoders have a minimum resolution limit), 24fps, |
| 26 |
# 8-bit yuv420p. |
| 27 |
pathlib.Path(path).parent.mkdir(parents=True, exist_ok=True) |
| 28 |
output_container = av.open(path, "w") |
| 29 |
|
| 30 |
streams = [] |
| 31 |
for _ in range(2): |
| 32 |
stream = output_container.add_stream("libx264", rate=24) |
| 33 |
assert isinstance(stream, av.VideoStream) |
| 34 |
stream.width = 1280 |
| 35 |
stream.height = 720 |
| 36 |
stream.pix_fmt = "yuv420p" |
| 37 |
streams.append(stream) |
| 38 |
|
| 39 |
for _ in range(24): |
| 40 |
frame = av.VideoFrame.from_ndarray( |
| 41 |
np.zeros((720, 1280, 3), dtype=np.uint8), format="rgb24" |
| 42 |
) |
| 43 |
for stream in streams: |
| 44 |
for packet in stream.encode(frame): |
| 45 |
output_container.mux(packet) |
| 46 |
|
| 47 |
for stream in streams: |
| 48 |
for packet in stream.encode(): |
| 49 |
output_container.mux(packet) |
| 50 |
|
| 51 |
output_container.close() |
| 52 |
|
| 53 |
|
| 54 |
class TestDecode(TestCase): |
| 55 |
def test_decoded_video_frame_count(self) -> None: |
| 56 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 57 |
video_stream = next(s for s in container.streams if s.type == "video") |
| 58 |
|
| 59 |
assert video_stream is container.streams.video[0] |
| 60 |
|
| 61 |
frame_count = 0 |
| 62 |
for frame in container.decode(video_stream): |
| 63 |
frame_count += 1 |
| 64 |
|
| 65 |
assert frame_count == video_stream.frames |
| 66 |
|
| 67 |
def test_decode_audio_corrupt(self) -> None: |
| 68 |
# write an empty file |
| 69 |
path = self.sandboxed("empty.flac") |
| 70 |
with open(path, "wb"): |
| 71 |
pass |
| 72 |
|
| 73 |
packet_count = 0 |
| 74 |
frame_count = 0 |
| 75 |
audio_frame: av.AudioFrame | None = None |
| 76 |
|
| 77 |
with av.open(path) as container: |
| 78 |
for packet in container.demux(audio=0): |
| 79 |
for frame in packet.decode(): |
| 80 |
frame_count += 1 |
| 81 |
audio_frame = frame |
| 82 |
packet_count += 1 |
| 83 |
|
| 84 |
assert packet_count == 1 |
| 85 |
assert frame_count == 0 |
| 86 |
|
| 87 |
def test_decode_audio_sample_count(self) -> None: |
| 88 |
container = av.open(fate_suite("audio-reference/chorusnoise_2ch_44kHz_s16.wav")) |
| 89 |
audio_stream = next(s for s in container.streams if s.type == "audio") |
| 90 |
|
| 91 |
assert audio_stream is container.streams.audio[0] |
| 92 |
assert isinstance(audio_stream, av.AudioStream) |
| 93 |
|
| 94 |
sample_count = 0 |
| 95 |
|
| 96 |
for frame in container.decode(audio_stream): |
| 97 |
sample_count += frame.samples |
| 98 |
|
| 99 |
assert audio_stream.duration is not None |
| 100 |
assert audio_stream.time_base is not None |
| 101 |
total_samples = ( |
| 102 |
audio_stream.duration |
| 103 |
* audio_stream.sample_rate.numerator |
| 104 |
/ audio_stream.time_base.denominator |
| 105 |
) |
| 106 |
assert sample_count == total_samples |
| 107 |
|
| 108 |
def test_decoded_time_base(self) -> None: |
| 109 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 110 |
stream = container.streams.video[0] |
| 111 |
|
| 112 |
assert stream.time_base == Fraction(1, 25) |
| 113 |
|
| 114 |
video_frame: av.VideoFrame | None = None |
| 115 |
|
| 116 |
for packet in container.demux(stream): |
| 117 |
for frame in packet.decode(): |
| 118 |
video_frame = frame |
| 119 |
assert not isinstance(frame, SubtitleSet) |
| 120 |
assert packet.time_base == frame.time_base |
| 121 |
assert stream.time_base == frame.time_base |
| 122 |
return |
| 123 |
|
| 124 |
def test_decoded_motion_vectors(self) -> None: |
| 125 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 126 |
stream = container.streams.video[0] |
| 127 |
codec_context = stream.codec_context |
| 128 |
codec_context.options = {"flags2": "+export_mvs"} |
| 129 |
|
| 130 |
for frame in container.decode(stream): |
| 131 |
vectors = frame.side_data.get("MOTION_VECTORS") |
| 132 |
if frame.key_frame: |
| 133 |
# Key frame don't have motion vectors |
| 134 |
assert vectors is None |
| 135 |
else: |
| 136 |
assert vectors is not None and len(vectors) > 0 |
| 137 |
return |
| 138 |
|
| 139 |
def test_decoded_motion_vectors_no_flag(self) -> None: |
| 140 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 141 |
stream = container.streams.video[0] |
| 142 |
|
| 143 |
for frame in container.decode(stream): |
| 144 |
vectors = frame.side_data.get("MOTION_VECTORS") |
| 145 |
if not frame.key_frame: |
| 146 |
assert vectors is None |
| 147 |
return |
| 148 |
|
| 149 |
def test_decoded_video_enc_params(self) -> None: |
| 150 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 151 |
stream = container.streams.video[0] |
| 152 |
stream.codec_context.options = {"export_side_data": "venc_params"} |
| 153 |
|
| 154 |
for frame in container.decode(stream): |
| 155 |
video_enc_params = cast( |
| 156 |
VideoEncParams, |
| 157 |
frame.side_data.get("VIDEO_ENC_PARAMS"), |
| 158 |
) |
| 159 |
assert video_enc_params is not None |
| 160 |
assert video_enc_params.nb_blocks == 40 * 24 |
| 161 |
|
| 162 |
first_block = video_enc_params.block_params(0) |
| 163 |
assert video_enc_params.qp + first_block.delta_qp == 29 |
| 164 |
return |
| 165 |
|
| 166 |
def test_decoded_video_enc_params_no_flag(self) -> None: |
| 167 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 168 |
stream = container.streams.video[0] |
| 169 |
# When no additional flag is given, there should be no side data with the video encoding params |
| 170 |
|
| 171 |
for frame in container.decode(stream): |
| 172 |
video_enc_params = frame.side_data.get("VIDEO_ENC_PARAMS") |
| 173 |
assert video_enc_params is None |
| 174 |
|
| 175 |
def test_decode_video_corrupt(self) -> None: |
| 176 |
# write an empty file |
| 177 |
path = self.sandboxed("empty.h264") |
| 178 |
with open(path, "wb"): |
| 179 |
pass |
| 180 |
|
| 181 |
packet_count = 0 |
| 182 |
frame_count = 0 |
| 183 |
|
| 184 |
with av.open(path) as container: |
| 185 |
for packet in container.demux(video=0): |
| 186 |
for frame in packet.decode(): |
| 187 |
frame_count += 1 |
| 188 |
packet_count += 1 |
| 189 |
|
| 190 |
assert packet_count == 1 |
| 191 |
assert frame_count == 0 |
| 192 |
|
| 193 |
def test_decode_close_then_use(self) -> None: |
| 194 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 195 |
container.close() |
| 196 |
|
| 197 |
# Check accessing every attribute either works or raises |
| 198 |
# an `AssertionError`. |
| 199 |
for attr in dir(container): |
| 200 |
with self.subTest(attr=attr): |
| 201 |
try: |
| 202 |
getattr(container, attr) |
| 203 |
except AssertionError: |
| 204 |
pass |
| 205 |
|
| 206 |
def test_flush_decoded_video_frame_count(self) -> None: |
| 207 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 208 |
video_stream = container.streams.video[0] |
| 209 |
|
| 210 |
# Decode the first GOP, which requires a flush to get all frames |
| 211 |
have_keyframe = False |
| 212 |
input_count = 0 |
| 213 |
output_count = 0 |
| 214 |
|
| 215 |
for packet in container.demux(video_stream): |
| 216 |
if packet.is_keyframe: |
| 217 |
if have_keyframe: |
| 218 |
break |
| 219 |
have_keyframe = True |
| 220 |
|
| 221 |
input_count += 1 |
| 222 |
|
| 223 |
for frame in video_stream.decode(packet): |
| 224 |
output_count += 1 |
| 225 |
|
| 226 |
# Check the test works as expected and requires a flush |
| 227 |
assert output_count < input_count |
| 228 |
|
| 229 |
for frame in video_stream.decode(None): |
| 230 |
# The Frame._time_base is not set by PyAV |
| 231 |
assert frame.time_base is None |
| 232 |
output_count += 1 |
| 233 |
|
| 234 |
assert output_count == input_count |
| 235 |
|
| 236 |
def test_no_side_data(self) -> None: |
| 237 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 238 |
frame = next(container.decode(video=0)) |
| 239 |
assert frame.rotation == 0 |
| 240 |
|
| 241 |
def test_side_data(self) -> None: |
| 242 |
container = av.open(fate_suite("mov/displaymatrix.mov")) |
| 243 |
frame = next(container.decode(video=0)) |
| 244 |
assert frame.rotation == -90 |
| 245 |
|
| 246 |
def test_hardware_decode(self) -> None: |
| 247 |
hwdevices_available = av.codec.hwaccel.hwdevices_available() |
| 248 |
if "HWACCEL_DEVICE_TYPE" not in os.environ: |
| 249 |
pytest.skip( |
| 250 |
"Set the HWACCEL_DEVICE_TYPE to run this test. " |
| 251 |
f"Options are {' '.join(hwdevices_available)}" |
| 252 |
) |
| 253 |
|
| 254 |
HWACCEL_DEVICE_TYPE = os.environ["HWACCEL_DEVICE_TYPE"] |
| 255 |
assert HWACCEL_DEVICE_TYPE in hwdevices_available, ( |
| 256 |
f"{HWACCEL_DEVICE_TYPE} not available" |
| 257 |
) |
| 258 |
|
| 259 |
test_video_path = "tests/assets/black.mp4" |
| 260 |
make_h264_test_video(test_video_path) |
| 261 |
|
| 262 |
hwaccel = av.codec.hwaccel.HWAccel( |
| 263 |
device_type=HWACCEL_DEVICE_TYPE, allow_software_fallback=False |
| 264 |
) |
| 265 |
|
| 266 |
container = av.open(test_video_path, hwaccel=hwaccel) |
| 267 |
video_stream = container.streams.video[0] |
| 268 |
assert video_stream.codec_context.is_hwaccel |
| 269 |
|
| 270 |
frame_count = 0 |
| 271 |
for frame in container.decode(video_stream): |
| 272 |
frame_count += 1 |
| 273 |
|
| 274 |
assert frame_count == video_stream.frames |