Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_video_reader.py 43 KB

You have to be logged in to leave a comment. Sign In
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
  1. import collections
  2. import math
  3. import os
  4. from fractions import Fraction
  5. import numpy as np
  6. import pytest
  7. import torch
  8. import torchvision.io as io
  9. from common_utils import assert_equal
  10. from numpy.random import randint
  11. from pytest import approx
  12. from torchvision import set_video_backend
  13. from torchvision.io import _HAS_CPU_VIDEO_DECODER
  14. try:
  15. import av
  16. # Do a version test too
  17. io.video._check_av_available()
  18. except ImportError:
  19. av = None
  20. VIDEO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "videos")
  21. CheckerConfig = [
  22. "duration",
  23. "video_fps",
  24. "audio_sample_rate",
  25. # We find for some videos (e.g. HMDB51 videos), the decoded audio frames and pts are
  26. # slightly different between TorchVision decoder and PyAv decoder. So omit it during check
  27. "check_aframes",
  28. "check_aframe_pts",
  29. ]
  30. GroundTruth = collections.namedtuple("GroundTruth", " ".join(CheckerConfig))
  31. all_check_config = GroundTruth(
  32. duration=0,
  33. video_fps=0,
  34. audio_sample_rate=0,
  35. check_aframes=True,
  36. check_aframe_pts=True,
  37. )
  38. test_videos = {
  39. "RATRACE_wave_f_nm_np1_fr_goo_37.avi": GroundTruth(
  40. duration=2.0,
  41. video_fps=30.0,
  42. audio_sample_rate=None,
  43. check_aframes=True,
  44. check_aframe_pts=True,
  45. ),
  46. "SchoolRulesHowTheyHelpUs_wave_f_nm_np1_ba_med_0.avi": GroundTruth(
  47. duration=2.0,
  48. video_fps=30.0,
  49. audio_sample_rate=None,
  50. check_aframes=True,
  51. check_aframe_pts=True,
  52. ),
  53. "TrumanShow_wave_f_nm_np1_fr_med_26.avi": GroundTruth(
  54. duration=2.0,
  55. video_fps=30.0,
  56. audio_sample_rate=None,
  57. check_aframes=True,
  58. check_aframe_pts=True,
  59. ),
  60. "v_SoccerJuggling_g23_c01.avi": GroundTruth(
  61. duration=8.0,
  62. video_fps=29.97,
  63. audio_sample_rate=None,
  64. check_aframes=True,
  65. check_aframe_pts=True,
  66. ),
  67. "v_SoccerJuggling_g24_c01.avi": GroundTruth(
  68. duration=8.0,
  69. video_fps=29.97,
  70. audio_sample_rate=None,
  71. check_aframes=True,
  72. check_aframe_pts=True,
  73. ),
  74. "R6llTwEh07w.mp4": GroundTruth(
  75. duration=10.0,
  76. video_fps=30.0,
  77. audio_sample_rate=44100,
  78. # PyAv miss one audio frame at the beginning (pts=0)
  79. check_aframes=False,
  80. check_aframe_pts=False,
  81. ),
  82. "SOX5yA1l24A.mp4": GroundTruth(
  83. duration=11.0,
  84. video_fps=29.97,
  85. audio_sample_rate=48000,
  86. # PyAv miss one audio frame at the beginning (pts=0)
  87. check_aframes=False,
  88. check_aframe_pts=False,
  89. ),
  90. "WUzgd7C1pWA.mp4": GroundTruth(
  91. duration=11.0,
  92. video_fps=29.97,
  93. audio_sample_rate=48000,
  94. # PyAv miss one audio frame at the beginning (pts=0)
  95. check_aframes=False,
  96. check_aframe_pts=False,
  97. ),
  98. }
  99. DecoderResult = collections.namedtuple("DecoderResult", "vframes vframe_pts vtimebase aframes aframe_pts atimebase")
  100. # av_seek_frame is imprecise so seek to a timestamp earlier by a margin
  101. # The unit of margin is second
  102. SEEK_FRAME_MARGIN = 0.25
  103. def _read_from_stream(container, start_pts, end_pts, stream, stream_name, buffer_size=4):
  104. """
  105. Args:
  106. container: pyav container
  107. start_pts/end_pts: the starting/ending Presentation TimeStamp where
  108. frames are read
  109. stream: pyav stream
  110. stream_name: a dictionary of streams. For example, {"video": 0} means
  111. video stream at stream index 0
  112. buffer_size: pts of frames decoded by PyAv is not guaranteed to be in
  113. ascending order. We need to decode more frames even when we meet end
  114. pts
  115. """
  116. # seeking in the stream is imprecise. Thus, seek to an earlier PTS by a margin
  117. margin = 1
  118. seek_offset = max(start_pts - margin, 0)
  119. container.seek(seek_offset, any_frame=False, backward=True, stream=stream)
  120. frames = {}
  121. buffer_count = 0
  122. for frame in container.decode(**stream_name):
  123. if frame.pts < start_pts:
  124. continue
  125. if frame.pts <= end_pts:
  126. frames[frame.pts] = frame
  127. else:
  128. buffer_count += 1
  129. if buffer_count >= buffer_size:
  130. break
  131. result = [frames[pts] for pts in sorted(frames)]
  132. return result
  133. def _get_timebase_by_av_module(full_path):
  134. container = av.open(full_path)
  135. video_time_base = container.streams.video[0].time_base
  136. if container.streams.audio:
  137. audio_time_base = container.streams.audio[0].time_base
  138. else:
  139. audio_time_base = None
  140. return video_time_base, audio_time_base
  141. def _fraction_to_tensor(fraction):
  142. ret = torch.zeros([2], dtype=torch.int32)
  143. ret[0] = fraction.numerator
  144. ret[1] = fraction.denominator
  145. return ret
  146. def _decode_frames_by_av_module(
  147. full_path,
  148. video_start_pts=0,
  149. video_end_pts=None,
  150. audio_start_pts=0,
  151. audio_end_pts=None,
  152. ):
  153. """
  154. Use PyAv to decode video frames. This provides a reference for our decoder
  155. to compare the decoding results.
  156. Input arguments:
  157. full_path: video file path
  158. video_start_pts/video_end_pts: the starting/ending Presentation TimeStamp where
  159. frames are read
  160. """
  161. if video_end_pts is None:
  162. video_end_pts = float("inf")
  163. if audio_end_pts is None:
  164. audio_end_pts = float("inf")
  165. container = av.open(full_path)
  166. video_frames = []
  167. vtimebase = torch.zeros([0], dtype=torch.int32)
  168. if container.streams.video:
  169. video_frames = _read_from_stream(
  170. container,
  171. video_start_pts,
  172. video_end_pts,
  173. container.streams.video[0],
  174. {"video": 0},
  175. )
  176. # container.streams.video[0].average_rate is not a reliable estimator of
  177. # frame rate. It can be wrong for certain codec, such as VP80
  178. # So we do not return video fps here
  179. vtimebase = _fraction_to_tensor(container.streams.video[0].time_base)
  180. audio_frames = []
  181. atimebase = torch.zeros([0], dtype=torch.int32)
  182. if container.streams.audio:
  183. audio_frames = _read_from_stream(
  184. container,
  185. audio_start_pts,
  186. audio_end_pts,
  187. container.streams.audio[0],
  188. {"audio": 0},
  189. )
  190. atimebase = _fraction_to_tensor(container.streams.audio[0].time_base)
  191. container.close()
  192. vframes = [frame.to_rgb().to_ndarray() for frame in video_frames]
  193. vframes = torch.as_tensor(np.stack(vframes))
  194. vframe_pts = torch.tensor([frame.pts for frame in video_frames], dtype=torch.int64)
  195. aframes = [frame.to_ndarray() for frame in audio_frames]
  196. if aframes:
  197. aframes = np.transpose(np.concatenate(aframes, axis=1))
  198. aframes = torch.as_tensor(aframes)
  199. else:
  200. aframes = torch.empty((1, 0), dtype=torch.float32)
  201. aframe_pts = torch.tensor([audio_frame.pts for audio_frame in audio_frames], dtype=torch.int64)
  202. return DecoderResult(
  203. vframes=vframes,
  204. vframe_pts=vframe_pts,
  205. vtimebase=vtimebase,
  206. aframes=aframes,
  207. aframe_pts=aframe_pts,
  208. atimebase=atimebase,
  209. )
  210. def _pts_convert(pts, timebase_from, timebase_to, round_func=math.floor):
  211. """convert pts between different time bases
  212. Args:
  213. pts: presentation timestamp, float
  214. timebase_from: original timebase. Fraction
  215. timebase_to: new timebase. Fraction
  216. round_func: rounding function.
  217. """
  218. new_pts = Fraction(pts, 1) * timebase_from / timebase_to
  219. return int(round_func(new_pts))
  220. def _get_video_tensor(video_dir, video_file):
  221. """open a video file, and represent the video data by a PT tensor"""
  222. full_path = os.path.join(video_dir, video_file)
  223. assert os.path.exists(full_path), "File not found: %s" % full_path
  224. with open(full_path, "rb") as fp:
  225. video_tensor = torch.frombuffer(fp.read(), dtype=torch.uint8)
  226. return full_path, video_tensor
  227. @pytest.mark.skipif(av is None, reason="PyAV unavailable")
  228. @pytest.mark.skipif(_HAS_CPU_VIDEO_DECODER is False, reason="Didn't compile with ffmpeg")
  229. class TestVideoReader:
  230. def check_separate_decoding_result(self, tv_result, config):
  231. """check the decoding results from TorchVision decoder"""
  232. (
  233. vframes,
  234. vframe_pts,
  235. vtimebase,
  236. vfps,
  237. vduration,
  238. aframes,
  239. aframe_pts,
  240. atimebase,
  241. asample_rate,
  242. aduration,
  243. ) = tv_result
  244. video_duration = vduration.item() * Fraction(vtimebase[0].item(), vtimebase[1].item())
  245. assert video_duration == approx(config.duration, abs=0.5)
  246. assert vfps.item() == approx(config.video_fps, abs=0.5)
  247. if asample_rate.numel() > 0:
  248. assert asample_rate.item() == config.audio_sample_rate
  249. audio_duration = aduration.item() * Fraction(atimebase[0].item(), atimebase[1].item())
  250. assert audio_duration == approx(config.duration, abs=0.5)
  251. # check if pts of video frames are sorted in ascending order
  252. for i in range(len(vframe_pts) - 1):
  253. assert vframe_pts[i] < vframe_pts[i + 1]
  254. if len(aframe_pts) > 1:
  255. # check if pts of audio frames are sorted in ascending order
  256. for i in range(len(aframe_pts) - 1):
  257. assert aframe_pts[i] < aframe_pts[i + 1]
  258. def check_probe_result(self, result, config):
  259. vtimebase, vfps, vduration, atimebase, asample_rate, aduration = result
  260. video_duration = vduration.item() * Fraction(vtimebase[0].item(), vtimebase[1].item())
  261. assert video_duration == approx(config.duration, abs=0.5)
  262. assert vfps.item() == approx(config.video_fps, abs=0.5)
  263. if asample_rate.numel() > 0:
  264. assert asample_rate.item() == config.audio_sample_rate
  265. audio_duration = aduration.item() * Fraction(atimebase[0].item(), atimebase[1].item())
  266. assert audio_duration == approx(config.duration, abs=0.5)
  267. def check_meta_result(self, result, config):
  268. assert result.video_duration == approx(config.duration, abs=0.5)
  269. assert result.video_fps == approx(config.video_fps, abs=0.5)
  270. if result.has_audio > 0:
  271. assert result.audio_sample_rate == config.audio_sample_rate
  272. assert result.audio_duration == approx(config.duration, abs=0.5)
  273. def compare_decoding_result(self, tv_result, ref_result, config=all_check_config):
  274. """
  275. Compare decoding results from two sources.
  276. Args:
  277. tv_result: decoding results from TorchVision decoder
  278. ref_result: reference decoding results which can be from either PyAv
  279. decoder or TorchVision decoder with getPtsOnly = 1
  280. config: config of decoding results checker
  281. """
  282. (
  283. vframes,
  284. vframe_pts,
  285. vtimebase,
  286. _vfps,
  287. _vduration,
  288. aframes,
  289. aframe_pts,
  290. atimebase,
  291. _asample_rate,
  292. _aduration,
  293. ) = tv_result
  294. if isinstance(ref_result, list):
  295. # the ref_result is from new video_reader decoder
  296. ref_result = DecoderResult(
  297. vframes=ref_result[0],
  298. vframe_pts=ref_result[1],
  299. vtimebase=ref_result[2],
  300. aframes=ref_result[5],
  301. aframe_pts=ref_result[6],
  302. atimebase=ref_result[7],
  303. )
  304. if vframes.numel() > 0 and ref_result.vframes.numel() > 0:
  305. mean_delta = torch.mean(torch.abs(vframes.float() - ref_result.vframes.float()))
  306. assert mean_delta == approx(0.0, abs=8.0)
  307. mean_delta = torch.mean(torch.abs(vframe_pts.float() - ref_result.vframe_pts.float()))
  308. assert mean_delta == approx(0.0, abs=1.0)
  309. assert_equal(vtimebase, ref_result.vtimebase)
  310. if config.check_aframes and aframes.numel() > 0 and ref_result.aframes.numel() > 0:
  311. """Audio stream is available and audio frame is required to return
  312. from decoder"""
  313. assert_equal(aframes, ref_result.aframes)
  314. if config.check_aframe_pts and aframe_pts.numel() > 0 and ref_result.aframe_pts.numel() > 0:
  315. """Audio stream is available"""
  316. assert_equal(aframe_pts, ref_result.aframe_pts)
  317. assert_equal(atimebase, ref_result.atimebase)
  318. @pytest.mark.parametrize("test_video", test_videos.keys())
  319. def test_stress_test_read_video_from_file(self, test_video):
  320. pytest.skip(
  321. "This stress test will iteratively decode the same set of videos."
  322. "It helps to detect memory leak but it takes lots of time to run."
  323. "By default, it is disabled"
  324. )
  325. num_iter = 10000
  326. # video related
  327. width, height, min_dimension, max_dimension = 0, 0, 0, 0
  328. video_start_pts, video_end_pts = 0, -1
  329. video_timebase_num, video_timebase_den = 0, 1
  330. # audio related
  331. samples, channels = 0, 0
  332. audio_start_pts, audio_end_pts = 0, -1
  333. audio_timebase_num, audio_timebase_den = 0, 1
  334. for _i in range(num_iter):
  335. full_path = os.path.join(VIDEO_DIR, test_video)
  336. # pass 1: decode all frames using new decoder
  337. torch.ops.video_reader.read_video_from_file(
  338. full_path,
  339. SEEK_FRAME_MARGIN,
  340. 0, # getPtsOnly
  341. 1, # readVideoStream
  342. width,
  343. height,
  344. min_dimension,
  345. max_dimension,
  346. video_start_pts,
  347. video_end_pts,
  348. video_timebase_num,
  349. video_timebase_den,
  350. 1, # readAudioStream
  351. samples,
  352. channels,
  353. audio_start_pts,
  354. audio_end_pts,
  355. audio_timebase_num,
  356. audio_timebase_den,
  357. )
  358. @pytest.mark.parametrize("test_video,config", test_videos.items())
  359. def test_read_video_from_file(self, test_video, config):
  360. """
  361. Test the case when decoder starts with a video file to decode frames.
  362. """
  363. # video related
  364. width, height, min_dimension, max_dimension = 0, 0, 0, 0
  365. video_start_pts, video_end_pts = 0, -1
  366. video_timebase_num, video_timebase_den = 0, 1
  367. # audio related
  368. samples, channels = 0, 0
  369. audio_start_pts, audio_end_pts = 0, -1
  370. audio_timebase_num, audio_timebase_den = 0, 1
  371. full_path = os.path.join(VIDEO_DIR, test_video)
  372. # pass 1: decode all frames using new decoder
  373. tv_result = torch.ops.video_reader.read_video_from_file(
  374. full_path,
  375. SEEK_FRAME_MARGIN,
  376. 0, # getPtsOnly
  377. 1, # readVideoStream
  378. width,
  379. height,
  380. min_dimension,
  381. max_dimension,
  382. video_start_pts,
  383. video_end_pts,
  384. video_timebase_num,
  385. video_timebase_den,
  386. 1, # readAudioStream
  387. samples,
  388. channels,
  389. audio_start_pts,
  390. audio_end_pts,
  391. audio_timebase_num,
  392. audio_timebase_den,
  393. )
  394. # pass 2: decode all frames using av
  395. pyav_result = _decode_frames_by_av_module(full_path)
  396. # check results from TorchVision decoder
  397. self.check_separate_decoding_result(tv_result, config)
  398. # compare decoding results
  399. self.compare_decoding_result(tv_result, pyav_result, config)
  400. @pytest.mark.parametrize("test_video,config", test_videos.items())
  401. @pytest.mark.parametrize("read_video_stream,read_audio_stream", [(1, 0), (0, 1)])
  402. def test_read_video_from_file_read_single_stream_only(
  403. self, test_video, config, read_video_stream, read_audio_stream
  404. ):
  405. """
  406. Test the case when decoder starts with a video file to decode frames, and
  407. only reads video stream and ignores audio stream
  408. """
  409. # video related
  410. width, height, min_dimension, max_dimension = 0, 0, 0, 0
  411. video_start_pts, video_end_pts = 0, -1
  412. video_timebase_num, video_timebase_den = 0, 1
  413. # audio related
  414. samples, channels = 0, 0
  415. audio_start_pts, audio_end_pts = 0, -1
  416. audio_timebase_num, audio_timebase_den = 0, 1
  417. full_path = os.path.join(VIDEO_DIR, test_video)
  418. # decode all frames using new decoder
  419. tv_result = torch.ops.video_reader.read_video_from_file(
  420. full_path,
  421. SEEK_FRAME_MARGIN,
  422. 0, # getPtsOnly
  423. read_video_stream,
  424. width,
  425. height,
  426. min_dimension,
  427. max_dimension,
  428. video_start_pts,
  429. video_end_pts,
  430. video_timebase_num,
  431. video_timebase_den,
  432. read_audio_stream,
  433. samples,
  434. channels,
  435. audio_start_pts,
  436. audio_end_pts,
  437. audio_timebase_num,
  438. audio_timebase_den,
  439. )
  440. (
  441. vframes,
  442. vframe_pts,
  443. vtimebase,
  444. vfps,
  445. vduration,
  446. aframes,
  447. aframe_pts,
  448. atimebase,
  449. asample_rate,
  450. aduration,
  451. ) = tv_result
  452. assert (vframes.numel() > 0) is bool(read_video_stream)
  453. assert (vframe_pts.numel() > 0) is bool(read_video_stream)
  454. assert (vtimebase.numel() > 0) is bool(read_video_stream)
  455. assert (vfps.numel() > 0) is bool(read_video_stream)
  456. expect_audio_data = read_audio_stream == 1 and config.audio_sample_rate is not None
  457. assert (aframes.numel() > 0) is bool(expect_audio_data)
  458. assert (aframe_pts.numel() > 0) is bool(expect_audio_data)
  459. assert (atimebase.numel() > 0) is bool(expect_audio_data)
  460. assert (asample_rate.numel() > 0) is bool(expect_audio_data)
  461. @pytest.mark.parametrize("test_video", test_videos.keys())
  462. def test_read_video_from_file_rescale_min_dimension(self, test_video):
  463. """
  464. Test the case when decoder starts with a video file to decode frames, and
  465. video min dimension between height and width is set.
  466. """
  467. # video related
  468. width, height, min_dimension, max_dimension = 0, 0, 128, 0
  469. video_start_pts, video_end_pts = 0, -1
  470. video_timebase_num, video_timebase_den = 0, 1
  471. # audio related
  472. samples, channels = 0, 0
  473. audio_start_pts, audio_end_pts = 0, -1
  474. audio_timebase_num, audio_timebase_den = 0, 1
  475. full_path = os.path.join(VIDEO_DIR, test_video)
  476. tv_result = torch.ops.video_reader.read_video_from_file(
  477. full_path,
  478. SEEK_FRAME_MARGIN,
  479. 0, # getPtsOnly
  480. 1, # readVideoStream
  481. width,
  482. height,
  483. min_dimension,
  484. max_dimension,
  485. video_start_pts,
  486. video_end_pts,
  487. video_timebase_num,
  488. video_timebase_den,
  489. 1, # readAudioStream
  490. samples,
  491. channels,
  492. audio_start_pts,
  493. audio_end_pts,
  494. audio_timebase_num,
  495. audio_timebase_den,
  496. )
  497. assert min_dimension == min(tv_result[0].size(1), tv_result[0].size(2))
  498. @pytest.mark.parametrize("test_video", test_videos.keys())
  499. def test_read_video_from_file_rescale_max_dimension(self, test_video):
  500. """
  501. Test the case when decoder starts with a video file to decode frames, and
  502. video min dimension between height and width is set.
  503. """
  504. # video related
  505. width, height, min_dimension, max_dimension = 0, 0, 0, 85
  506. video_start_pts, video_end_pts = 0, -1
  507. video_timebase_num, video_timebase_den = 0, 1
  508. # audio related
  509. samples, channels = 0, 0
  510. audio_start_pts, audio_end_pts = 0, -1
  511. audio_timebase_num, audio_timebase_den = 0, 1
  512. full_path = os.path.join(VIDEO_DIR, test_video)
  513. tv_result = torch.ops.video_reader.read_video_from_file(
  514. full_path,
  515. SEEK_FRAME_MARGIN,
  516. 0, # getPtsOnly
  517. 1, # readVideoStream
  518. width,
  519. height,
  520. min_dimension,
  521. max_dimension,
  522. video_start_pts,
  523. video_end_pts,
  524. video_timebase_num,
  525. video_timebase_den,
  526. 1, # readAudioStream
  527. samples,
  528. channels,
  529. audio_start_pts,
  530. audio_end_pts,
  531. audio_timebase_num,
  532. audio_timebase_den,
  533. )
  534. assert max_dimension == max(tv_result[0].size(1), tv_result[0].size(2))
  535. @pytest.mark.parametrize("test_video", test_videos.keys())
  536. def test_read_video_from_file_rescale_both_min_max_dimension(self, test_video):
  537. """
  538. Test the case when decoder starts with a video file to decode frames, and
  539. video min dimension between height and width is set.
  540. """
  541. # video related
  542. width, height, min_dimension, max_dimension = 0, 0, 64, 85
  543. video_start_pts, video_end_pts = 0, -1
  544. video_timebase_num, video_timebase_den = 0, 1
  545. # audio related
  546. samples, channels = 0, 0
  547. audio_start_pts, audio_end_pts = 0, -1
  548. audio_timebase_num, audio_timebase_den = 0, 1
  549. full_path = os.path.join(VIDEO_DIR, test_video)
  550. tv_result = torch.ops.video_reader.read_video_from_file(
  551. full_path,
  552. SEEK_FRAME_MARGIN,
  553. 0, # getPtsOnly
  554. 1, # readVideoStream
  555. width,
  556. height,
  557. min_dimension,
  558. max_dimension,
  559. video_start_pts,
  560. video_end_pts,
  561. video_timebase_num,
  562. video_timebase_den,
  563. 1, # readAudioStream
  564. samples,
  565. channels,
  566. audio_start_pts,
  567. audio_end_pts,
  568. audio_timebase_num,
  569. audio_timebase_den,
  570. )
  571. assert min_dimension == min(tv_result[0].size(1), tv_result[0].size(2))
  572. assert max_dimension == max(tv_result[0].size(1), tv_result[0].size(2))
  573. @pytest.mark.parametrize("test_video", test_videos.keys())
  574. def test_read_video_from_file_rescale_width(self, test_video):
  575. """
  576. Test the case when decoder starts with a video file to decode frames, and
  577. video width is set.
  578. """
  579. # video related
  580. width, height, min_dimension, max_dimension = 256, 0, 0, 0
  581. video_start_pts, video_end_pts = 0, -1
  582. video_timebase_num, video_timebase_den = 0, 1
  583. # audio related
  584. samples, channels = 0, 0
  585. audio_start_pts, audio_end_pts = 0, -1
  586. audio_timebase_num, audio_timebase_den = 0, 1
  587. full_path = os.path.join(VIDEO_DIR, test_video)
  588. tv_result = torch.ops.video_reader.read_video_from_file(
  589. full_path,
  590. SEEK_FRAME_MARGIN,
  591. 0, # getPtsOnly
  592. 1, # readVideoStream
  593. width,
  594. height,
  595. min_dimension,
  596. max_dimension,
  597. video_start_pts,
  598. video_end_pts,
  599. video_timebase_num,
  600. video_timebase_den,
  601. 1, # readAudioStream
  602. samples,
  603. channels,
  604. audio_start_pts,
  605. audio_end_pts,
  606. audio_timebase_num,
  607. audio_timebase_den,
  608. )
  609. assert tv_result[0].size(2) == width
  610. @pytest.mark.parametrize("test_video", test_videos.keys())
  611. def test_read_video_from_file_rescale_height(self, test_video):
  612. """
  613. Test the case when decoder starts with a video file to decode frames, and
  614. video height is set.
  615. """
  616. # video related
  617. width, height, min_dimension, max_dimension = 0, 224, 0, 0
  618. video_start_pts, video_end_pts = 0, -1
  619. video_timebase_num, video_timebase_den = 0, 1
  620. # audio related
  621. samples, channels = 0, 0
  622. audio_start_pts, audio_end_pts = 0, -1
  623. audio_timebase_num, audio_timebase_den = 0, 1
  624. full_path = os.path.join(VIDEO_DIR, test_video)
  625. tv_result = torch.ops.video_reader.read_video_from_file(
  626. full_path,
  627. SEEK_FRAME_MARGIN,
  628. 0, # getPtsOnly
  629. 1, # readVideoStream
  630. width,
  631. height,
  632. min_dimension,
  633. max_dimension,
  634. video_start_pts,
  635. video_end_pts,
  636. video_timebase_num,
  637. video_timebase_den,
  638. 1, # readAudioStream
  639. samples,
  640. channels,
  641. audio_start_pts,
  642. audio_end_pts,
  643. audio_timebase_num,
  644. audio_timebase_den,
  645. )
  646. assert tv_result[0].size(1) == height
  647. @pytest.mark.parametrize("test_video", test_videos.keys())
  648. def test_read_video_from_file_rescale_width_and_height(self, test_video):
  649. """
  650. Test the case when decoder starts with a video file to decode frames, and
  651. both video height and width are set.
  652. """
  653. # video related
  654. width, height, min_dimension, max_dimension = 320, 240, 0, 0
  655. video_start_pts, video_end_pts = 0, -1
  656. video_timebase_num, video_timebase_den = 0, 1
  657. # audio related
  658. samples, channels = 0, 0
  659. audio_start_pts, audio_end_pts = 0, -1
  660. audio_timebase_num, audio_timebase_den = 0, 1
  661. full_path = os.path.join(VIDEO_DIR, test_video)
  662. tv_result = torch.ops.video_reader.read_video_from_file(
  663. full_path,
  664. SEEK_FRAME_MARGIN,
  665. 0, # getPtsOnly
  666. 1, # readVideoStream
  667. width,
  668. height,
  669. min_dimension,
  670. max_dimension,
  671. video_start_pts,
  672. video_end_pts,
  673. video_timebase_num,
  674. video_timebase_den,
  675. 1, # readAudioStream
  676. samples,
  677. channels,
  678. audio_start_pts,
  679. audio_end_pts,
  680. audio_timebase_num,
  681. audio_timebase_den,
  682. )
  683. assert tv_result[0].size(1) == height
  684. assert tv_result[0].size(2) == width
  685. @pytest.mark.parametrize("test_video", test_videos.keys())
  686. @pytest.mark.parametrize("samples", [9600, 96000])
  687. def test_read_video_from_file_audio_resampling(self, test_video, samples):
  688. """
  689. Test the case when decoder starts with a video file to decode frames, and
  690. audio waveform are resampled
  691. """
  692. # video related
  693. width, height, min_dimension, max_dimension = 0, 0, 0, 0
  694. video_start_pts, video_end_pts = 0, -1
  695. video_timebase_num, video_timebase_den = 0, 1
  696. # audio related
  697. channels = 0
  698. audio_start_pts, audio_end_pts = 0, -1
  699. audio_timebase_num, audio_timebase_den = 0, 1
  700. full_path = os.path.join(VIDEO_DIR, test_video)
  701. tv_result = torch.ops.video_reader.read_video_from_file(
  702. full_path,
  703. SEEK_FRAME_MARGIN,
  704. 0, # getPtsOnly
  705. 1, # readVideoStream
  706. width,
  707. height,
  708. min_dimension,
  709. max_dimension,
  710. video_start_pts,
  711. video_end_pts,
  712. video_timebase_num,
  713. video_timebase_den,
  714. 1, # readAudioStream
  715. samples,
  716. channels,
  717. audio_start_pts,
  718. audio_end_pts,
  719. audio_timebase_num,
  720. audio_timebase_den,
  721. )
  722. (
  723. vframes,
  724. vframe_pts,
  725. vtimebase,
  726. vfps,
  727. vduration,
  728. aframes,
  729. aframe_pts,
  730. atimebase,
  731. asample_rate,
  732. aduration,
  733. ) = tv_result
  734. if aframes.numel() > 0:
  735. assert samples == asample_rate.item()
  736. assert 1 == aframes.size(1)
  737. # when audio stream is found
  738. duration = float(aframe_pts[-1]) * float(atimebase[0]) / float(atimebase[1])
  739. assert aframes.size(0) == approx(int(duration * asample_rate.item()), abs=0.1 * asample_rate.item())
  740. @pytest.mark.parametrize("test_video,config", test_videos.items())
  741. def test_compare_read_video_from_memory_and_file(self, test_video, config):
  742. """
  743. Test the case when video is already in memory, and decoder reads data in memory
  744. """
  745. # video related
  746. width, height, min_dimension, max_dimension = 0, 0, 0, 0
  747. video_start_pts, video_end_pts = 0, -1
  748. video_timebase_num, video_timebase_den = 0, 1
  749. # audio related
  750. samples, channels = 0, 0
  751. audio_start_pts, audio_end_pts = 0, -1
  752. audio_timebase_num, audio_timebase_den = 0, 1
  753. full_path, video_tensor = _get_video_tensor(VIDEO_DIR, test_video)
  754. # pass 1: decode all frames using cpp decoder
  755. tv_result_memory = torch.ops.video_reader.read_video_from_memory(
  756. video_tensor,
  757. SEEK_FRAME_MARGIN,
  758. 0, # getPtsOnly
  759. 1, # readVideoStream
  760. width,
  761. height,
  762. min_dimension,
  763. max_dimension,
  764. video_start_pts,
  765. video_end_pts,
  766. video_timebase_num,
  767. video_timebase_den,
  768. 1, # readAudioStream
  769. samples,
  770. channels,
  771. audio_start_pts,
  772. audio_end_pts,
  773. audio_timebase_num,
  774. audio_timebase_den,
  775. )
  776. self.check_separate_decoding_result(tv_result_memory, config)
  777. # pass 2: decode all frames from file
  778. tv_result_file = torch.ops.video_reader.read_video_from_file(
  779. full_path,
  780. SEEK_FRAME_MARGIN,
  781. 0, # getPtsOnly
  782. 1, # readVideoStream
  783. width,
  784. height,
  785. min_dimension,
  786. max_dimension,
  787. video_start_pts,
  788. video_end_pts,
  789. video_timebase_num,
  790. video_timebase_den,
  791. 1, # readAudioStream
  792. samples,
  793. channels,
  794. audio_start_pts,
  795. audio_end_pts,
  796. audio_timebase_num,
  797. audio_timebase_den,
  798. )
  799. self.check_separate_decoding_result(tv_result_file, config)
  800. # finally, compare results decoded from memory and file
  801. self.compare_decoding_result(tv_result_memory, tv_result_file)
  802. @pytest.mark.parametrize("test_video,config", test_videos.items())
  803. def test_read_video_from_memory(self, test_video, config):
  804. """
  805. Test the case when video is already in memory, and decoder reads data in memory
  806. """
  807. # video related
  808. width, height, min_dimension, max_dimension = 0, 0, 0, 0
  809. video_start_pts, video_end_pts = 0, -1
  810. video_timebase_num, video_timebase_den = 0, 1
  811. # audio related
  812. samples, channels = 0, 0
  813. audio_start_pts, audio_end_pts = 0, -1
  814. audio_timebase_num, audio_timebase_den = 0, 1
  815. full_path, video_tensor = _get_video_tensor(VIDEO_DIR, test_video)
  816. # pass 1: decode all frames using cpp decoder
  817. tv_result = torch.ops.video_reader.read_video_from_memory(
  818. video_tensor,
  819. SEEK_FRAME_MARGIN,
  820. 0, # getPtsOnly
  821. 1, # readVideoStream
  822. width,
  823. height,
  824. min_dimension,
  825. max_dimension,
  826. video_start_pts,
  827. video_end_pts,
  828. video_timebase_num,
  829. video_timebase_den,
  830. 1, # readAudioStream
  831. samples,
  832. channels,
  833. audio_start_pts,
  834. audio_end_pts,
  835. audio_timebase_num,
  836. audio_timebase_den,
  837. )
  838. # pass 2: decode all frames using av
  839. pyav_result = _decode_frames_by_av_module(full_path)
  840. self.check_separate_decoding_result(tv_result, config)
  841. self.compare_decoding_result(tv_result, pyav_result, config)
  842. @pytest.mark.parametrize("test_video,config", test_videos.items())
  843. def test_read_video_from_memory_get_pts_only(self, test_video, config):
  844. """
  845. Test the case when video is already in memory, and decoder reads data in memory.
  846. Compare frame pts between decoding for pts only and full decoding
  847. for both pts and frame data
  848. """
  849. # video related
  850. width, height, min_dimension, max_dimension = 0, 0, 0, 0
  851. video_start_pts, video_end_pts = 0, -1
  852. video_timebase_num, video_timebase_den = 0, 1
  853. # audio related
  854. samples, channels = 0, 0
  855. audio_start_pts, audio_end_pts = 0, -1
  856. audio_timebase_num, audio_timebase_den = 0, 1
  857. _, video_tensor = _get_video_tensor(VIDEO_DIR, test_video)
  858. # pass 1: decode all frames using cpp decoder
  859. tv_result = torch.ops.video_reader.read_video_from_memory(
  860. video_tensor,
  861. SEEK_FRAME_MARGIN,
  862. 0, # getPtsOnly
  863. 1, # readVideoStream
  864. width,
  865. height,
  866. min_dimension,
  867. max_dimension,
  868. video_start_pts,
  869. video_end_pts,
  870. video_timebase_num,
  871. video_timebase_den,
  872. 1, # readAudioStream
  873. samples,
  874. channels,
  875. audio_start_pts,
  876. audio_end_pts,
  877. audio_timebase_num,
  878. audio_timebase_den,
  879. )
  880. assert abs(config.video_fps - tv_result[3].item()) < 0.01
  881. # pass 2: decode all frames to get PTS only using cpp decoder
  882. tv_result_pts_only = torch.ops.video_reader.read_video_from_memory(
  883. video_tensor,
  884. SEEK_FRAME_MARGIN,
  885. 1, # getPtsOnly
  886. 1, # readVideoStream
  887. width,
  888. height,
  889. min_dimension,
  890. max_dimension,
  891. video_start_pts,
  892. video_end_pts,
  893. video_timebase_num,
  894. video_timebase_den,
  895. 1, # readAudioStream
  896. samples,
  897. channels,
  898. audio_start_pts,
  899. audio_end_pts,
  900. audio_timebase_num,
  901. audio_timebase_den,
  902. )
  903. assert not tv_result_pts_only[0].numel()
  904. assert not tv_result_pts_only[5].numel()
  905. self.compare_decoding_result(tv_result, tv_result_pts_only)
  906. @pytest.mark.parametrize("test_video,config", test_videos.items())
  907. @pytest.mark.parametrize("num_frames", [4, 8, 16, 32, 64, 128])
  908. def test_read_video_in_range_from_memory(self, test_video, config, num_frames):
  909. """
  910. Test the case when video is already in memory, and decoder reads data in memory.
  911. In addition, decoder takes meaningful start- and end PTS as input, and decode
  912. frames within that interval
  913. """
  914. full_path, video_tensor = _get_video_tensor(VIDEO_DIR, test_video)
  915. # video related
  916. width, height, min_dimension, max_dimension = 0, 0, 0, 0
  917. video_start_pts, video_end_pts = 0, -1
  918. video_timebase_num, video_timebase_den = 0, 1
  919. # audio related
  920. samples, channels = 0, 0
  921. audio_start_pts, audio_end_pts = 0, -1
  922. audio_timebase_num, audio_timebase_den = 0, 1
  923. # pass 1: decode all frames using new decoder
  924. tv_result = torch.ops.video_reader.read_video_from_memory(
  925. video_tensor,
  926. SEEK_FRAME_MARGIN,
  927. 0, # getPtsOnly
  928. 1, # readVideoStream
  929. width,
  930. height,
  931. min_dimension,
  932. max_dimension,
  933. video_start_pts,
  934. video_end_pts,
  935. video_timebase_num,
  936. video_timebase_den,
  937. 1, # readAudioStream
  938. samples,
  939. channels,
  940. audio_start_pts,
  941. audio_end_pts,
  942. audio_timebase_num,
  943. audio_timebase_den,
  944. )
  945. (
  946. vframes,
  947. vframe_pts,
  948. vtimebase,
  949. vfps,
  950. vduration,
  951. aframes,
  952. aframe_pts,
  953. atimebase,
  954. asample_rate,
  955. aduration,
  956. ) = tv_result
  957. assert abs(config.video_fps - vfps.item()) < 0.01
  958. start_pts_ind_max = vframe_pts.size(0) - num_frames
  959. if start_pts_ind_max <= 0:
  960. return
  961. # randomly pick start pts
  962. start_pts_ind = randint(0, start_pts_ind_max)
  963. end_pts_ind = start_pts_ind + num_frames - 1
  964. video_start_pts = vframe_pts[start_pts_ind]
  965. video_end_pts = vframe_pts[end_pts_ind]
  966. video_timebase_num, video_timebase_den = vtimebase[0], vtimebase[1]
  967. if len(atimebase) > 0:
  968. # when audio stream is available
  969. audio_timebase_num, audio_timebase_den = atimebase[0], atimebase[1]
  970. audio_start_pts = _pts_convert(
  971. video_start_pts.item(),
  972. Fraction(video_timebase_num.item(), video_timebase_den.item()),
  973. Fraction(audio_timebase_num.item(), audio_timebase_den.item()),
  974. math.floor,
  975. )
  976. audio_end_pts = _pts_convert(
  977. video_end_pts.item(),
  978. Fraction(video_timebase_num.item(), video_timebase_den.item()),
  979. Fraction(audio_timebase_num.item(), audio_timebase_den.item()),
  980. math.ceil,
  981. )
  982. # pass 2: decode frames in the randomly generated range
  983. tv_result = torch.ops.video_reader.read_video_from_memory(
  984. video_tensor,
  985. SEEK_FRAME_MARGIN,
  986. 0, # getPtsOnly
  987. 1, # readVideoStream
  988. width,
  989. height,
  990. min_dimension,
  991. max_dimension,
  992. video_start_pts,
  993. video_end_pts,
  994. video_timebase_num,
  995. video_timebase_den,
  996. 1, # readAudioStream
  997. samples,
  998. channels,
  999. audio_start_pts,
  1000. audio_end_pts,
  1001. audio_timebase_num,
  1002. audio_timebase_den,
  1003. )
  1004. # pass 3: decode frames in range using PyAv
  1005. video_timebase_av, audio_timebase_av = _get_timebase_by_av_module(full_path)
  1006. video_start_pts_av = _pts_convert(
  1007. video_start_pts.item(),
  1008. Fraction(video_timebase_num.item(), video_timebase_den.item()),
  1009. Fraction(video_timebase_av.numerator, video_timebase_av.denominator),
  1010. math.floor,
  1011. )
  1012. video_end_pts_av = _pts_convert(
  1013. video_end_pts.item(),
  1014. Fraction(video_timebase_num.item(), video_timebase_den.item()),
  1015. Fraction(video_timebase_av.numerator, video_timebase_av.denominator),
  1016. math.ceil,
  1017. )
  1018. if audio_timebase_av:
  1019. audio_start_pts = _pts_convert(
  1020. video_start_pts.item(),
  1021. Fraction(video_timebase_num.item(), video_timebase_den.item()),
  1022. Fraction(audio_timebase_av.numerator, audio_timebase_av.denominator),
  1023. math.floor,
  1024. )
  1025. audio_end_pts = _pts_convert(
  1026. video_end_pts.item(),
  1027. Fraction(video_timebase_num.item(), video_timebase_den.item()),
  1028. Fraction(audio_timebase_av.numerator, audio_timebase_av.denominator),
  1029. math.ceil,
  1030. )
  1031. pyav_result = _decode_frames_by_av_module(
  1032. full_path,
  1033. video_start_pts_av,
  1034. video_end_pts_av,
  1035. audio_start_pts,
  1036. audio_end_pts,
  1037. )
  1038. assert tv_result[0].size(0) == num_frames
  1039. if pyav_result.vframes.size(0) == num_frames:
  1040. # if PyAv decodes a different number of video frames, skip
  1041. # comparing the decoding results between Torchvision video reader
  1042. # and PyAv
  1043. self.compare_decoding_result(tv_result, pyav_result, config)
  1044. @pytest.mark.parametrize("test_video,config", test_videos.items())
  1045. def test_probe_video_from_file(self, test_video, config):
  1046. """
  1047. Test the case when decoder probes a video file
  1048. """
  1049. full_path = os.path.join(VIDEO_DIR, test_video)
  1050. probe_result = torch.ops.video_reader.probe_video_from_file(full_path)
  1051. self.check_probe_result(probe_result, config)
  1052. @pytest.mark.parametrize("test_video,config", test_videos.items())
  1053. def test_probe_video_from_memory(self, test_video, config):
  1054. """
  1055. Test the case when decoder probes a video in memory
  1056. """
  1057. _, video_tensor = _get_video_tensor(VIDEO_DIR, test_video)
  1058. probe_result = torch.ops.video_reader.probe_video_from_memory(video_tensor)
  1059. self.check_probe_result(probe_result, config)
  1060. @pytest.mark.parametrize("test_video,config", test_videos.items())
  1061. def test_probe_video_from_memory_script(self, test_video, config):
  1062. scripted_fun = torch.jit.script(io._probe_video_from_memory)
  1063. assert scripted_fun is not None
  1064. _, video_tensor = _get_video_tensor(VIDEO_DIR, test_video)
  1065. probe_result = scripted_fun(video_tensor)
  1066. self.check_meta_result(probe_result, config)
  1067. @pytest.mark.parametrize("test_video", test_videos.keys())
  1068. def test_read_video_from_memory_scripted(self, test_video):
  1069. """
  1070. Test the case when video is already in memory, and decoder reads data in memory
  1071. """
  1072. # video related
  1073. width, height, min_dimension, max_dimension = 0, 0, 0, 0
  1074. video_start_pts, video_end_pts = 0, -1
  1075. video_timebase_num, video_timebase_den = 0, 1
  1076. # audio related
  1077. samples, channels = 0, 0
  1078. audio_start_pts, audio_end_pts = 0, -1
  1079. audio_timebase_num, audio_timebase_den = 0, 1
  1080. scripted_fun = torch.jit.script(io._read_video_from_memory)
  1081. assert scripted_fun is not None
  1082. _, video_tensor = _get_video_tensor(VIDEO_DIR, test_video)
  1083. # decode all frames using cpp decoder
  1084. scripted_fun(
  1085. video_tensor,
  1086. SEEK_FRAME_MARGIN,
  1087. 1, # readVideoStream
  1088. width,
  1089. height,
  1090. min_dimension,
  1091. max_dimension,
  1092. [video_start_pts, video_end_pts],
  1093. video_timebase_num,
  1094. video_timebase_den,
  1095. 1, # readAudioStream
  1096. samples,
  1097. channels,
  1098. [audio_start_pts, audio_end_pts],
  1099. audio_timebase_num,
  1100. audio_timebase_den,
  1101. )
  1102. # FUTURE: check value of video / audio frames
  1103. def test_invalid_file(self):
  1104. set_video_backend("video_reader")
  1105. with pytest.raises(RuntimeError):
  1106. io.read_video("foo.mp4")
  1107. set_video_backend("pyav")
  1108. with pytest.raises(RuntimeError):
  1109. io.read_video("foo.mp4")
  1110. @pytest.mark.parametrize("test_video", test_videos.keys())
  1111. @pytest.mark.parametrize("backend", ["video_reader", "pyav"])
  1112. @pytest.mark.parametrize("start_offset", [0, 500])
  1113. @pytest.mark.parametrize("end_offset", [3000, None])
  1114. def test_audio_present_pts(self, test_video, backend, start_offset, end_offset):
  1115. """Test if audio frames are returned with pts unit."""
  1116. full_path = os.path.join(VIDEO_DIR, test_video)
  1117. container = av.open(full_path)
  1118. if container.streams.audio:
  1119. set_video_backend(backend)
  1120. _, audio, _ = io.read_video(full_path, start_offset, end_offset, pts_unit="pts")
  1121. assert all([dimension > 0 for dimension in audio.shape[:2]])
  1122. @pytest.mark.parametrize("test_video", test_videos.keys())
  1123. @pytest.mark.parametrize("backend", ["video_reader", "pyav"])
  1124. @pytest.mark.parametrize("start_offset", [0, 0.1])
  1125. @pytest.mark.parametrize("end_offset", [0.3, None])
  1126. def test_audio_present_sec(self, test_video, backend, start_offset, end_offset):
  1127. """Test if audio frames are returned with sec unit."""
  1128. full_path = os.path.join(VIDEO_DIR, test_video)
  1129. container = av.open(full_path)
  1130. if container.streams.audio:
  1131. set_video_backend(backend)
  1132. _, audio, _ = io.read_video(full_path, start_offset, end_offset, pts_unit="sec")
  1133. assert all([dimension > 0 for dimension in audio.shape[:2]])
  1134. if __name__ == "__main__":
  1135. pytest.main([__file__])
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...