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