Skip to content

Commit

Permalink
add from_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
DexerBR authored and WyattBlue committed Sep 3, 2024
1 parent 1411bf7 commit 5dc17e8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
9 changes: 9 additions & 0 deletions av/video/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ class VideoFrame(Frame):
) -> VideoFrame: ...
@staticmethod
def from_ndarray(array: _SupportedNDarray, format: str = "rgb24") -> VideoFrame: ...
@staticmethod
def from_bytes(
data: bytes,
width: int,
height: int,
format="rgba",
flip_horizontal=False,
flip_vertical=False,
) -> VideoFrame: ...
40 changes: 34 additions & 6 deletions av/video/frame.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ cdef byteswap_array(array, bint big_endian):
return array


cdef copy_array_to_plane(array, VideoPlane plane, unsigned int bytes_per_pixel):
cdef bytes imgbytes = array.tobytes()
cdef const uint8_t[:] i_buf = imgbytes
cdef copy_bytes_to_plane(img_bytes, VideoPlane plane, unsigned int bytes_per_pixel, bint flip_horizontal, bint flip_vertical):
cdef const uint8_t[:] i_buf = img_bytes
cdef size_t i_pos = 0
cdef size_t i_stride = plane.width * bytes_per_pixel
cdef size_t i_size = plane.height * i_stride
Expand All @@ -51,12 +50,33 @@ cdef copy_array_to_plane(array, VideoPlane plane, unsigned int bytes_per_pixel):
cdef size_t o_pos = 0
cdef size_t o_stride = abs(plane.line_size)

while i_pos < i_size:
o_buf[o_pos:o_pos + i_stride] = i_buf[i_pos:i_pos + i_stride]
i_pos += i_stride
cdef int start_row, end_row, step
if flip_vertical:
start_row = plane.height - 1
end_row = -1
step = -1
else:
start_row = 0
end_row = plane.height
step = 1

cdef int i, j
for row in range(start_row, end_row, step):
i_pos = row * i_stride
if flip_horizontal:
for i in range(0, i_stride, bytes_per_pixel):
for j in range(bytes_per_pixel):
o_buf[o_pos + i + j] = i_buf[i_pos + i_stride - i - bytes_per_pixel + j]
else:
o_buf[o_pos:o_pos + i_stride] = i_buf[i_pos:i_pos + i_stride]
o_pos += o_stride


cdef copy_array_to_plane(array, VideoPlane plane, unsigned int bytes_per_pixel):
cdef bytes imgbytes = array.tobytes()
copy_bytes_to_plane(imgbytes, plane, bytes_per_pixel, False, False)


cdef useful_array(VideoPlane plane, unsigned int bytes_per_pixel=1, str dtype="uint8"):
"""
Return the useful part of the VideoPlane as a single dimensional array.
Expand Down Expand Up @@ -570,3 +590,11 @@ cdef class VideoFrame(Frame):
copy_array_to_plane(array, frame.planes[0], 1 if array.ndim == 2 else array.shape[2])

return frame

def from_bytes(img_bytes: bytes, width: int, height: int, format="rgba", flip_horizontal=False, flip_vertical=False):
frame = VideoFrame(width, height, format)
if format == "rgba":
copy_bytes_to_plane(img_bytes, frame.planes[0], 4, flip_horizontal, flip_vertical)
else:
raise NotImplementedError(f"Format '{format}' is not supported.")
return frame

0 comments on commit 5dc17e8

Please sign in to comment.