[Perf] Bound memory during video frame conversion (#5732)

Signed-off-by: Xunzhuo Liu <xunzhuo.liu@amd.com>
This commit is contained in:
Xunzhuo authored and GitHub committed 2026-08-06 14:41:59 +08:00
1 parent 93457f29ff
commit a9ba47d8ce
2 files changed
+70 -10

No files matched your search

+24 -10
View File
@@ -481,18 +481,32 @@ def _coerce_video_to_uint8_frames(video: Any) -> np.ndarray:
if not frames:
raise ValueError("No frames found to encode.")
frames_np = np.stack(frames, axis=0)
if frames_np.ndim == 4 and frames_np.shape[-1] == 4:
frames_np = frames_np[..., :3]
frame_shape = frames[0].shape
has_alpha = len(frame_shape) == 3 and frame_shape[-1] == 4
output_shape = (*frame_shape[:-1], 3) if has_alpha else frame_shape
frames_u8 = np.empty((len(frames), *output_shape), dtype=np.uint8)
common_dtype = np.result_type(*(frame.dtype for frame in frames))
if frames_np.dtype == np.uint8:
frames_u8 = frames_np
else:
frames_np = np.clip(frames_np, 0.0, 1.0)
frames_np *= 255.0
frames_u8 = np.round(frames_np).astype(np.uint8)
# Convert one frame at a time instead of stacking the normalized float
# payload first. Long videos can otherwise require another full-size
# float array plus conversion temporaries before encoding.
for index, frame in enumerate(frames):
if frame.shape != frame_shape:
raise ValueError("All video frames must have the same shape.")
frame = frame[..., :3] if has_alpha else frame
if frame.dtype == np.uint8:
frames_u8[index] = frame
continue
return np.ascontiguousarray(frames_u8)
# np.stack(), used by the previous implementation, promoted mixed
# frame dtypes before scaling. Preserve those rounding semantics
# without allocating a full-video float buffer.
scaled = np.clip(frame.astype(common_dtype, copy=False), 0.0, 1.0)
scaled *= 255.0
np.rint(scaled, out=scaled)
frames_u8[index] = scaled
return frames_u8
def _encode_video_bytes(