_video_opt.py 19.9 KB
Newer Older
Francisco Massa's avatar
Francisco Massa committed
1
import math
2
3
import warnings
from fractions import Fraction
4
from typing import Dict, List, Optional, Tuple, Union
5

6
import torch
7

8
from ..extension import _load_library
9
10
11


try:
12
    _load_library("video_reader")
13
    _HAS_VIDEO_OPT = True
14
except (ImportError, OSError):
15
    _HAS_VIDEO_OPT = False
16
17
18
19

default_timebase = Fraction(0, 1)


20
21
# simple class for torch scripting
# the complex Fraction class from fractions module is not scriptable
22
class Timebase:
23
24
25
26
27
    __annotations__ = {"numerator": int, "denominator": int}
    __slots__ = ["numerator", "denominator"]

    def __init__(
        self,
28
29
30
        numerator: int,
        denominator: int,
    ) -> None:
31
32
33
34
        self.numerator = numerator
        self.denominator = denominator


35
class VideoMetaData:
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    __annotations__ = {
        "has_video": bool,
        "video_timebase": Timebase,
        "video_duration": float,
        "video_fps": float,
        "has_audio": bool,
        "audio_timebase": Timebase,
        "audio_duration": float,
        "audio_sample_rate": float,
    }
    __slots__ = [
        "has_video",
        "video_timebase",
        "video_duration",
        "video_fps",
        "has_audio",
        "audio_timebase",
        "audio_duration",
        "audio_sample_rate",
    ]

57
    def __init__(self) -> None:
58
59
60
61
62
63
64
65
66
67
        self.has_video = False
        self.video_timebase = Timebase(0, 1)
        self.video_duration = 0.0
        self.video_fps = 0.0
        self.has_audio = False
        self.audio_timebase = Timebase(0, 1)
        self.audio_duration = 0.0
        self.audio_sample_rate = 0.0


68
def _validate_pts(pts_range: Tuple[int, int]) -> None:
69

70
71
72
    if pts_range[0] > pts_range[1] > 0:
        raise ValueError(
            f"Start pts should not be smaller than end pts, got start pts: {pts_range[0]} and end pts: {pts_range[1]}"
73
        )
74
75


76
77
78
79
80
81
82
83
def _fill_info(
    vtimebase: torch.Tensor,
    vfps: torch.Tensor,
    vduration: torch.Tensor,
    atimebase: torch.Tensor,
    asample_rate: torch.Tensor,
    aduration: torch.Tensor,
) -> VideoMetaData:
84
85
86
87
    """
    Build update VideoMetaData struct with info about the video
    """
    meta = VideoMetaData()
88
    if vtimebase.numel() > 0:
89
        meta.video_timebase = Timebase(int(vtimebase[0].item()), int(vtimebase[1].item()))
90
        timebase = vtimebase[0].item() / float(vtimebase[1].item())
91
        if vduration.numel() > 0:
92
93
            meta.has_video = True
            meta.video_duration = float(vduration.item()) * timebase
94
    if vfps.numel() > 0:
95
        meta.video_fps = float(vfps.item())
96
    if atimebase.numel() > 0:
97
        meta.audio_timebase = Timebase(int(atimebase[0].item()), int(atimebase[1].item()))
98
        timebase = atimebase[0].item() / float(atimebase[1].item())
99
        if aduration.numel() > 0:
100
101
            meta.has_audio = True
            meta.audio_duration = float(aduration.item()) * timebase
102
    if asample_rate.numel() > 0:
103
        meta.audio_sample_rate = float(asample_rate.item())
104

105
    return meta
106
107


108
109
110
def _align_audio_frames(
    aframes: torch.Tensor, aframe_pts: torch.Tensor, audio_pts_range: Tuple[int, int]
) -> torch.Tensor:
111
112
113
114
115
116
117
    start, end = aframe_pts[0], aframe_pts[-1]
    num_samples = aframes.size(0)
    step_per_aframe = float(end - start + 1) / float(num_samples)
    s_idx = 0
    e_idx = num_samples
    if start < audio_pts_range[0]:
        s_idx = int((audio_pts_range[0] - start) / step_per_aframe)
118
    if audio_pts_range[1] != -1 and end > audio_pts_range[1]:
119
120
121
122
123
        e_idx = int((audio_pts_range[1] - end) / step_per_aframe)
    return aframes[s_idx:e_idx, :]


def _read_video_from_file(
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    filename: str,
    seek_frame_margin: float = 0.25,
    read_video_stream: bool = True,
    video_width: int = 0,
    video_height: int = 0,
    video_min_dimension: int = 0,
    video_max_dimension: int = 0,
    video_pts_range: Tuple[int, int] = (0, -1),
    video_timebase: Fraction = default_timebase,
    read_audio_stream: bool = True,
    audio_samples: int = 0,
    audio_channels: int = 0,
    audio_pts_range: Tuple[int, int] = (0, -1),
    audio_timebase: Fraction = default_timebase,
) -> Tuple[torch.Tensor, torch.Tensor, VideoMetaData]:
139
    """
140
    Reads a video from a file, returning both the video frames and the audio frames
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
    Args:
    filename (str): path to the video file
    seek_frame_margin (double, optional): seeking frame in the stream is imprecise. Thus,
        when video_start_pts is specified, we seek the pts earlier by seek_frame_margin seconds
    read_video_stream (int, optional): whether read video stream. If yes, set to 1. Otherwise, 0
    video_width/video_height/video_min_dimension/video_max_dimension (int): together decide
        the size of decoded frames:

            - When video_width = 0, video_height = 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the original frame resolution
            - When video_width = 0, video_height = 0, video_min_dimension != 0,
                and video_max_dimension = 0, keep the aspect ratio and resize the
                frame so that shorter edge size is video_min_dimension
            - When video_width = 0, video_height = 0, video_min_dimension = 0,
                and video_max_dimension != 0, keep the aspect ratio and resize
                the frame so that longer edge size is video_max_dimension
            - When video_width = 0, video_height = 0, video_min_dimension != 0,
                and video_max_dimension != 0, resize the frame so that shorter
                edge size is video_min_dimension, and longer edge size is
                video_max_dimension. The aspect ratio may not be preserved
            - When video_width = 0, video_height != 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the aspect ratio and resize
                the frame so that frame video_height is $video_height
            - When video_width != 0, video_height == 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the aspect ratio and resize
                the frame so that frame video_width is $video_width
            - When video_width != 0, video_height != 0, video_min_dimension = 0,
                and video_max_dimension = 0, resize the frame so that frame
                video_width and  video_height are set to $video_width and
                $video_height, respectively
    video_pts_range (list(int), optional): the start and end presentation timestamp of video stream
    video_timebase (Fraction, optional): a Fraction rational number which denotes timebase in video stream
    read_audio_stream (int, optional): whether read audio stream. If yes, set to 1. Otherwise, 0
    audio_samples (int, optional): audio sampling rate
    audio_channels (int optional): audio channels
    audio_pts_range (list(int), optional): the start and end presentation timestamp of audio stream
    audio_timebase (Fraction, optional): a Fraction rational number which denotes time base in audio stream
179
180

    Returns
181
182
        vframes (Tensor[T, H, W, C]): the `T` video frames
        aframes (Tensor[L, K]): the audio frames, where `L` is the number of points and
183
            `K` is the number of audio_channels
184
185
        info (Dict): metadata for the video and audio. Can contain the fields video_fps (float)
            and audio_fps (int)
186
187
188
189
190
191
192
193
194
195
196
197
    """
    _validate_pts(video_pts_range)
    _validate_pts(audio_pts_range)

    result = torch.ops.video_reader.read_video_from_file(
        filename,
        seek_frame_margin,
        0,  # getPtsOnly
        read_video_stream,
        video_width,
        video_height,
        video_min_dimension,
198
        video_max_dimension,
199
200
201
202
203
204
205
206
207
208
209
210
        video_pts_range[0],
        video_pts_range[1],
        video_timebase.numerator,
        video_timebase.denominator,
        read_audio_stream,
        audio_samples,
        audio_channels,
        audio_pts_range[0],
        audio_pts_range[1],
        audio_timebase.numerator,
        audio_timebase.denominator,
    )
211
    vframes, _vframe_pts, vtimebase, vfps, vduration, aframes, aframe_pts, atimebase, asample_rate, aduration = result
212
    info = _fill_info(vtimebase, vfps, vduration, atimebase, asample_rate, aduration)
213
214
215
216
217
218
    if aframes.numel() > 0:
        # when audio stream is found
        aframes = _align_audio_frames(aframes, aframe_pts, audio_pts_range)
    return vframes, aframes, info


219
def _read_video_timestamps_from_file(filename: str) -> Tuple[List[int], List[int], VideoMetaData]:
220
221
222
223
224
225
226
227
228
229
230
231
232
    """
    Decode all video- and audio frames in the video. Only pts
    (presentation timestamp) is returned. The actual frame pixel data is not
    copied. Thus, it is much faster than read_video(...)
    """
    result = torch.ops.video_reader.read_video_from_file(
        filename,
        0,  # seek_frame_margin
        1,  # getPtsOnly
        1,  # read_video_stream
        0,  # video_width
        0,  # video_height
        0,  # video_min_dimension
233
        0,  # video_max_dimension
234
235
236
237
238
239
240
241
242
243
244
245
        0,  # video_start_pts
        -1,  # video_end_pts
        0,  # video_timebase_num
        1,  # video_timebase_den
        1,  # read_audio_stream
        0,  # audio_samples
        0,  # audio_channels
        0,  # audio_start_pts
        -1,  # audio_end_pts
        0,  # audio_timebase_num
        1,  # audio_timebase_den
    )
246
    _vframes, vframe_pts, vtimebase, vfps, vduration, _aframes, aframe_pts, atimebase, asample_rate, aduration = result
247
    info = _fill_info(vtimebase, vfps, vduration, atimebase, asample_rate, aduration)
248
249
250
251
252
253

    vframe_pts = vframe_pts.numpy().tolist()
    aframe_pts = aframe_pts.numpy().tolist()
    return vframe_pts, aframe_pts, info


254
def _probe_video_from_file(filename: str) -> VideoMetaData:
255
    """
256
    Probe a video file and return VideoMetaData with info about the video
257
258
259
260
261
262
263
    """
    result = torch.ops.video_reader.probe_video_from_file(filename)
    vtimebase, vfps, vduration, atimebase, asample_rate, aduration = result
    info = _fill_info(vtimebase, vfps, vduration, atimebase, asample_rate, aduration)
    return info


264
def _read_video_from_memory(
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    video_data: torch.Tensor,
    seek_frame_margin: float = 0.25,
    read_video_stream: int = 1,
    video_width: int = 0,
    video_height: int = 0,
    video_min_dimension: int = 0,
    video_max_dimension: int = 0,
    video_pts_range: Tuple[int, int] = (0, -1),
    video_timebase_numerator: int = 0,
    video_timebase_denominator: int = 1,
    read_audio_stream: int = 1,
    audio_samples: int = 0,
    audio_channels: int = 0,
    audio_pts_range: Tuple[int, int] = (0, -1),
    audio_timebase_numerator: int = 0,
    audio_timebase_denominator: int = 1,
) -> Tuple[torch.Tensor, torch.Tensor]:
282
    """
283
    Reads a video from memory, returning both the video frames as the audio frames
284
    This function is torchscriptable.
285

286
287
    Args:
    video_data (data type could be 1) torch.Tensor, dtype=torch.int8 or 2) python bytes):
288
        compressed video content stored in either 1) torch.Tensor 2) python bytes
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
    seek_frame_margin (double, optional): seeking frame in the stream is imprecise.
        Thus, when video_start_pts is specified, we seek the pts earlier by seek_frame_margin seconds
    read_video_stream (int, optional): whether read video stream. If yes, set to 1. Otherwise, 0
    video_width/video_height/video_min_dimension/video_max_dimension (int): together decide
        the size of decoded frames:

            - When video_width = 0, video_height = 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the original frame resolution
            - When video_width = 0, video_height = 0, video_min_dimension != 0,
                and video_max_dimension = 0, keep the aspect ratio and resize the
                frame so that shorter edge size is video_min_dimension
            - When video_width = 0, video_height = 0, video_min_dimension = 0,
                and video_max_dimension != 0, keep the aspect ratio and resize
                the frame so that longer edge size is video_max_dimension
            - When video_width = 0, video_height = 0, video_min_dimension != 0,
                and video_max_dimension != 0, resize the frame so that shorter
                edge size is video_min_dimension, and longer edge size is
                video_max_dimension. The aspect ratio may not be preserved
            - When video_width = 0, video_height != 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the aspect ratio and resize
                the frame so that frame video_height is $video_height
            - When video_width != 0, video_height == 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the aspect ratio and resize
                the frame so that frame video_width is $video_width
            - When video_width != 0, video_height != 0, video_min_dimension = 0,
                and video_max_dimension = 0, resize the frame so that frame
                video_width and  video_height are set to $video_width and
                $video_height, respectively
    video_pts_range (list(int), optional): the start and end presentation timestamp of video stream
    video_timebase_numerator / video_timebase_denominator (float, optional): a rational
        number which denotes timebase in video stream
    read_audio_stream (int, optional): whether read audio stream. If yes, set to 1. Otherwise, 0
    audio_samples (int, optional): audio sampling rate
    audio_channels (int optional): audio audio_channels
    audio_pts_range (list(int), optional): the start and end presentation timestamp of audio stream
    audio_timebase_numerator / audio_timebase_denominator (float, optional):
325
        a rational number which denotes time base in audio stream
326

327
328
329
    Returns:
        vframes (Tensor[T, H, W, C]): the `T` video frames
        aframes (Tensor[L, K]): the audio frames, where `L` is the number of points and
330
331
332
333
334
335
            `K` is the number of channels
    """

    _validate_pts(video_pts_range)
    _validate_pts(audio_pts_range)

336
    if not isinstance(video_data, torch.Tensor):
337
        with warnings.catch_warnings():
338
            # Ignore the warning because we actually don't modify the buffer in this function
339
340
            warnings.filterwarnings("ignore", message="The given buffer is not writable")
            video_data = torch.frombuffer(video_data, dtype=torch.uint8)
341

342
    result = torch.ops.video_reader.read_video_from_memory(
343
        video_data,
344
345
346
347
348
349
        seek_frame_margin,
        0,  # getPtsOnly
        read_video_stream,
        video_width,
        video_height,
        video_min_dimension,
350
        video_max_dimension,
351
352
        video_pts_range[0],
        video_pts_range[1],
353
354
        video_timebase_numerator,
        video_timebase_denominator,
355
356
357
358
359
        read_audio_stream,
        audio_samples,
        audio_channels,
        audio_pts_range[0],
        audio_pts_range[1],
360
361
        audio_timebase_numerator,
        audio_timebase_denominator,
362
363
    )

364
    vframes, _vframe_pts, vtimebase, vfps, vduration, aframes, aframe_pts, atimebase, asample_rate, aduration = result
365

366
367
368
    if aframes.numel() > 0:
        # when audio stream is found
        aframes = _align_audio_frames(aframes, aframe_pts, audio_pts_range)
369
370

    return vframes, aframes
371
372


373
374
375
def _read_video_timestamps_from_memory(
    video_data: torch.Tensor,
) -> Tuple[List[int], List[int], VideoMetaData]:
376
377
378
379
380
    """
    Decode all frames in the video. Only pts (presentation timestamp) is returned.
    The actual frame pixel data is not copied. Thus, read_video_timestamps(...)
    is much faster than read_video(...)
    """
381
    if not isinstance(video_data, torch.Tensor):
382
        with warnings.catch_warnings():
383
            # Ignore the warning because we actually don't modify the buffer in this function
384
385
            warnings.filterwarnings("ignore", message="The given buffer is not writable")
            video_data = torch.frombuffer(video_data, dtype=torch.uint8)
386
    result = torch.ops.video_reader.read_video_from_memory(
387
        video_data,
388
389
390
391
392
393
        0,  # seek_frame_margin
        1,  # getPtsOnly
        1,  # read_video_stream
        0,  # video_width
        0,  # video_height
        0,  # video_min_dimension
394
        0,  # video_max_dimension
395
396
397
398
399
400
401
402
403
404
405
406
        0,  # video_start_pts
        -1,  # video_end_pts
        0,  # video_timebase_num
        1,  # video_timebase_den
        1,  # read_audio_stream
        0,  # audio_samples
        0,  # audio_channels
        0,  # audio_start_pts
        -1,  # audio_end_pts
        0,  # audio_timebase_num
        1,  # audio_timebase_den
    )
407
    _vframes, vframe_pts, vtimebase, vfps, vduration, _aframes, aframe_pts, atimebase, asample_rate, aduration = result
408
    info = _fill_info(vtimebase, vfps, vduration, atimebase, asample_rate, aduration)
409
410
411
412

    vframe_pts = vframe_pts.numpy().tolist()
    aframe_pts = aframe_pts.numpy().tolist()
    return vframe_pts, aframe_pts, info
413
414


415
416
417
def _probe_video_from_memory(
    video_data: torch.Tensor,
) -> VideoMetaData:
418
    """
419
420
    Probe a video in memory and return VideoMetaData with info about the video
    This function is torchscriptable
421
422
    """
    if not isinstance(video_data, torch.Tensor):
423
        with warnings.catch_warnings():
424
            # Ignore the warning because we actually don't modify the buffer in this function
425
426
            warnings.filterwarnings("ignore", message="The given buffer is not writable")
            video_data = torch.frombuffer(video_data, dtype=torch.uint8)
427
428
429
430
    result = torch.ops.video_reader.probe_video_from_memory(video_data)
    vtimebase, vfps, vduration, atimebase, asample_rate, aduration = result
    info = _fill_info(vtimebase, vfps, vduration, atimebase, asample_rate, aduration)
    return info
Francisco Massa's avatar
Francisco Massa committed
431
432


433
434
435
436
437
438
def _read_video(
    filename: str,
    start_pts: Union[float, Fraction] = 0,
    end_pts: Optional[Union[float, Fraction]] = None,
    pts_unit: str = "pts",
) -> Tuple[torch.Tensor, torch.Tensor, Dict[str, float]]:
Francisco Massa's avatar
Francisco Massa committed
439
440
441
    if end_pts is None:
        end_pts = float("inf")

442
443
444
445
446
    if pts_unit == "pts":
        warnings.warn(
            "The pts_unit 'pts' gives wrong results and will be removed in a "
            + "follow-up version. Please use pts_unit 'sec'."
        )
Francisco Massa's avatar
Francisco Massa committed
447
448
449

    info = _probe_video_from_file(filename)

450
451
    has_video = info.has_video
    has_audio = info.has_audio
Francisco Massa's avatar
Francisco Massa committed
452
453

    def get_pts(time_base):
Prabhat Roy's avatar
Prabhat Roy committed
454
455
        start_offset = start_pts
        end_offset = end_pts
456
        if pts_unit == "sec":
Prabhat Roy's avatar
Prabhat Roy committed
457
            start_offset = int(math.floor(start_pts * (1 / time_base)))
Francisco Massa's avatar
Francisco Massa committed
458
            if end_offset != float("inf"):
Prabhat Roy's avatar
Prabhat Roy committed
459
                end_offset = int(math.ceil(end_pts * (1 / time_base)))
Francisco Massa's avatar
Francisco Massa committed
460
461
462
463
        if end_offset == float("inf"):
            end_offset = -1
        return start_offset, end_offset

Prabhat Roy's avatar
Prabhat Roy committed
464
465
    video_pts_range = (0, -1)
    video_timebase = default_timebase
Francisco Massa's avatar
Francisco Massa committed
466
    if has_video:
Prabhat Roy's avatar
Prabhat Roy committed
467
        video_timebase = Fraction(info.video_timebase.numerator, info.video_timebase.denominator)
Francisco Massa's avatar
Francisco Massa committed
468
469
        video_pts_range = get_pts(video_timebase)

Prabhat Roy's avatar
Prabhat Roy committed
470
471
    audio_pts_range = (0, -1)
    audio_timebase = default_timebase
Francisco Massa's avatar
Francisco Massa committed
472
    if has_audio:
Prabhat Roy's avatar
Prabhat Roy committed
473
        audio_timebase = Fraction(info.audio_timebase.numerator, info.audio_timebase.denominator)
Francisco Massa's avatar
Francisco Massa committed
474
475
        audio_pts_range = get_pts(audio_timebase)

476
    vframes, aframes, info = _read_video_from_file(
Francisco Massa's avatar
Francisco Massa committed
477
478
479
480
481
482
483
484
        filename,
        read_video_stream=True,
        video_pts_range=video_pts_range,
        video_timebase=video_timebase,
        read_audio_stream=True,
        audio_pts_range=audio_pts_range,
        audio_timebase=audio_timebase,
    )
485
486
    _info = {}
    if has_video:
487
        _info["video_fps"] = info.video_fps
488
    if has_audio:
489
        _info["audio_fps"] = info.audio_sample_rate
490
491

    return vframes, aframes, _info
Francisco Massa's avatar
Francisco Massa committed
492
493


494
495
496
def _read_video_timestamps(
    filename: str, pts_unit: str = "pts"
) -> Tuple[Union[List[int], List[Fraction]], Optional[float]]:
497
498
499
500
501
    if pts_unit == "pts":
        warnings.warn(
            "The pts_unit 'pts' gives wrong results and will be removed in a "
            + "follow-up version. Please use pts_unit 'sec'."
        )
Francisco Massa's avatar
Francisco Massa committed
502

503
    pts: Union[List[int], List[Fraction]]
Francisco Massa's avatar
Francisco Massa committed
504
505
    pts, _, info = _read_video_timestamps_from_file(filename)

506
    if pts_unit == "sec":
507
        video_time_base = Fraction(info.video_timebase.numerator, info.video_timebase.denominator)
Francisco Massa's avatar
Francisco Massa committed
508
509
        pts = [x * video_time_base for x in pts]

510
    video_fps = info.video_fps if info.has_video else None
Francisco Massa's avatar
Francisco Massa committed
511
512

    return pts, video_fps