-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathtest_videoframe.py
More file actions
971 lines (755 loc) · 37.5 KB
/
test_videoframe.py
File metadata and controls
971 lines (755 loc) · 37.5 KB
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
import time
from fractions import Fraction
from unittest import SkipTest
import numpy
import pytest
import av
from av import VideoFrame
from av.video.reformatter import ColorRange, Colorspace, Interpolation
from .common import (
TestCase,
assertImagesAlmostEqual,
assertNdarraysEqual,
fate_png,
fate_suite,
has_pillow,
)
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_opaque() -> None:
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container:
video_stream = container.streams.video[0]
ctx = video_stream.codec_context
ctx.flags |= av.codec.context.Flags.copy_opaque
assert video_stream.codec_context.copy_opaque
for packet_idx, packet in enumerate(container.demux()):
packet.opaque = (time.time(), packet_idx)
for frame in packet.decode():
assert isinstance(frame, av.frame.Frame)
if frame.opaque is None:
continue
assert type(frame.opaque) is tuple and len(frame.opaque) == 2
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"
class TestVideoFrameImage(TestCase):
def setUp(self) -> None:
if not has_pillow:
pytest.skip()
def test_roundtrip(self) -> None:
import PIL.Image as Image
image = Image.open(fate_png())
frame = VideoFrame.from_image(image)
img = frame.to_image()
img.save(self.sandboxed("roundtrip-high.jpg"))
assertImagesAlmostEqual(image, img)
def test_interpolation(self) -> None:
import PIL.Image as Image
image = Image.open(fate_png())
frame = VideoFrame.from_image(image)
assert frame.width == 330 and frame.height == 330
img = frame.to_image(width=200, height=100, interpolation=Interpolation.BICUBIC)
assert img.width == 200 and img.height == 100
img = frame.to_image(width=200, height=100, interpolation="BICUBIC")
assert img.width == 200 and img.height == 100
img = frame.to_image(
width=200, height=100, interpolation=int(Interpolation.BICUBIC)
)
assert img.width == 200 and img.height == 100
def test_to_image_rgb24(self) -> None:
sizes = [(318, 238), (320, 240), (500, 500)]
for width, height in sizes:
frame = VideoFrame(width, height, format="rgb24")
# fill video frame data
for plane in frame.planes:
ba = bytearray(plane.buffer_size)
pos = 0
for row in range(height):
for i in range(plane.line_size):
ba[pos] = i % 256
pos += 1
plane.update(ba)
# construct expected image data
expected = bytearray(height * width * 3)
pos = 0
for row in range(height):
for i in range(width * 3):
expected[pos] = i % 256
pos += 1
img = frame.to_image()
assert img.size == (width, height)
assert img.tobytes() == expected
def test_basic_to_ndarray() -> None:
array = VideoFrame(640, 480, "rgb24").to_ndarray()
assert array.shape == (480, 640, 3)
def test_to_image_with_dimensions() -> None:
if not has_pillow:
pytest.skip()
img = VideoFrame(640, 480, format="rgb24").to_image(width=320, height=240)
assert img.size == (320, 240)
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 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 frame.width == 318 and frame.height == 238
assert frame.format.name == "gray"
assertNdarraysEqual(frame.to_ndarray(), array)
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 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 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 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 frame.width == 318 and frame.height == 238
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 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 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_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 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 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 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 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 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 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 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 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_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_allign() -> 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_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_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_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_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_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_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_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_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_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"
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"
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_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)