| 1 |
from fractions import Fraction |
| 2 |
|
| 3 |
import numpy |
| 4 |
import pytest |
| 5 |
|
| 6 |
import av |
| 7 |
from av import VideoFrame |
| 8 |
from av.video.frame import supported_np_pix_fmts |
| 9 |
from av.video.reformatter import Colorspace, Interpolation |
| 10 |
|
| 11 |
from .common import assertNdarraysEqual, fate_png, fate_suite |
| 12 |
|
| 13 |
|
| 14 |
def assertPixelValue16(plane, expected, byteorder: str) -> None: |
| 15 |
view = memoryview(plane) |
| 16 |
if byteorder == "big": |
| 17 |
assert view[0] == (expected >> 8 & 0xFF) |
| 18 |
assert view[1] == expected & 0xFF |
| 19 |
else: |
| 20 |
assert view[0] == expected & 0xFF |
| 21 |
assert view[1] == (expected >> 8 & 0xFF) |
| 22 |
|
| 23 |
|
| 24 |
def test_frame_duration_matches_packet() -> None: |
| 25 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 26 |
packet_durations = [ |
| 27 |
(p.pts, p.duration) for p in container.demux() if p.pts is not None |
| 28 |
] |
| 29 |
packet_durations.sort(key=lambda x: x[0]) |
| 30 |
|
| 31 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 32 |
frame_durations = [ |
| 33 |
(f.pts, f.duration) for f in container.decode(video=0) if f.pts is not None |
| 34 |
] |
| 35 |
frame_durations.sort(key=lambda x: x[0]) |
| 36 |
|
| 37 |
assert len(packet_durations) == len(frame_durations) |
| 38 |
assert all(pd[1] == fd[1] for pd, fd in zip(packet_durations, frame_durations)) |
| 39 |
|
| 40 |
|
| 41 |
def test_invalid_pixel_format() -> None: |
| 42 |
with pytest.raises(ValueError, match="not a pixel format: '__unknown_pix_fmt'"): |
| 43 |
VideoFrame(640, 480, "__unknown_pix_fmt") |
| 44 |
|
| 45 |
|
| 46 |
def test_null_constructor() -> None: |
| 47 |
frame = VideoFrame() |
| 48 |
assert frame.width == 0 |
| 49 |
assert frame.height == 0 |
| 50 |
assert frame.format.name == "yuv420p" |
| 51 |
|
| 52 |
|
| 53 |
def test_manual_yuv_constructor() -> None: |
| 54 |
frame = VideoFrame(640, 480, "yuv420p") |
| 55 |
assert frame.width == 640 |
| 56 |
assert frame.height == 480 |
| 57 |
assert frame.format.name == "yuv420p" |
| 58 |
|
| 59 |
|
| 60 |
def test_manual_rgb_constructor() -> None: |
| 61 |
frame = VideoFrame(640, 480, "rgb24") |
| 62 |
assert frame.width == 640 |
| 63 |
assert frame.height == 480 |
| 64 |
assert frame.format.name == "rgb24" |
| 65 |
|
| 66 |
|
| 67 |
def test_null_planes() -> None: |
| 68 |
frame = VideoFrame() # yuv420p |
| 69 |
assert len(frame.planes) == 0 |
| 70 |
|
| 71 |
|
| 72 |
def test_yuv420p_planes() -> None: |
| 73 |
frame = VideoFrame(640, 480, "yuv420p") |
| 74 |
assert len(frame.planes) == 3 |
| 75 |
assert frame.planes[0].width == 640 |
| 76 |
assert frame.planes[0].height == 480 |
| 77 |
assert frame.planes[0].line_size == 640 |
| 78 |
assert frame.planes[0].buffer_size == 640 * 480 |
| 79 |
for i in range(1, 3): |
| 80 |
assert frame.planes[i].width == 320 |
| 81 |
assert frame.planes[i].height == 240 |
| 82 |
assert frame.planes[i].line_size == 320 |
| 83 |
assert frame.planes[i].buffer_size == 320 * 240 |
| 84 |
|
| 85 |
|
| 86 |
def test_yuv420p_planes_align() -> None: |
| 87 |
# If we request 8-byte alignment for a width which is not a multiple of 8, |
| 88 |
# the line sizes are larger than the plane width. |
| 89 |
frame = VideoFrame(318, 238, "yuv420p") |
| 90 |
assert len(frame.planes) == 3 |
| 91 |
assert frame.planes[0].width == 318 |
| 92 |
assert frame.planes[0].height == 238 |
| 93 |
assert frame.planes[0].line_size == 320 |
| 94 |
assert frame.planes[0].buffer_size == 320 * 238 |
| 95 |
for i in range(1, 3): |
| 96 |
assert frame.planes[i].width == 159 |
| 97 |
assert frame.planes[i].height == 119 |
| 98 |
assert frame.planes[i].line_size == 160 |
| 99 |
assert frame.planes[i].buffer_size == 160 * 119 |
| 100 |
|
| 101 |
|
| 102 |
def test_rgb24_planes() -> None: |
| 103 |
frame = VideoFrame(640, 480, "rgb24") |
| 104 |
assert len(frame.planes) == 1 |
| 105 |
assert frame.planes[0].width == 640 |
| 106 |
assert frame.planes[0].height == 480 |
| 107 |
assert frame.planes[0].line_size == 640 * 3 |
| 108 |
assert frame.planes[0].buffer_size == 640 * 480 * 3 |
| 109 |
|
| 110 |
|
| 111 |
def test_memoryview_read() -> None: |
| 112 |
frame = VideoFrame(640, 480, "rgb24") |
| 113 |
frame.planes[0].update(b"01234" + (b"x" * (640 * 480 * 3 - 5))) |
| 114 |
mem = memoryview(frame.planes[0]) |
| 115 |
assert mem.ndim == 1 |
| 116 |
assert mem.shape == (640 * 480 * 3,) |
| 117 |
assert not mem.readonly |
| 118 |
assert mem[1] == 49 |
| 119 |
assert mem[:7] == b"01234xx" |
| 120 |
mem[1] = 46 |
| 121 |
assert mem[:7] == b"0.234xx" |
| 122 |
|
| 123 |
|
| 124 |
def test_opaque() -> None: |
| 125 |
frame = VideoFrame(640, 480, "rgb24") |
| 126 |
frame.opaque = 3 |
| 127 |
assert frame.opaque == 3 |
| 128 |
frame.opaque = "a" |
| 129 |
assert frame.opaque == "a" |
| 130 |
|
| 131 |
greeting = "Hello World!" |
| 132 |
frame.opaque = greeting |
| 133 |
assert frame.opaque is greeting |
| 134 |
|
| 135 |
frame.opaque = None |
| 136 |
assert frame.opaque is None |
| 137 |
|
| 138 |
|
| 139 |
def test_interpolation() -> None: |
| 140 |
container = av.open(fate_png()) |
| 141 |
for _ in container.decode(video=0): |
| 142 |
frame = _ |
| 143 |
break |
| 144 |
|
| 145 |
assert frame.width == 330 and frame.height == 330 |
| 146 |
|
| 147 |
img = frame.reformat(width=200, height=100, interpolation=Interpolation.BICUBIC) |
| 148 |
assert img.width == 200 and img.height == 100 |
| 149 |
|
| 150 |
img = frame.reformat(width=200, height=100, interpolation="BICUBIC") |
| 151 |
assert img.width == 200 and img.height == 100 |
| 152 |
|
| 153 |
img = frame.reformat( |
| 154 |
width=200, height=100, interpolation=int(Interpolation.BICUBIC) |
| 155 |
) |
| 156 |
assert img.width == 200 and img.height == 100 |
| 157 |
|
| 158 |
|
| 159 |
def test_basic_to_ndarray() -> None: |
| 160 |
array = VideoFrame(640, 480, "rgb24").to_ndarray() |
| 161 |
assert array.shape == (480, 640, 3) |
| 162 |
|
| 163 |
|
| 164 |
def _vflip(frame: VideoFrame) -> VideoFrame: |
| 165 |
"""Vertically flip a frame, which yields a bottom-up frame with a negative |
| 166 |
``line_size`` (the same layout DirectShow produces, see GH-2213).""" |
| 167 |
graph = av.filter.Graph() |
| 168 |
src = graph.add_buffer( |
| 169 |
template=None, |
| 170 |
width=frame.width, |
| 171 |
height=frame.height, |
| 172 |
format=frame.format, |
| 173 |
time_base=Fraction(1, 1000), |
| 174 |
) |
| 175 |
vflip = graph.add("vflip") |
| 176 |
sink = graph.add("buffersink") |
| 177 |
src.link_to(vflip) |
| 178 |
vflip.link_to(sink) |
| 179 |
graph.configure() |
| 180 |
graph.push(frame) |
| 181 |
out = graph.pull() |
| 182 |
assert isinstance(out, VideoFrame) |
| 183 |
return out |
| 184 |
|
| 185 |
|
| 186 |
@pytest.mark.parametrize("format", ["rgb24", "bgr24", "gray"]) |
| 187 |
def test_negative_linesize_to_ndarray(format: str) -> None: |
| 188 |
# Bottom-up packed frames have a negative line_size; to_ndarray() must read |
| 189 |
# them without crashing (GH-2213) and in the correct top-down order. |
| 190 |
height, width = 6, 4 |
| 191 |
if format == "gray": |
| 192 |
array = numpy.arange(height * width, dtype=numpy.uint8).reshape(height, width) |
| 193 |
else: |
| 194 |
array = numpy.zeros((height, width, 3), dtype=numpy.uint8) |
| 195 |
for row in range(height): |
| 196 |
array[row, :, :] = row * 10 |
| 197 |
|
| 198 |
frame = _vflip(VideoFrame.from_ndarray(array, format=format)) |
| 199 |
assert frame.planes[0].line_size < 0 |
| 200 |
|
| 201 |
result = frame.to_ndarray(format=format) |
| 202 |
assertNdarraysEqual(result, array[::-1]) |
| 203 |
# Fully materializing the array used to segfault on a bottom-up frame. |
| 204 |
assert result.copy().sum() == int(array.sum()) |
| 205 |
|
| 206 |
|
| 207 |
def test_ndarray_gray() -> None: |
| 208 |
array = numpy.random.randint(0, 256, size=(480, 640), dtype=numpy.uint8) |
| 209 |
for format in ("gray", "gray8"): |
| 210 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 211 |
assert format in supported_np_pix_fmts |
| 212 |
assert frame.width == 640 and frame.height == 480 |
| 213 |
assert frame.format.name == "gray" |
| 214 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 215 |
|
| 216 |
|
| 217 |
def test_ndarray_gray_align() -> None: |
| 218 |
array = numpy.random.randint(0, 256, size=(238, 318), dtype=numpy.uint8) |
| 219 |
for format in ("gray", "gray8"): |
| 220 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 221 |
assert format in supported_np_pix_fmts |
| 222 |
assert frame.width == 318 and frame.height == 238 |
| 223 |
assert frame.format.name == "gray" |
| 224 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 225 |
|
| 226 |
|
| 227 |
def test_ndarray_gray9be() -> None: |
| 228 |
array = numpy.random.randint(0, 512, size=(480, 640), dtype=numpy.uint16) |
| 229 |
frame = VideoFrame.from_ndarray(array, format="gray9be") |
| 230 |
assert frame.width == 640 and frame.height == 480 |
| 231 |
assert frame.format.name == "gray9be" |
| 232 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 233 |
|
| 234 |
# check endianness by examining value of first pixel |
| 235 |
assertPixelValue16(frame.planes[0], array[0][0], "big") |
| 236 |
|
| 237 |
|
| 238 |
def test_ndarray_gray9le() -> None: |
| 239 |
array = numpy.random.randint(0, 512, size=(480, 640), dtype=numpy.uint16) |
| 240 |
frame = VideoFrame.from_ndarray(array, format="gray9le") |
| 241 |
assert frame.width == 640 and frame.height == 480 |
| 242 |
assert frame.format.name == "gray9le" |
| 243 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 244 |
|
| 245 |
# check endianness by examining value of first pixel |
| 246 |
assertPixelValue16(frame.planes[0], array[0][0], "little") |
| 247 |
|
| 248 |
|
| 249 |
def test_ndarray_gray10be() -> None: |
| 250 |
array = numpy.random.randint(0, 1024, size=(480, 640), dtype=numpy.uint16) |
| 251 |
frame = VideoFrame.from_ndarray(array, format="gray10be") |
| 252 |
assert frame.width == 640 and frame.height == 480 |
| 253 |
assert frame.format.name == "gray10be" |
| 254 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 255 |
|
| 256 |
# check endianness by examining value of first pixel |
| 257 |
assertPixelValue16(frame.planes[0], array[0][0], "big") |
| 258 |
|
| 259 |
|
| 260 |
def test_ndarray_gray10le() -> None: |
| 261 |
array = numpy.random.randint(0, 1024, size=(480, 640), dtype=numpy.uint16) |
| 262 |
frame = VideoFrame.from_ndarray(array, format="gray10le") |
| 263 |
assert frame.width == 640 and frame.height == 480 |
| 264 |
assert frame.format.name == "gray10le" |
| 265 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 266 |
|
| 267 |
# check endianness by examining value of first pixel |
| 268 |
assertPixelValue16(frame.planes[0], array[0][0], "little") |
| 269 |
|
| 270 |
|
| 271 |
def test_ndarray_gray12be() -> None: |
| 272 |
array = numpy.random.randint(0, 4096, size=(480, 640), dtype=numpy.uint16) |
| 273 |
frame = VideoFrame.from_ndarray(array, format="gray12be") |
| 274 |
assert frame.width == 640 and frame.height == 480 |
| 275 |
assert frame.format.name == "gray12be" |
| 276 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 277 |
|
| 278 |
# check endianness by examining value of first pixel |
| 279 |
assertPixelValue16(frame.planes[0], array[0][0], "big") |
| 280 |
|
| 281 |
|
| 282 |
def test_ndarray_gray12le() -> None: |
| 283 |
array = numpy.random.randint(0, 4096, size=(480, 640), dtype=numpy.uint16) |
| 284 |
frame = VideoFrame.from_ndarray(array, format="gray12le") |
| 285 |
assert frame.width == 640 and frame.height == 480 |
| 286 |
assert frame.format.name == "gray12le" |
| 287 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 288 |
|
| 289 |
# check endianness by examining value of first pixel |
| 290 |
assertPixelValue16(frame.planes[0], array[0][0], "little") |
| 291 |
|
| 292 |
|
| 293 |
def test_ndarray_gray14be() -> None: |
| 294 |
array = numpy.random.randint(0, 16384, size=(480, 640), dtype=numpy.uint16) |
| 295 |
frame = VideoFrame.from_ndarray(array, format="gray14be") |
| 296 |
assert frame.width == 640 and frame.height == 480 |
| 297 |
assert frame.format.name == "gray14be" |
| 298 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 299 |
|
| 300 |
# check endianness by examining value of first pixel |
| 301 |
assertPixelValue16(frame.planes[0], array[0][0], "big") |
| 302 |
|
| 303 |
|
| 304 |
def test_ndarray_gray14le() -> None: |
| 305 |
array = numpy.random.randint(0, 16384, size=(480, 640), dtype=numpy.uint16) |
| 306 |
frame = VideoFrame.from_ndarray(array, format="gray14le") |
| 307 |
assert frame.width == 640 and frame.height == 480 |
| 308 |
assert frame.format.name == "gray14le" |
| 309 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 310 |
|
| 311 |
# check endianness by examining value of first pixel |
| 312 |
assertPixelValue16(frame.planes[0], array[0][0], "little") |
| 313 |
|
| 314 |
|
| 315 |
def test_ndarray_gray16be() -> None: |
| 316 |
array = numpy.random.randint(0, 65536, size=(480, 640), dtype=numpy.uint16) |
| 317 |
frame = VideoFrame.from_ndarray(array, format="gray16be") |
| 318 |
assert frame.width == 640 and frame.height == 480 |
| 319 |
assert frame.format.name == "gray16be" |
| 320 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 321 |
|
| 322 |
# check endianness by examining value of first pixel |
| 323 |
assertPixelValue16(frame.planes[0], array[0][0], "big") |
| 324 |
|
| 325 |
|
| 326 |
def test_ndarray_gray16le() -> None: |
| 327 |
array = numpy.random.randint(0, 65536, size=(480, 640), dtype=numpy.uint16) |
| 328 |
frame = VideoFrame.from_ndarray(array, format="gray16le") |
| 329 |
assert frame.width == 640 and frame.height == 480 |
| 330 |
assert frame.format.name == "gray16le" |
| 331 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 332 |
|
| 333 |
# check endianness by examining value of first pixel |
| 334 |
assertPixelValue16(frame.planes[0], array[0][0], "little") |
| 335 |
|
| 336 |
|
| 337 |
def test_ndarray_grayf32() -> None: |
| 338 |
array = numpy.random.random_sample(size=(480, 640)).astype(numpy.float32) |
| 339 |
for format in ("grayf32be", "grayf32le"): |
| 340 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 341 |
assert format in supported_np_pix_fmts |
| 342 |
assert frame.width == 640 and frame.height == 480 |
| 343 |
assert frame.format.name == format |
| 344 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 345 |
|
| 346 |
|
| 347 |
def test_ndarray_grayf32_align() -> None: |
| 348 |
array = numpy.random.random_sample(size=(238, 318)).astype(numpy.float32) |
| 349 |
for format in ("grayf32be", "grayf32le"): |
| 350 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 351 |
assert format in supported_np_pix_fmts |
| 352 |
assert frame.width == 318 and frame.height == 238 |
| 353 |
assert frame.format.name == format |
| 354 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 355 |
|
| 356 |
|
| 357 |
def test_ndarray_rgb() -> None: |
| 358 |
array = numpy.random.randint(0, 256, size=(480, 640, 3), dtype=numpy.uint8) |
| 359 |
for format in ("rgb24", "bgr24"): |
| 360 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 361 |
assert format in supported_np_pix_fmts |
| 362 |
assert frame.width == 640 and frame.height == 480 |
| 363 |
assert frame.format.name == format |
| 364 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 365 |
|
| 366 |
|
| 367 |
def test_ndarray_rgb_align() -> None: |
| 368 |
array = numpy.random.randint(0, 256, size=(238, 318, 3), dtype=numpy.uint8) |
| 369 |
for format in ("rgb24", "bgr24"): |
| 370 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 371 |
assert format in supported_np_pix_fmts |
| 372 |
assert frame.width == 318 and frame.height == 238 |
| 373 |
assert frame.format.name == format |
| 374 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 375 |
|
| 376 |
|
| 377 |
def test_ndarray_rgbf32() -> None: |
| 378 |
array = numpy.random.random_sample(size=(480, 640, 3)).astype(numpy.float32) |
| 379 |
for format in ("rgbf32be", "rgbf32le"): |
| 380 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 381 |
assert frame.width == 640 and frame.height == 480 |
| 382 |
assert frame.format.name == format |
| 383 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 384 |
|
| 385 |
|
| 386 |
def test_ndarray_rgba() -> None: |
| 387 |
array = numpy.random.randint(0, 256, size=(480, 640, 4), dtype=numpy.uint8) |
| 388 |
for format in ("argb", "rgba", "abgr", "bgra"): |
| 389 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 390 |
assert format in supported_np_pix_fmts |
| 391 |
assert frame.width == 640 and frame.height == 480 |
| 392 |
assert frame.format.name == format |
| 393 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 394 |
|
| 395 |
|
| 396 |
def test_ndarray_rgba_align() -> None: |
| 397 |
array = numpy.random.randint(0, 256, size=(238, 318, 4), dtype=numpy.uint8) |
| 398 |
for format in ("argb", "rgba", "abgr", "bgra"): |
| 399 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 400 |
assert format in supported_np_pix_fmts |
| 401 |
assert frame.width == 318 and frame.height == 238 |
| 402 |
assert frame.format.name == format |
| 403 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 404 |
|
| 405 |
|
| 406 |
def test_ndarray_bayer8() -> None: |
| 407 |
array = numpy.random.randint(0, 256, size=(480, 640), dtype=numpy.uint8) |
| 408 |
for format in ("bayer_bggr8", "bayer_gbrg8", "bayer_grbg8", "bayer_rggb8"): |
| 409 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 410 |
assert format in supported_np_pix_fmts |
| 411 |
assert frame.width == 640 and frame.height == 480 |
| 412 |
assert frame.format.name == format |
| 413 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 414 |
|
| 415 |
|
| 416 |
def test_ndarray_bayer16() -> None: |
| 417 |
array = numpy.random.randint(0, 65536, size=(480, 640), dtype=numpy.uint16) |
| 418 |
for format in ( |
| 419 |
"bayer_bggr16be", |
| 420 |
"bayer_bggr16le", |
| 421 |
"bayer_gbrg16be", |
| 422 |
"bayer_gbrg16le", |
| 423 |
"bayer_grbg16be", |
| 424 |
"bayer_grbg16le", |
| 425 |
"bayer_rggb16be", |
| 426 |
"bayer_rggb16le", |
| 427 |
): |
| 428 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 429 |
assert format in supported_np_pix_fmts |
| 430 |
assert frame.width == 640 and frame.height == 480 |
| 431 |
assert frame.format.name == format |
| 432 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 433 |
|
| 434 |
|
| 435 |
def test_ndarray_gbrap() -> None: |
| 436 |
array = numpy.random.randint(0, 256, size=(480, 640, 4), dtype=numpy.uint8) |
| 437 |
frame = VideoFrame.from_ndarray(array, format="gbrap") |
| 438 |
assert frame.width == 640 and frame.height == 480 |
| 439 |
assert frame.format.name == "gbrap" |
| 440 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 441 |
|
| 442 |
|
| 443 |
def test_ndarray_gbrap_align() -> None: |
| 444 |
array = numpy.random.randint(0, 256, size=(238, 318, 4), dtype=numpy.uint8) |
| 445 |
frame = VideoFrame.from_ndarray(array, format="gbrap") |
| 446 |
assert frame.width == 318 and frame.height == 238 |
| 447 |
assert frame.format.name == "gbrap" |
| 448 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 449 |
|
| 450 |
|
| 451 |
def test_ndarray_gbrap10() -> None: |
| 452 |
array = numpy.random.randint(0, 1024, size=(480, 640, 4), dtype=numpy.uint16) |
| 453 |
for format in ("gbrap10be", "gbrap10le"): |
| 454 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 455 |
assert format in supported_np_pix_fmts |
| 456 |
assert frame.width == 640 and frame.height == 480 |
| 457 |
assert frame.format.name == format |
| 458 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 459 |
|
| 460 |
|
| 461 |
def test_ndarray_gbrap10_align() -> None: |
| 462 |
array = numpy.random.randint(0, 1024, size=(238, 318, 4), dtype=numpy.uint16) |
| 463 |
for format in ("gbrap10be", "gbrap10le"): |
| 464 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 465 |
assert format in supported_np_pix_fmts |
| 466 |
assert frame.width == 318 and frame.height == 238 |
| 467 |
assert frame.format.name == format |
| 468 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 469 |
|
| 470 |
|
| 471 |
def test_ndarray_gbrap12() -> None: |
| 472 |
array = numpy.random.randint(0, 4096, size=(480, 640, 4), dtype=numpy.uint16) |
| 473 |
for format in ("gbrap12be", "gbrap12le"): |
| 474 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 475 |
assert format in supported_np_pix_fmts |
| 476 |
assert frame.width == 640 and frame.height == 480 |
| 477 |
assert frame.format.name == format |
| 478 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 479 |
|
| 480 |
|
| 481 |
def test_ndarray_gbrap12_align() -> None: |
| 482 |
array = numpy.random.randint(0, 4096, size=(238, 318, 4), dtype=numpy.uint16) |
| 483 |
for format in ("gbrap12be", "gbrap12le"): |
| 484 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 485 |
assert format in supported_np_pix_fmts |
| 486 |
assert frame.width == 318 and frame.height == 238 |
| 487 |
assert frame.format.name == format |
| 488 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 489 |
|
| 490 |
|
| 491 |
def test_ndarray_gbrap14() -> None: |
| 492 |
array = numpy.random.randint(0, 16384, size=(480, 640, 4), dtype=numpy.uint16) |
| 493 |
for format in ("gbrap14be", "gbrap14le"): |
| 494 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 495 |
assert format in supported_np_pix_fmts |
| 496 |
assert frame.width == 640 and frame.height == 480 |
| 497 |
assert frame.format.name == format |
| 498 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 499 |
|
| 500 |
|
| 501 |
def test_ndarray_gbrap14_align() -> None: |
| 502 |
array = numpy.random.randint(0, 16384, size=(238, 318, 4), dtype=numpy.uint16) |
| 503 |
for format in ("gbrap14be", "gbrap14le"): |
| 504 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 505 |
assert format in supported_np_pix_fmts |
| 506 |
assert frame.width == 318 and frame.height == 238 |
| 507 |
assert frame.format.name == format |
| 508 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 509 |
|
| 510 |
|
| 511 |
def test_ndarray_gbrap16() -> None: |
| 512 |
array = numpy.random.randint(0, 65536, size=(480, 640, 4), dtype=numpy.uint16) |
| 513 |
for format in ("gbrap16be", "gbrap16le"): |
| 514 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 515 |
assert format in supported_np_pix_fmts |
| 516 |
assert frame.width == 640 and frame.height == 480 |
| 517 |
assert frame.format.name == format |
| 518 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 519 |
|
| 520 |
|
| 521 |
def test_ndarray_gbrap16_align() -> None: |
| 522 |
array = numpy.random.randint(0, 65536, size=(238, 318, 4), dtype=numpy.uint16) |
| 523 |
for format in ("gbrap16be", "gbrap16le"): |
| 524 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 525 |
assert format in supported_np_pix_fmts |
| 526 |
assert frame.width == 318 and frame.height == 238 |
| 527 |
assert frame.format.name == format |
| 528 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 529 |
|
| 530 |
|
| 531 |
def test_ndarray_gbrapf32() -> None: |
| 532 |
array = numpy.random.random_sample(size=(480, 640, 4)).astype(numpy.float32) |
| 533 |
for format in ("gbrapf32be", "gbrapf32le"): |
| 534 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 535 |
assert frame.width == 640 and frame.height == 480 |
| 536 |
assert frame.format.name == format |
| 537 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 538 |
|
| 539 |
|
| 540 |
def test_ndarray_gbrapf32_align() -> None: |
| 541 |
array = numpy.random.random_sample(size=(238, 318, 4)).astype(numpy.float32) |
| 542 |
for format in ("gbrapf32be", "gbrapf32le"): |
| 543 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 544 |
assert frame.width == 318 and frame.height == 238 |
| 545 |
assert frame.format.name == format |
| 546 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 547 |
|
| 548 |
|
| 549 |
def test_ndarray_gbrp() -> None: |
| 550 |
array = numpy.random.randint(0, 256, size=(480, 640, 3), dtype=numpy.uint8) |
| 551 |
frame = VideoFrame.from_ndarray(array, format="gbrp") |
| 552 |
assert frame.width == 640 and frame.height == 480 |
| 553 |
assert frame.format.name == "gbrp" |
| 554 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 555 |
|
| 556 |
|
| 557 |
def test_ndarray_gbrp_align() -> None: |
| 558 |
array = numpy.random.randint(0, 256, size=(238, 318, 3), dtype=numpy.uint8) |
| 559 |
frame = VideoFrame.from_ndarray(array, format="gbrp") |
| 560 |
assert frame.width == 318 and frame.height == 238 |
| 561 |
assert frame.format.name == "gbrp" |
| 562 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 563 |
|
| 564 |
|
| 565 |
def test_ndarray_gbrp9() -> None: |
| 566 |
array = numpy.random.randint(0, 512, size=(480, 640, 3), dtype=numpy.uint16) |
| 567 |
for format in ("gbrp9be", "gbrp9le"): |
| 568 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 569 |
assert format in supported_np_pix_fmts |
| 570 |
assert frame.width == 640 and frame.height == 480 |
| 571 |
assert frame.format.name == format |
| 572 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 573 |
|
| 574 |
|
| 575 |
def test_ndarray_gbrp9_align() -> None: |
| 576 |
array = numpy.random.randint(0, 512, size=(238, 318, 3), dtype=numpy.uint16) |
| 577 |
for format in ("gbrp9be", "gbrp9le"): |
| 578 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 579 |
assert format in supported_np_pix_fmts |
| 580 |
assert frame.width == 318 and frame.height == 238 |
| 581 |
assert frame.format.name == format |
| 582 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 583 |
|
| 584 |
|
| 585 |
def test_ndarray_gbrp10() -> None: |
| 586 |
array = numpy.random.randint(0, 1024, size=(480, 640, 3), dtype=numpy.uint16) |
| 587 |
for format in ("gbrp10be", "gbrp10le"): |
| 588 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 589 |
assert format in supported_np_pix_fmts |
| 590 |
assert frame.width == 640 and frame.height == 480 |
| 591 |
assert frame.format.name == format |
| 592 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 593 |
|
| 594 |
|
| 595 |
def test_ndarray_gbrp10_align() -> None: |
| 596 |
array = numpy.random.randint(0, 1024, size=(238, 318, 3), dtype=numpy.uint16) |
| 597 |
for format in ("gbrp10be", "gbrp10le"): |
| 598 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 599 |
assert format in supported_np_pix_fmts |
| 600 |
assert frame.width == 318 and frame.height == 238 |
| 601 |
assert frame.format.name == format |
| 602 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 603 |
|
| 604 |
|
| 605 |
def test_ndarray_gbrp12() -> None: |
| 606 |
array = numpy.random.randint(0, 4096, size=(480, 640, 3), dtype=numpy.uint16) |
| 607 |
for format in ("gbrp12be", "gbrp12le"): |
| 608 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 609 |
assert format in supported_np_pix_fmts |
| 610 |
assert frame.width == 640 and frame.height == 480 |
| 611 |
assert frame.format.name == format |
| 612 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 613 |
|
| 614 |
|
| 615 |
def test_ndarray_gbrp12_align() -> None: |
| 616 |
array = numpy.random.randint(0, 4096, size=(238, 318, 3), dtype=numpy.uint16) |
| 617 |
for format in ("gbrp12be", "gbrp12le"): |
| 618 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 619 |
assert format in supported_np_pix_fmts |
| 620 |
assert frame.width == 318 and frame.height == 238 |
| 621 |
assert frame.format.name == format |
| 622 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 623 |
|
| 624 |
|
| 625 |
def test_ndarray_gbrp14() -> None: |
| 626 |
array = numpy.random.randint(0, 16384, size=(480, 640, 3), dtype=numpy.uint16) |
| 627 |
for format in ("gbrp14be", "gbrp14le"): |
| 628 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 629 |
assert format in supported_np_pix_fmts |
| 630 |
assert frame.width == 640 and frame.height == 480 |
| 631 |
assert frame.format.name == format |
| 632 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 633 |
|
| 634 |
|
| 635 |
def test_ndarray_gbrp14_align() -> None: |
| 636 |
array = numpy.random.randint(0, 16384, size=(238, 318, 3), dtype=numpy.uint16) |
| 637 |
for format in ("gbrp14be", "gbrp14le"): |
| 638 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 639 |
assert format in supported_np_pix_fmts |
| 640 |
assert frame.width == 318 and frame.height == 238 |
| 641 |
assert frame.format.name == format |
| 642 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 643 |
|
| 644 |
|
| 645 |
def test_ndarray_gbrp16() -> None: |
| 646 |
array = numpy.random.randint(0, 65536, size=(480, 640, 3), dtype=numpy.uint16) |
| 647 |
for format in ("gbrp16be", "gbrp16le"): |
| 648 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 649 |
assert format in supported_np_pix_fmts |
| 650 |
assert frame.width == 640 and frame.height == 480 |
| 651 |
assert frame.format.name == format |
| 652 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 653 |
|
| 654 |
|
| 655 |
def test_ndarray_gbrp16_align() -> None: |
| 656 |
array = numpy.random.randint(0, 65536, size=(238, 318, 3), dtype=numpy.uint16) |
| 657 |
for format in ("gbrp16be", "gbrp16le"): |
| 658 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 659 |
assert format in supported_np_pix_fmts |
| 660 |
assert frame.width == 318 and frame.height == 238 |
| 661 |
assert frame.format.name == format |
| 662 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 663 |
|
| 664 |
|
| 665 |
def test_ndarray_gbrpf32() -> None: |
| 666 |
array = numpy.random.random_sample(size=(480, 640, 3)).astype(numpy.float32) |
| 667 |
for format in ("gbrpf32be", "gbrpf32le"): |
| 668 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 669 |
assert frame.width == 640 and frame.height == 480 |
| 670 |
assert frame.format.name == format |
| 671 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 672 |
|
| 673 |
|
| 674 |
def test_ndarray_gbrpf32_align() -> None: |
| 675 |
array = numpy.random.random_sample(size=(238, 318, 3)).astype(numpy.float32) |
| 676 |
for format in ["gbrpf32be", "gbrpf32le"]: |
| 677 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 678 |
assert frame.width == 318 and frame.height == 238 |
| 679 |
assert frame.format.name == format |
| 680 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 681 |
|
| 682 |
|
| 683 |
def test_ndarray_yuv420p() -> None: |
| 684 |
array = numpy.random.randint(0, 256, size=(720, 640), dtype=numpy.uint8) |
| 685 |
frame = VideoFrame.from_ndarray(array, format="yuv420p") |
| 686 |
assert frame.width == 640 and frame.height == 480 |
| 687 |
assert frame.format.name == "yuv420p" |
| 688 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 689 |
|
| 690 |
|
| 691 |
def test_ndarray_yuv422p() -> None: |
| 692 |
array = numpy.random.randint(0, 256, size=(960, 640), dtype=numpy.uint8) |
| 693 |
frame = VideoFrame.from_ndarray(array, format="yuv422p") |
| 694 |
assert frame.width == 640 and frame.height == 480 |
| 695 |
assert frame.format.name == "yuv422p" |
| 696 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 697 |
|
| 698 |
|
| 699 |
def test_ndarray_yuv420p_align() -> None: |
| 700 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 701 |
frame = VideoFrame.from_ndarray(array, format="yuv420p") |
| 702 |
assert frame.width == 318 and frame.height == 238 |
| 703 |
assert frame.format.name == "yuv420p" |
| 704 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 705 |
|
| 706 |
|
| 707 |
def test_ndarray_yuvj420p() -> None: |
| 708 |
array = numpy.random.randint(0, 256, size=(720, 640), dtype=numpy.uint8) |
| 709 |
frame = VideoFrame.from_ndarray(array, format="yuvj420p") |
| 710 |
assert frame.width == 640 and frame.height == 480 |
| 711 |
assert frame.format.name == "yuvj420p" |
| 712 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 713 |
|
| 714 |
|
| 715 |
def test_ndarray_yuyv422() -> None: |
| 716 |
array = numpy.random.randint(0, 256, size=(480, 640, 2), dtype=numpy.uint8) |
| 717 |
frame = VideoFrame.from_ndarray(array, format="yuyv422") |
| 718 |
assert frame.width == 640 and frame.height == 480 |
| 719 |
assert frame.format.name == "yuyv422" |
| 720 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 721 |
|
| 722 |
|
| 723 |
def test_ndarray_yuv444p() -> None: |
| 724 |
array = numpy.random.randint(0, 256, size=(3, 480, 640), dtype=numpy.uint8) |
| 725 |
frame = VideoFrame.from_ndarray(array, format="yuv444p") |
| 726 |
assert frame.width == 640 and frame.height == 480 |
| 727 |
assert frame.format.name == "yuv444p" |
| 728 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 729 |
|
| 730 |
array = numpy.random.randint(0, 256, size=(3, 480, 640), dtype=numpy.uint8) |
| 731 |
frame = VideoFrame.from_ndarray(array, channel_last=False, format="yuv444p") |
| 732 |
assert frame.width == 640 and frame.height == 480 |
| 733 |
assert frame.format.name == "yuv444p" |
| 734 |
assertNdarraysEqual(frame.to_ndarray(channel_last=False), array) |
| 735 |
assert array.shape != frame.to_ndarray(channel_last=True).shape |
| 736 |
assert ( |
| 737 |
frame.to_ndarray(channel_last=False).shape |
| 738 |
!= frame.to_ndarray(channel_last=True).shape |
| 739 |
) |
| 740 |
|
| 741 |
|
| 742 |
def test_ndarray_yuvj444p() -> None: |
| 743 |
array = numpy.random.randint(0, 256, size=(3, 480, 640), dtype=numpy.uint8) |
| 744 |
frame = VideoFrame.from_ndarray(array, format="yuvj444p") |
| 745 |
assert frame.width == 640 and frame.height == 480 |
| 746 |
assert frame.format.name == "yuvj444p" |
| 747 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 748 |
|
| 749 |
|
| 750 |
def test_ndarray_yuv444p16() -> None: |
| 751 |
array = numpy.random.randint(0, 65536, size=(480, 640, 3), dtype=numpy.uint16) |
| 752 |
for format in ("yuv444p16be", "yuv444p16le"): |
| 753 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 754 |
assert frame.width == 640 and frame.height == 480 |
| 755 |
assert frame.format.name == format |
| 756 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 757 |
|
| 758 |
|
| 759 |
def test_ndarray_yuv422p10le() -> None: |
| 760 |
array = numpy.random.randint(0, 65536, size=(3, 480, 640), dtype=numpy.uint16) |
| 761 |
for format in ("yuv422p10le",): |
| 762 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 763 |
assert frame.width == 640 and frame.height == 480 |
| 764 |
assert frame.format.name == format |
| 765 |
assert format in supported_np_pix_fmts |
| 766 |
|
| 767 |
|
| 768 |
def test_ndarray_yuv444p16_align() -> None: |
| 769 |
array = numpy.random.randint(0, 65536, size=(238, 318, 3), dtype=numpy.uint16) |
| 770 |
for format in ("yuv444p16be", "yuv444p16le"): |
| 771 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 772 |
assert frame.width == 318 and frame.height == 238 |
| 773 |
assert frame.format.name == format |
| 774 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 775 |
|
| 776 |
|
| 777 |
def test_ndarray_yuva444p16() -> None: |
| 778 |
array = numpy.random.randint(0, 65536, size=(480, 640, 4), dtype=numpy.uint16) |
| 779 |
for format in ("yuva444p16be", "yuva444p16le"): |
| 780 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 781 |
assert frame.width == 640 and frame.height == 480 |
| 782 |
assert frame.format.name == format |
| 783 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 784 |
|
| 785 |
|
| 786 |
def test_ndarray_yuva444p16_align() -> None: |
| 787 |
array = numpy.random.randint(0, 65536, size=(238, 318, 4), dtype=numpy.uint16) |
| 788 |
for format in ("yuva444p16be", "yuva444p16le"): |
| 789 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 790 |
assert frame.width == 318 and frame.height == 238 |
| 791 |
assert frame.format.name == format |
| 792 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 793 |
|
| 794 |
|
| 795 |
def test_ndarray_yuyv422_align() -> None: |
| 796 |
array = numpy.random.randint(0, 256, size=(238, 318, 2), dtype=numpy.uint8) |
| 797 |
frame = VideoFrame.from_ndarray(array, format="yuyv422") |
| 798 |
assert frame.width == 318 and frame.height == 238 |
| 799 |
assert frame.format.name == "yuyv422" |
| 800 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 801 |
|
| 802 |
|
| 803 |
def test_ndarray_rgb48be() -> None: |
| 804 |
array = numpy.random.randint(0, 65536, size=(480, 640, 3), dtype=numpy.uint16) |
| 805 |
frame = VideoFrame.from_ndarray(array, format="rgb48be") |
| 806 |
assert frame.width == 640 and frame.height == 480 |
| 807 |
assert frame.format.name == "rgb48be" |
| 808 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 809 |
|
| 810 |
# check endianness by examining red value of first pixel |
| 811 |
assertPixelValue16(frame.planes[0], array[0][0][0], "big") |
| 812 |
|
| 813 |
|
| 814 |
def test_ndarray_bgr48be() -> None: |
| 815 |
array = numpy.random.randint(0, 65536, size=(480, 640, 3), dtype=numpy.uint16) |
| 816 |
frame = VideoFrame.from_ndarray(array, format="bgr48be") |
| 817 |
assert frame.width == 640 and frame.height == 480 |
| 818 |
assert frame.format.name == "bgr48be" |
| 819 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 820 |
|
| 821 |
# check endianness by examining blue value of first pixel |
| 822 |
assertPixelValue16(frame.planes[0], array[0][0][0], "big") |
| 823 |
|
| 824 |
|
| 825 |
def test_ndarray_rgb48le() -> None: |
| 826 |
array = numpy.random.randint(0, 65536, size=(480, 640, 3), dtype=numpy.uint16) |
| 827 |
frame = VideoFrame.from_ndarray(array, format="rgb48le") |
| 828 |
assert frame.width == 640 and frame.height == 480 |
| 829 |
assert frame.format.name == "rgb48le" |
| 830 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 831 |
|
| 832 |
# check endianness by examining red value of first pixel |
| 833 |
assertPixelValue16(frame.planes[0], array[0][0][0], "little") |
| 834 |
|
| 835 |
|
| 836 |
def test_ndarray_bgr48le() -> None: |
| 837 |
array = numpy.random.randint(0, 65536, size=(480, 640, 3), dtype=numpy.uint16) |
| 838 |
frame = VideoFrame.from_ndarray(array, format="bgr48le") |
| 839 |
assert frame.width == 640 and frame.height == 480 |
| 840 |
assert frame.format.name == "bgr48le" |
| 841 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 842 |
|
| 843 |
# check endianness by examining blue value of first pixel |
| 844 |
assertPixelValue16(frame.planes[0], array[0][0][0], "little") |
| 845 |
|
| 846 |
|
| 847 |
def test_ndarray_rgb48le_align() -> None: |
| 848 |
array = numpy.random.randint(0, 65536, size=(238, 318, 3), dtype=numpy.uint16) |
| 849 |
frame = VideoFrame.from_ndarray(array, format="rgb48le") |
| 850 |
assert frame.width == 318 and frame.height == 238 |
| 851 |
assert frame.format.name == "rgb48le" |
| 852 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 853 |
|
| 854 |
# check endianness by examining red value of first pixel |
| 855 |
assertPixelValue16(frame.planes[0], array[0][0][0], "little") |
| 856 |
|
| 857 |
|
| 858 |
def test_ndarray_bgr48le_align() -> None: |
| 859 |
array = numpy.random.randint(0, 65536, size=(238, 318, 3), dtype=numpy.uint16) |
| 860 |
frame = VideoFrame.from_ndarray(array, format="bgr48le") |
| 861 |
assert frame.width == 318 and frame.height == 238 |
| 862 |
assert frame.format.name == "bgr48le" |
| 863 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 864 |
|
| 865 |
# check endianness by examining blue value of first pixel |
| 866 |
assertPixelValue16(frame.planes[0], array[0][0][0], "little") |
| 867 |
|
| 868 |
|
| 869 |
def test_ndarray_rgba64be() -> None: |
| 870 |
array = numpy.random.randint(0, 65536, size=(480, 640, 4), dtype=numpy.uint16) |
| 871 |
frame = VideoFrame.from_ndarray(array, format="rgba64be") |
| 872 |
assert frame.width == 640 and frame.height == 480 |
| 873 |
assert frame.format.name == "rgba64be" |
| 874 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 875 |
|
| 876 |
# check endianness by examining red value of first pixel |
| 877 |
assertPixelValue16(frame.planes[0], array[0][0][0], "big") |
| 878 |
|
| 879 |
|
| 880 |
def test_ndarray_bgra64be() -> None: |
| 881 |
array = numpy.random.randint(0, 65536, size=(480, 640, 4), dtype=numpy.uint16) |
| 882 |
frame = VideoFrame.from_ndarray(array, format="bgra64be") |
| 883 |
assert frame.width == 640 and frame.height == 480 |
| 884 |
assert frame.format.name == "bgra64be" |
| 885 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 886 |
|
| 887 |
# check endianness by examining blue value of first pixel |
| 888 |
assertPixelValue16(frame.planes[0], array[0][0][0], "big") |
| 889 |
|
| 890 |
|
| 891 |
def test_ndarray_rgba64le() -> None: |
| 892 |
array = numpy.random.randint(0, 65536, size=(480, 640, 4), dtype=numpy.uint16) |
| 893 |
frame = VideoFrame.from_ndarray(array, format="rgba64le") |
| 894 |
assert frame.width == 640 and frame.height == 480 |
| 895 |
assert frame.format.name == "rgba64le" |
| 896 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 897 |
|
| 898 |
# check endianness by examining red value of first pixel |
| 899 |
assertPixelValue16(frame.planes[0], array[0][0][0], "little") |
| 900 |
|
| 901 |
|
| 902 |
def test_ndarray_bgra64le() -> None: |
| 903 |
array = numpy.random.randint(0, 65536, size=(480, 640, 4), dtype=numpy.uint16) |
| 904 |
frame = VideoFrame.from_ndarray(array, format="bgra64le") |
| 905 |
assert frame.width == 640 and frame.height == 480 |
| 906 |
assert frame.format.name == "bgra64le" |
| 907 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 908 |
|
| 909 |
# check endianness by examining blue value of first pixel |
| 910 |
assertPixelValue16(frame.planes[0], array[0][0][0], "little") |
| 911 |
|
| 912 |
|
| 913 |
def test_ndarray_rgbaf16() -> None: |
| 914 |
array = numpy.random.random_sample(size=(480, 640, 4)).astype(numpy.float16) |
| 915 |
for format in ("rgbaf16be", "rgbaf16le"): |
| 916 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 917 |
assert frame.width == 640 and frame.height == 480 |
| 918 |
assert frame.format.name == format |
| 919 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 920 |
|
| 921 |
|
| 922 |
def test_ndarray_rgbaf32() -> None: |
| 923 |
array = numpy.random.random_sample(size=(480, 640, 4)).astype(numpy.float32) |
| 924 |
for format in ("rgbaf32be", "rgbaf32le"): |
| 925 |
frame = VideoFrame.from_ndarray(array, format=format) |
| 926 |
assert frame.width == 640 and frame.height == 480 |
| 927 |
assert frame.format.name == format |
| 928 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 929 |
|
| 930 |
|
| 931 |
def test_ndarray_rgb8() -> None: |
| 932 |
array = numpy.random.randint(0, 256, size=(480, 640), dtype=numpy.uint8) |
| 933 |
frame = VideoFrame.from_ndarray(array, format="rgb8") |
| 934 |
assert frame.width == 640 and frame.height == 480 |
| 935 |
assert frame.format.name == "rgb8" |
| 936 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 937 |
|
| 938 |
|
| 939 |
def test_ndarray_bgr8() -> None: |
| 940 |
array = numpy.random.randint(0, 256, size=(480, 640), dtype=numpy.uint8) |
| 941 |
frame = VideoFrame.from_ndarray(array, format="bgr8") |
| 942 |
assert frame.width == 640 and frame.height == 480 |
| 943 |
assert frame.format.name == "bgr8" |
| 944 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 945 |
|
| 946 |
|
| 947 |
def test_ndarray_pal8(): |
| 948 |
array = numpy.random.randint(0, 256, size=(480, 640), dtype=numpy.uint8) |
| 949 |
palette = numpy.random.randint(0, 256, size=(256, 4), dtype=numpy.uint8) |
| 950 |
frame = VideoFrame.from_ndarray((array, palette), format="pal8") |
| 951 |
assert frame.width == 640 and frame.height == 480 |
| 952 |
assert frame.format.name == "pal8" |
| 953 |
assert frame.format.name in supported_np_pix_fmts |
| 954 |
returned = frame.to_ndarray() |
| 955 |
assert type(returned) is tuple and len(returned) == 2 |
| 956 |
assertNdarraysEqual(returned[0], array) |
| 957 |
assertNdarraysEqual(returned[1], palette) |
| 958 |
|
| 959 |
|
| 960 |
def test_ndarray_nv12() -> None: |
| 961 |
array = numpy.random.randint(0, 256, size=(720, 640), dtype=numpy.uint8) |
| 962 |
frame = VideoFrame.from_ndarray(array, format="nv12") |
| 963 |
assert frame.width == 640 and frame.height == 480 |
| 964 |
assert frame.format.name == "nv12" |
| 965 |
assert frame.format.name in supported_np_pix_fmts |
| 966 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 967 |
|
| 968 |
|
| 969 |
def test_ndarray_nv12_align() -> None: |
| 970 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 971 |
frame = VideoFrame.from_ndarray(array, format="nv12") |
| 972 |
assert frame.width == 318 and frame.height == 238 |
| 973 |
assert frame.format.name == "nv12" |
| 974 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 975 |
|
| 976 |
|
| 977 |
def test_shares_memory_gray() -> None: |
| 978 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 979 |
frame = VideoFrame.from_numpy_buffer(array, "gray") |
| 980 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 981 |
|
| 982 |
# overwrite the array, the contents thereof |
| 983 |
array[...] = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 984 |
# Make sure the frame reflects that |
| 985 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 986 |
|
| 987 |
# repeat the test, but with an array that is not fully contiguous, though the |
| 988 |
# pixels in a row are |
| 989 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 990 |
array = array[:, :300] |
| 991 |
assert not array.data.c_contiguous |
| 992 |
frame = VideoFrame.from_numpy_buffer(array, "gray") |
| 993 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 994 |
|
| 995 |
# overwrite the array, the contents thereof |
| 996 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 997 |
# Make sure the frame reflects that |
| 998 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 999 |
|
| 1000 |
|
| 1001 |
def test_shares_memory_gray8() -> None: |
| 1002 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1003 |
frame = VideoFrame.from_numpy_buffer(array, "gray8") |
| 1004 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1005 |
|
| 1006 |
# overwrite the array, the contents thereof |
| 1007 |
array[...] = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1008 |
# Make sure the frame reflects that |
| 1009 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1010 |
|
| 1011 |
# repeat the test, but with an array that is not fully contiguous, though the |
| 1012 |
# pixels in a row are |
| 1013 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1014 |
array = array[:, :300] |
| 1015 |
assert not array.data.c_contiguous |
| 1016 |
frame = VideoFrame.from_numpy_buffer(array, "gray8") |
| 1017 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1018 |
|
| 1019 |
# overwrite the array, the contents thereof |
| 1020 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1021 |
# Make sure the frame reflects that |
| 1022 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1023 |
|
| 1024 |
|
| 1025 |
def test_shares_memory_rgb8() -> None: |
| 1026 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1027 |
frame = VideoFrame.from_numpy_buffer(array, "rgb8") |
| 1028 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1029 |
|
| 1030 |
# overwrite the array, the contents thereof |
| 1031 |
array[...] = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1032 |
# Make sure the frame reflects that |
| 1033 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1034 |
|
| 1035 |
# repeat the test, but with an array that is not fully contiguous, though the |
| 1036 |
# pixels in a row are |
| 1037 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1038 |
array = array[:, :300] |
| 1039 |
assert not array.data.c_contiguous |
| 1040 |
frame = VideoFrame.from_numpy_buffer(array, "rgb8") |
| 1041 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1042 |
|
| 1043 |
# overwrite the array, the contents thereof |
| 1044 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1045 |
# Make sure the frame reflects that |
| 1046 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1047 |
|
| 1048 |
|
| 1049 |
def test_shares_memory_bgr8() -> None: |
| 1050 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1051 |
frame = VideoFrame.from_numpy_buffer(array, "bgr8") |
| 1052 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1053 |
|
| 1054 |
# overwrite the array, the contents thereof |
| 1055 |
array[...] = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1056 |
# Make sure the frame reflects that |
| 1057 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1058 |
|
| 1059 |
# repeat the test, but with an array that is not fully contiguous, though the |
| 1060 |
# pixels in a row are |
| 1061 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1062 |
array = array[:, :300] |
| 1063 |
assert not array.data.c_contiguous |
| 1064 |
frame = VideoFrame.from_numpy_buffer(array, "bgr8") |
| 1065 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1066 |
|
| 1067 |
# overwrite the array, the contents thereof |
| 1068 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1069 |
# Make sure the frame reflects that |
| 1070 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1071 |
|
| 1072 |
|
| 1073 |
def test_shares_memory_rgb24() -> None: |
| 1074 |
array = numpy.random.randint(0, 256, size=(357, 318, 3), dtype=numpy.uint8) |
| 1075 |
frame = VideoFrame.from_numpy_buffer(array, "rgb24") |
| 1076 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1077 |
|
| 1078 |
# overwrite the array, the contents thereof |
| 1079 |
array[...] = numpy.random.randint(0, 256, size=(357, 318, 3), dtype=numpy.uint8) |
| 1080 |
# Make sure the frame reflects that |
| 1081 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1082 |
|
| 1083 |
# repeat the test, but with an array that is not fully contiguous, though the |
| 1084 |
# pixels in a row are |
| 1085 |
array = numpy.random.randint(0, 256, size=(357, 318, 3), dtype=numpy.uint8) |
| 1086 |
array = array[:, :300, :] |
| 1087 |
assert not array.data.c_contiguous |
| 1088 |
frame = VideoFrame.from_numpy_buffer(array, "rgb24") |
| 1089 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1090 |
|
| 1091 |
# overwrite the array, the contents thereof |
| 1092 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1093 |
# Make sure the frame reflects that |
| 1094 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1095 |
|
| 1096 |
|
| 1097 |
def test_shares_memory_rgba() -> None: |
| 1098 |
array = numpy.random.randint(0, 256, size=(357, 318, 4), dtype=numpy.uint8) |
| 1099 |
frame = VideoFrame.from_numpy_buffer(array, "rgba") |
| 1100 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1101 |
|
| 1102 |
# overwrite the array, the contents thereof |
| 1103 |
array[...] = numpy.random.randint(0, 256, size=(357, 318, 4), dtype=numpy.uint8) |
| 1104 |
# Make sure the frame reflects that |
| 1105 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1106 |
|
| 1107 |
# repeat the test, but with an array that is not fully contiguous, though the |
| 1108 |
# pixels in a row are |
| 1109 |
array = numpy.random.randint(0, 256, size=(357, 318, 4), dtype=numpy.uint8) |
| 1110 |
array = array[:, :300, :] |
| 1111 |
assert not array.data.c_contiguous |
| 1112 |
frame = VideoFrame.from_numpy_buffer(array, "rgba") |
| 1113 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1114 |
|
| 1115 |
# overwrite the array, the contents thereof |
| 1116 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1117 |
# Make sure the frame reflects that |
| 1118 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1119 |
|
| 1120 |
|
| 1121 |
def test_shares_memory_bayer8() -> None: |
| 1122 |
for format in ("bayer_rggb8", "bayer_bggr8", "bayer_grbg8", "bayer_gbrg8"): |
| 1123 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1124 |
frame = VideoFrame.from_numpy_buffer(array, format) |
| 1125 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1126 |
|
| 1127 |
array[...] = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1128 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1129 |
|
| 1130 |
array = numpy.random.randint(0, 256, size=(357, 318), dtype=numpy.uint8) |
| 1131 |
array = array[:, :300] |
| 1132 |
assert not array.data.c_contiguous |
| 1133 |
frame = VideoFrame.from_numpy_buffer(array, format) |
| 1134 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1135 |
|
| 1136 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1137 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1138 |
|
| 1139 |
|
| 1140 |
def test_shares_memory_yuv420p() -> None: |
| 1141 |
array = numpy.random.randint(0, 256, size=(512 * 6 // 4, 256), dtype=numpy.uint8) |
| 1142 |
frame = VideoFrame.from_numpy_buffer(array, "yuv420p") |
| 1143 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1144 |
|
| 1145 |
# overwrite the array, the contents thereof |
| 1146 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1147 |
# Make sure the frame reflects that |
| 1148 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1149 |
|
| 1150 |
# repeat the test, but with an array where there are some padding bytes |
| 1151 |
# note that the uv rows have half the padding in the middle of a row, and the |
| 1152 |
# other half at the end |
| 1153 |
height = 512 |
| 1154 |
stride = 256 |
| 1155 |
width = 200 |
| 1156 |
array = numpy.random.randint( |
| 1157 |
0, 256, size=(height * 6 // 4, stride), dtype=numpy.uint8 |
| 1158 |
) |
| 1159 |
uv_width = width // 2 |
| 1160 |
uv_stride = stride // 2 |
| 1161 |
|
| 1162 |
# compare carefully, avoiding all the padding bytes which to_ndarray strips out |
| 1163 |
frame = VideoFrame.from_numpy_buffer(array, "yuv420p", width=width) |
| 1164 |
frame_array = frame.to_ndarray() |
| 1165 |
assertNdarraysEqual(frame_array[:height, :width], array[:height, :width]) |
| 1166 |
assertNdarraysEqual(frame_array[height:, :uv_width], array[height:, :uv_width]) |
| 1167 |
assertNdarraysEqual( |
| 1168 |
frame_array[height:, uv_width:], |
| 1169 |
array[height:, uv_stride : uv_stride + uv_width], |
| 1170 |
) |
| 1171 |
|
| 1172 |
# overwrite the array, and check the shared frame buffer changed too! |
| 1173 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1174 |
frame_array = frame.to_ndarray() |
| 1175 |
assertNdarraysEqual(frame_array[:height, :width], array[:height, :width]) |
| 1176 |
assertNdarraysEqual(frame_array[height:, :uv_width], array[height:, :uv_width]) |
| 1177 |
assertNdarraysEqual( |
| 1178 |
frame_array[height:, uv_width:], |
| 1179 |
array[height:, uv_stride : uv_stride + uv_width], |
| 1180 |
) |
| 1181 |
|
| 1182 |
|
| 1183 |
def test_shares_memory_yuvj420p() -> None: |
| 1184 |
array = numpy.random.randint(0, 256, size=(512 * 6 // 4, 256), dtype=numpy.uint8) |
| 1185 |
frame = VideoFrame.from_numpy_buffer(array, "yuvj420p") |
| 1186 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1187 |
|
| 1188 |
# overwrite the array, the contents thereof |
| 1189 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1190 |
# Make sure the frame reflects that |
| 1191 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1192 |
|
| 1193 |
# repeat the test with padding, just as we did in the yuv420p case |
| 1194 |
height = 512 |
| 1195 |
stride = 256 |
| 1196 |
width = 200 |
| 1197 |
array = numpy.random.randint( |
| 1198 |
0, 256, size=(height * 6 // 4, stride), dtype=numpy.uint8 |
| 1199 |
) |
| 1200 |
uv_width = width // 2 |
| 1201 |
uv_stride = stride // 2 |
| 1202 |
|
| 1203 |
# compare carefully, avoiding all the padding bytes which to_ndarray strips out |
| 1204 |
frame = VideoFrame.from_numpy_buffer(array, "yuvj420p", width=width) |
| 1205 |
frame_array = frame.to_ndarray() |
| 1206 |
assertNdarraysEqual(frame_array[:height, :width], array[:height, :width]) |
| 1207 |
assertNdarraysEqual(frame_array[height:, :uv_width], array[height:, :uv_width]) |
| 1208 |
assertNdarraysEqual( |
| 1209 |
frame_array[height:, uv_width:], |
| 1210 |
array[height:, uv_stride : uv_stride + uv_width], |
| 1211 |
) |
| 1212 |
|
| 1213 |
# overwrite the array, and check the shared frame buffer changed too! |
| 1214 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1215 |
frame_array = frame.to_ndarray() |
| 1216 |
assertNdarraysEqual(frame_array[:height, :width], array[:height, :width]) |
| 1217 |
assertNdarraysEqual(frame_array[height:, :uv_width], array[height:, :uv_width]) |
| 1218 |
assertNdarraysEqual( |
| 1219 |
frame_array[height:, uv_width:], |
| 1220 |
array[height:, uv_stride : uv_stride + uv_width], |
| 1221 |
) |
| 1222 |
|
| 1223 |
|
| 1224 |
def test_shares_memory_nv12() -> None: |
| 1225 |
array = numpy.random.randint(0, 256, size=(512 * 6 // 4, 256), dtype=numpy.uint8) |
| 1226 |
frame = VideoFrame.from_numpy_buffer(array, "nv12") |
| 1227 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1228 |
|
| 1229 |
# overwrite the array, the contents thereof |
| 1230 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1231 |
# Make sure the frame reflects that |
| 1232 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1233 |
|
| 1234 |
# repeat the test, but with an array that is not fully contiguous, though the |
| 1235 |
# pixels in a row are |
| 1236 |
array = numpy.random.randint(0, 256, size=(512 * 6 // 4, 256), dtype=numpy.uint8) |
| 1237 |
array = array[:, :200] |
| 1238 |
assert not array.data.c_contiguous |
| 1239 |
frame = VideoFrame.from_numpy_buffer(array, "nv12") |
| 1240 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1241 |
|
| 1242 |
# overwrite the array, the contents thereof |
| 1243 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1244 |
# Make sure the frame reflects that |
| 1245 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1246 |
|
| 1247 |
|
| 1248 |
def test_shares_memory_bgr24() -> None: |
| 1249 |
array = numpy.random.randint(0, 256, size=(357, 318, 3), dtype=numpy.uint8) |
| 1250 |
frame = VideoFrame.from_numpy_buffer(array, "bgr24") |
| 1251 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1252 |
|
| 1253 |
# overwrite the array, the contents thereof |
| 1254 |
array[...] = numpy.random.randint(0, 256, size=(357, 318, 3), dtype=numpy.uint8) |
| 1255 |
# Make sure the frame reflects that |
| 1256 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1257 |
|
| 1258 |
# repeat the test, but with an array that is not fully contiguous, though the |
| 1259 |
# pixels in a row are |
| 1260 |
array = numpy.random.randint(0, 256, size=(357, 318, 3), dtype=numpy.uint8) |
| 1261 |
array = array[:, :300, :] |
| 1262 |
assert not array.data.c_contiguous |
| 1263 |
frame = VideoFrame.from_numpy_buffer(array, "bgr24") |
| 1264 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1265 |
|
| 1266 |
# overwrite the array, the contents thereof |
| 1267 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1268 |
# Make sure the frame reflects that |
| 1269 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1270 |
|
| 1271 |
|
| 1272 |
def test_shares_memory_bgra() -> None: |
| 1273 |
array = numpy.random.randint(0, 256, size=(357, 318, 4), dtype=numpy.uint8) |
| 1274 |
frame = VideoFrame.from_numpy_buffer(array, "bgra") |
| 1275 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1276 |
|
| 1277 |
# overwrite the array, the contents thereof |
| 1278 |
array[...] = numpy.random.randint(0, 256, size=(357, 318, 4), dtype=numpy.uint8) |
| 1279 |
# Make sure the frame reflects that |
| 1280 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1281 |
|
| 1282 |
# repeat the test, but with an array that is not fully contiguous, though the |
| 1283 |
# pixels in a row are |
| 1284 |
array = numpy.random.randint(0, 256, size=(357, 318, 4), dtype=numpy.uint8) |
| 1285 |
array = array[:, :300, :] |
| 1286 |
assert not array.data.c_contiguous |
| 1287 |
frame = VideoFrame.from_numpy_buffer(array, "bgra") |
| 1288 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1289 |
|
| 1290 |
# overwrite the array, the contents thereof |
| 1291 |
array[...] = numpy.random.randint(0, 256, size=array.shape, dtype=numpy.uint8) |
| 1292 |
# Make sure the frame reflects that |
| 1293 |
assertNdarraysEqual(frame.to_ndarray(), array) |
| 1294 |
|
| 1295 |
|
| 1296 |
def test_reformat_pts() -> None: |
| 1297 |
frame = VideoFrame(640, 480, "rgb24") |
| 1298 |
frame.pts = 123 |
| 1299 |
frame.time_base = Fraction("456/1") |
| 1300 |
frame = frame.reformat(320, 240) |
| 1301 |
assert frame.pts == 123 and frame.time_base == 456 |
| 1302 |
|
| 1303 |
|
| 1304 |
def test_reformat_identity() -> None: |
| 1305 |
frame1 = VideoFrame(640, 480, "rgb24") |
| 1306 |
frame2 = frame1.reformat(640, 480, "rgb24") |
| 1307 |
assert frame1 is frame2 |
| 1308 |
|
| 1309 |
|
| 1310 |
def test_reformat_colorspace() -> None: |
| 1311 |
frame = VideoFrame(640, 480, "rgb24") |
| 1312 |
frame.reformat(src_colorspace=None, dst_colorspace="smpte240m") |
| 1313 |
|
| 1314 |
frame = VideoFrame(640, 480, "rgb24") |
| 1315 |
frame.reformat(src_colorspace=None, dst_colorspace=Colorspace.smpte240m) |
| 1316 |
|
| 1317 |
frame = VideoFrame(640, 480, "yuv420p") |
| 1318 |
frame.reformat(src_colorspace=None, dst_colorspace="smpte240m") |
| 1319 |
|
| 1320 |
frame = VideoFrame(640, 480, "rgb24") |
| 1321 |
frame.colorspace = Colorspace.smpte240m |
| 1322 |
assert frame.colorspace == int(Colorspace.smpte240m) |
| 1323 |
assert frame.colorspace == Colorspace.smpte240m |
| 1324 |
|
| 1325 |
|
| 1326 |
def test_reformat_pixel_format_align() -> None: |
| 1327 |
height = 480 |
| 1328 |
for width in range(2, 258, 2): |
| 1329 |
frame_yuv = VideoFrame(width, height, "yuv420p") |
| 1330 |
for plane in frame_yuv.planes: |
| 1331 |
plane.update(b"\xff" * plane.buffer_size) |
| 1332 |
|
| 1333 |
expected_rgb = numpy.zeros(shape=(height, width, 3), dtype=numpy.uint8) |
| 1334 |
expected_rgb[:, :, 0] = 255 |
| 1335 |
expected_rgb[:, :, 1] = 124 |
| 1336 |
expected_rgb[:, :, 2] = 255 |
| 1337 |
|
| 1338 |
frame_rgb = frame_yuv.reformat(format="rgb24") |
| 1339 |
result = frame_rgb.to_ndarray() |
| 1340 |
assert result.shape == expected_rgb.shape |
| 1341 |
assert numpy.abs(result.astype(int) - expected_rgb.astype(int)).max() <= 1 |