test_video.py 13.3 KB
Newer Older
1
2
3
4
5
6
import os
import collections
import contextlib
import tempfile
import unittest
import random
7
import sys
8

9
10
import itertools

11
12
13
14
15

import numpy as np

import torch
import torchvision
16
from torchvision.io import _HAS_VIDEO_OPT, VideoReader
17
from common_utils import PY39_SKIP
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

try:
    import av

    # Do a version test too
    torchvision.io.video._check_av_available()
except ImportError:
    av = None


VIDEO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "videos")

CheckerConfig = [
    "duration",
    "video_fps",
    "audio_sample_rate",
    # We find for some videos (e.g. HMDB51 videos), the decoded audio frames and pts are
    # slightly different between TorchVision decoder and PyAv decoder. So omit it during check
    "check_aframes",
    "check_aframe_pts",
]
GroundTruth = collections.namedtuple("GroundTruth", " ".join(CheckerConfig))

all_check_config = GroundTruth(
    duration=0,
    video_fps=0,
    audio_sample_rate=0,
    check_aframes=True,
    check_aframe_pts=True,
)

test_videos = {
    "RATRACE_wave_f_nm_np1_fr_goo_37.avi": GroundTruth(
        duration=2.0,
        video_fps=30.0,
        audio_sample_rate=None,
        check_aframes=True,
        check_aframe_pts=True,
    ),
    "SchoolRulesHowTheyHelpUs_wave_f_nm_np1_ba_med_0.avi": GroundTruth(
        duration=2.0,
        video_fps=30.0,
        audio_sample_rate=None,
        check_aframes=True,
        check_aframe_pts=True,
    ),
    "TrumanShow_wave_f_nm_np1_fr_med_26.avi": GroundTruth(
        duration=2.0,
        video_fps=30.0,
        audio_sample_rate=None,
        check_aframes=True,
        check_aframe_pts=True,
    ),
    "v_SoccerJuggling_g23_c01.avi": GroundTruth(
        duration=8.0,
        video_fps=29.97,
        audio_sample_rate=None,
        check_aframes=True,
        check_aframe_pts=True,
    ),
    "v_SoccerJuggling_g24_c01.avi": GroundTruth(
        duration=8.0,
        video_fps=29.97,
        audio_sample_rate=None,
        check_aframes=True,
        check_aframe_pts=True,
    ),
    # Last three test segfault on video reader (see issues)
    "R6llTwEh07w.mp4": GroundTruth(
        duration=10.0,
        video_fps=30.0,
        audio_sample_rate=44100,
        # PyAv miss one audio frame at the beginning (pts=0)
        check_aframes=False,
        check_aframe_pts=False,
    ),
    "SOX5yA1l24A.mp4": GroundTruth(
        duration=11.0,
        video_fps=29.97,
        audio_sample_rate=48000,
        # PyAv miss one audio frame at the beginning (pts=0)
        check_aframes=False,
        check_aframe_pts=False,
    ),
    "WUzgd7C1pWA.mp4": GroundTruth(
        duration=11.0,
        video_fps=29.97,
        audio_sample_rate=48000,
        # PyAv miss one audio frame at the beginning (pts=0)
        check_aframes=False,
        check_aframe_pts=False,
    ),
}

DecoderResult = collections.namedtuple(
    "DecoderResult", "vframes vframe_pts vtimebase aframes aframe_pts atimebase"
)


def _read_from_stream(
    container, start_pts, end_pts, stream, stream_name, buffer_size=4
):
    """
    Args:
        container: pyav container
        start_pts/end_pts: the starting/ending Presentation TimeStamp where
            frames are read
        stream: pyav stream
        stream_name: a dictionary of streams. For example, {"video": 0} means
            video stream at stream index 0
        buffer_size: pts of frames decoded by PyAv is not guaranteed to be in
            ascending order. We need to decode more frames even when we meet end
            pts
    """
    # seeking in the stream is imprecise. Thus, seek to an ealier PTS by a margin
    margin = 1
    seek_offset = max(start_pts - margin, 0)

    container.seek(seek_offset, any_frame=False, backward=True, stream=stream)
    frames = {}
    buffer_count = 0
    for frame in container.decode(**stream_name):
        if frame.pts < start_pts:
            continue
        if frame.pts <= end_pts:
            frames[frame.pts] = frame
        else:
            buffer_count += 1
            if buffer_count >= buffer_size:
                break
    result = [frames[pts] for pts in sorted(frames)]

    return result


def _fraction_to_tensor(fraction):
    ret = torch.zeros([2], dtype=torch.int32)
    ret[0] = fraction.numerator
    ret[1] = fraction.denominator
    return ret


def _decode_frames_by_av_module(
    full_path,
    video_start_pts=0,
    video_end_pts=None,
    audio_start_pts=0,
    audio_end_pts=None,
):
    """
    Use PyAv to decode video frames. This provides a reference for our decoder
    to compare the decoding results.
    Input arguments:
        full_path: video file path
        video_start_pts/video_end_pts: the starting/ending Presentation TimeStamp where
            frames are read
    """
    if video_end_pts is None:
        video_end_pts = float("inf")
    if audio_end_pts is None:
        audio_end_pts = float("inf")
    container = av.open(full_path)

    video_frames = []
    vtimebase = torch.zeros([0], dtype=torch.int32)
    if container.streams.video:
        video_frames = _read_from_stream(
            container,
            video_start_pts,
            video_end_pts,
            container.streams.video[0],
            {"video": 0},
        )
        # container.streams.video[0].average_rate is not a reliable estimator of
        # frame rate. It can be wrong for certain codec, such as VP80
        # So we do not return video fps here
        vtimebase = _fraction_to_tensor(container.streams.video[0].time_base)

    audio_frames = []
    atimebase = torch.zeros([0], dtype=torch.int32)
    if container.streams.audio:
        audio_frames = _read_from_stream(
            container,
            audio_start_pts,
            audio_end_pts,
            container.streams.audio[0],
            {"audio": 0},
        )
        atimebase = _fraction_to_tensor(container.streams.audio[0].time_base)

    container.close()
    vframes = [frame.to_rgb().to_ndarray() for frame in video_frames]
    vframes = torch.as_tensor(np.stack(vframes))

    vframe_pts = torch.tensor([frame.pts for frame in video_frames], dtype=torch.int64)

    aframes = [frame.to_ndarray() for frame in audio_frames]
    if aframes:
        aframes = np.transpose(np.concatenate(aframes, axis=1))
        aframes = torch.as_tensor(aframes)
    else:
        aframes = torch.empty((1, 0), dtype=torch.float32)

    aframe_pts = torch.tensor(
        [audio_frame.pts for audio_frame in audio_frames], dtype=torch.int64
    )

    return DecoderResult(
        vframes=vframes.permute(0, 3, 1, 2),
        vframe_pts=vframe_pts,
        vtimebase=vtimebase,
        aframes=aframes,
        aframe_pts=aframe_pts,
        atimebase=atimebase,
    )


def _template_read_video(video_object, s=0, e=None):

    if e is None:
        e = float("inf")
    if e < s:
        raise ValueError(
            "end time should be larger than start time, got "
            "start time={} and end time={}".format(s, e)
        )
    video_object.set_current_stream("video")
    video_object.seek(s)
    video_frames = torch.empty(0)
    frames = []
    video_pts = []
Bruno Korbar's avatar
Bruno Korbar committed
249
250
    for frame in itertools.takewhile(lambda x: x['pts'] <= e, video_object):
        if frame['pts'] < s:
251
            continue
Bruno Korbar's avatar
Bruno Korbar committed
252
253
        frames.append(frame['data'])
        video_pts.append(frame['pts'])
254
255
256
257
258
259
260
261
    if len(frames) > 0:
        video_frames = torch.stack(frames, 0)

    video_object.set_current_stream("audio")
    video_object.seek(s)
    audio_frames = torch.empty(0)
    frames = []
    audio_pts = []
Bruno Korbar's avatar
Bruno Korbar committed
262
263
    for frame in itertools.takewhile(lambda x: x['pts'] <= e, video_object):
        if frame['pts'] < s:
264
            continue
Bruno Korbar's avatar
Bruno Korbar committed
265
266
        frames.append(frame['data'])
        audio_pts.append(frame['pts'])
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
    if len(frames) > 0:
        audio_frames = torch.stack(frames, 0)

    return DecoderResult(
        vframes=video_frames,
        vframe_pts=video_pts,
        vtimebase=None,
        aframes=audio_frames,
        aframe_pts=audio_pts,
        atimebase=None,
    )
    return video_frames, audio_frames, video_object.get_metadata()


@unittest.skipIf(_HAS_VIDEO_OPT is False, "Didn't compile with ffmpeg")
class TestVideo(unittest.TestCase):
283
    @PY39_SKIP
284
285
286
287
288
289
290
291
292
293
294
295
296
    @unittest.skipIf(av is None, "PyAV unavailable")
    def test_read_video_tensor(self):
        """
        Check if reading the video using the `next` based API yields the
        same sized tensors as the pyav alternative.
        """
        torchvision.set_video_backend("pyav")
        for test_video, config in test_videos.items():
            full_path = os.path.join(VIDEO_DIR, test_video)
            # pass 1: decode all frames using existing TV decoder
            tv_result, _, _ = torchvision.io.read_video(full_path, pts_unit="sec")
            tv_result = tv_result.permute(0, 3, 1, 2)
            # pass 2: decode all frames using new api
297
            reader = VideoReader(full_path, "video")
298
            frames = []
Bruno Korbar's avatar
Bruno Korbar committed
299
300
            for frame in reader:
                frames.append(frame['data'])
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
            new_api = torch.stack(frames, 0)
            self.assertEqual(tv_result.size(), new_api.size())

    # def test_partial_video_reading_fn(self):
    #     torchvision.set_video_backend("video_reader")
    #     for test_video, config in test_videos.items():
    #         full_path = os.path.join(VIDEO_DIR, test_video)

    #         # select two random points between 0 and duration
    #         r = []
    #         r.append(random.uniform(0, config.duration))
    #         r.append(random.uniform(0, config.duration))
    #         s = min(r)
    #         e = max(r)

316
    #         reader = VideoReader(full_path, "video")
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    #         results = _template_read_video(reader, s, e)
    #         tv_video, tv_audio, info = torchvision.io.read_video(
    #             full_path, start_pts=s, end_pts=e, pts_unit="sec"
    #         )
    #         self.assertAlmostEqual(tv_video.size(0), results.vframes.size(0), delta=2.0)

    # def test_pts(self):
    #     """
    #     Check if every frame read from
    #     """
    #     torchvision.set_video_backend("video_reader")
    #     for test_video, config in test_videos.items():
    #         full_path = os.path.join(VIDEO_DIR, test_video)

    #         tv_timestamps, _ = torchvision.io.read_video_timestamps(
    #             full_path, pts_unit="sec"
    #         )
    #         # pass 2: decode all frames using new api
335
    #         reader = VideoReader(full_path, "video")
336
    #         pts = []
337
338
    #         t, p = next(reader)
    #         while t.numel() > 0:  # THIS NEEDS TO BE FIXED
339
    #             pts.append(p)
340
    #             t, p = next(reader)
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358

    #         tv_timestamps = [float(p) for p in tv_timestamps]
    #         napi_pts = [float(p) for p in pts]
    #         for i in range(len(napi_pts)):
    #             self.assertAlmostEqual(napi_pts[i], tv_timestamps[i], delta=0.001)
    #     # check if pts of video frames are sorted in ascending order
    #     for i in range(len(napi_pts) - 1):
    #         self.assertEqual(napi_pts[i] < napi_pts[i + 1], True)

    @unittest.skipIf(av is None, "PyAV unavailable")
    def test_metadata(self):
        """
        Test that the metadata returned via pyav corresponds to the one returned
        by the new video decoder API
        """
        torchvision.set_video_backend("pyav")
        for test_video, config in test_videos.items():
            full_path = os.path.join(VIDEO_DIR, test_video)
359
            reader = VideoReader(full_path, "video")
360
361
362
363
364
365
366
367
            reader_md = reader.get_metadata()
            self.assertAlmostEqual(
                config.video_fps, reader_md["video"]["fps"][0], delta=0.0001
            )
            self.assertAlmostEqual(
                config.duration, reader_md["video"]["duration"][0], delta=0.5
            )

368
    @PY39_SKIP
369
370
371
372
373
374
375
376
377
378
    @unittest.skipIf(av is None, "PyAV unavailable")
    def test_video_reading_fn(self):
        """
        Test that the outputs of the pyav and ffmpeg outputs are mostly the same
        """
        for test_video, config in test_videos.items():
            full_path = os.path.join(VIDEO_DIR, test_video)

            ref_result = _decode_frames_by_av_module(full_path)

379
            reader = VideoReader(full_path, "video")
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
            newapi_result = _template_read_video(reader)

            # First we check if the frames are approximately the same
            # (note that every codec context has signature artefacts which
            # make a direct comparison not feasible)
            if newapi_result.vframes.numel() > 0 and ref_result.vframes.numel() > 0:
                mean_delta = torch.mean(
                    torch.abs(
                        newapi_result.vframes.float() - ref_result.vframes.float()
                    )
                )
            self.assertAlmostEqual(mean_delta, 0, delta=8.0)

            # Just a sanity check: are the two of the correct size?
            self.assertEqual(newapi_result.vframes.size(), ref_result.vframes.size())

            # Lastly, we compare the resulting audio streams
            if (
                config.check_aframes
                and newapi_result.aframes.numel() > 0
                and ref_result.aframes.numel() > 0
            ):
                """Audio stream is available and audio frame is required to return
                from decoder"""
                is_same = torch.all(
                    torch.eq(newapi_result.aframes, ref_result.aframes)
                ).item()
                self.assertEqual(is_same, True)


if __name__ == "__main__":
    unittest.main()