| 1 |
import cython |
| 2 |
from cython.cimports import libav as lib |
| 3 |
from cython.cimports.av.sidedata.sidedata import SideData |
| 4 |
from cython.cimports.libc.stdint import uintptr_t |
| 5 |
|
| 6 |
_cinit_bypass_sentinel = cython.declare(object, object()) |
| 7 |
|
| 8 |
|
| 9 |
@cython.final |
| 10 |
@cython.cclass |
| 11 |
class MotionVectors(SideData): |
| 12 |
def __init__(self, sentinel, frame: Frame, index: cython.int): |
| 13 |
SideData.__init__(self, sentinel, frame, index) |
| 14 |
self._vectors = {} |
| 15 |
self._len = self.ptr.size // cython.sizeof(lib.AVMotionVector) |
| 16 |
|
| 17 |
def __repr__(self): |
| 18 |
return ( |
| 19 |
f"<av.sidedata.MotionVectors {self.ptr.size} bytes " |
| 20 |
f"of {len(self)} vectors at 0x{cython.cast(uintptr_t, self.ptr.data):0x}>" |
| 21 |
) |
| 22 |
|
| 23 |
def __len__(self): |
| 24 |
return self._len |
| 25 |
|
| 26 |
def __getitem__(self, index: cython.Py_ssize_t): |
| 27 |
try: |
| 28 |
return self._vectors[index] |
| 29 |
except KeyError: |
| 30 |
pass |
| 31 |
|
| 32 |
if index >= self._len: |
| 33 |
raise IndexError(index) |
| 34 |
|
| 35 |
vector = self._vectors[index] = MotionVector( |
| 36 |
_cinit_bypass_sentinel, self, index |
| 37 |
) |
| 38 |
return vector |
| 39 |
|
| 40 |
def __iter__(self): |
| 41 |
"""Iterate over all motion vectors.""" |
| 42 |
for i in range(self._len): |
| 43 |
yield self[i] |
| 44 |
|
| 45 |
def to_ndarray(self): |
| 46 |
""" |
| 47 |
Convert motion vectors to a NumPy structured array. |
| 48 |
|
| 49 |
Returns a NumPy array with fields corresponding to the AVMotionVector structure. |
| 50 |
""" |
| 51 |
import numpy as np |
| 52 |
|
| 53 |
return np.frombuffer( |
| 54 |
self, |
| 55 |
dtype=np.dtype( |
| 56 |
[ |
| 57 |
("source", "int32"), |
| 58 |
("w", "uint8"), |
| 59 |
("h", "uint8"), |
| 60 |
("src_x", "int16"), |
| 61 |
("src_y", "int16"), |
| 62 |
("dst_x", "int16"), |
| 63 |
("dst_y", "int16"), |
| 64 |
("flags", "uint64"), |
| 65 |
("motion_x", "int32"), |
| 66 |
("motion_y", "int32"), |
| 67 |
("motion_scale", "uint16"), |
| 68 |
], |
| 69 |
align=True, |
| 70 |
), |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
@cython.final |
| 75 |
@cython.cclass |
| 76 |
class MotionVector: |
| 77 |
""" |
| 78 |
Represents a single motion vector from video frame data. |
| 79 |
|
| 80 |
Motion vectors describe the motion of a block of pixels between frames. |
| 81 |
""" |
| 82 |
|
| 83 |
def __init__(self, sentinel, parent: MotionVectors, index: cython.int): |
| 84 |
if sentinel is not _cinit_bypass_sentinel: |
| 85 |
raise RuntimeError("cannot manually instantiate MotionVector") |
| 86 |
self.parent = parent |
| 87 |
base: cython.pointer[lib.AVMotionVector] = cython.cast( |
| 88 |
cython.pointer[lib.AVMotionVector], parent.ptr.data |
| 89 |
) |
| 90 |
self.ptr = base + index |
| 91 |
|
| 92 |
def __repr__(self): |
| 93 |
return ( |
| 94 |
f"<av.sidedata.MotionVector {self.w}x{self.h} " |
| 95 |
f"from ({self.src_x},{self.src_y}) to ({self.dst_x},{self.dst_y})>" |
| 96 |
) |
| 97 |
|
| 98 |
@property |
| 99 |
def source(self): |
| 100 |
return self.ptr.source |
| 101 |
|
| 102 |
@property |
| 103 |
def w(self): |
| 104 |
return self.ptr.w |
| 105 |
|
| 106 |
@property |
| 107 |
def h(self): |
| 108 |
return self.ptr.h |
| 109 |
|
| 110 |
@property |
| 111 |
def src_x(self): |
| 112 |
return self.ptr.src_x |
| 113 |
|
| 114 |
@property |
| 115 |
def src_y(self): |
| 116 |
return self.ptr.src_y |
| 117 |
|
| 118 |
@property |
| 119 |
def dst_x(self): |
| 120 |
return self.ptr.dst_x |
| 121 |
|
| 122 |
@property |
| 123 |
def dst_y(self): |
| 124 |
return self.ptr.dst_y |
| 125 |
|
| 126 |
@property |
| 127 |
def motion_x(self): |
| 128 |
return self.ptr.motion_x |
| 129 |
|
| 130 |
@property |
| 131 |
def motion_y(self): |
| 132 |
return self.ptr.motion_y |
| 133 |
|
| 134 |
@property |
| 135 |
def motion_scale(self): |
| 136 |
return self.ptr.motion_scale |