The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Fix issues with loading half float files with pyav. (#16949)
comfyanonymous7f7fd918ed08
diff --git a/comfy_api/latest/_input_impl/video_types.py b/comfy_api/latest/_input_impl/video_types.pyindex b0e331b8d..91d147ed4 100644--- a/comfy_api/latest/_input_impl/video_types.py+++ b/comfy_api/latest/_input_impl/video_types.py@@ -532,10 +532,25 @@ class VideoFromFile(VideoInput):checked_alpha = True- # Fix non-deterministic video decode when the video width is not a multiple of 32- # For non-yuvj pixel formats: most H.264/H.265 video and static images (e.g. lossy WebP via LoadImage)- # Pad both axes to a multiple of 32 and smear the border so the alignment padding never bleeds into the cropped edges- if image_format in ('gbrpf32le', 'gbrapf32le') and frame.width % 32 != 0:+ if frame.format.name in (+ 'gbrpf16le', 'gbrpf16be', 'gbrapf16le', 'gbrapf16be', 'grayf16le', 'grayf16be',+ 'gbrpf32le', 'gbrpf32be', 'gbrapf32le', 'gbrapf32be', 'grayf32le', 'grayf32be',+ ):+ # Read float planes directly: swscale can clip HDR values when converting float formats.+ byte_order = '>' if frame.format.is_big_endian else '<'+ dtype = np.dtype(f'{byte_order}f{frame.format.components[0].bits // 8}')+ planes = []+ for component in frame.format.components:+ plane = frame.planes[component.plane]+ planes.append(np.ndarray(+ (frame.height, frame.width), dtype=dtype, buffer=plane,+ strides=(plane.line_size, dtype.itemsize),+ ))+ img = np.stack(planes * 3 if len(planes) == 1 else planes, axis=-1, dtype=np.float32)+ elif image_format in ('gbrpf32le', 'gbrapf32le') and frame.width % 32 != 0:+ # Fix non-deterministic video decode when the video width is not a multiple of 32+ # For non-yuvj pixel formats: most H.264/H.265 video and static images (e.g. lossy WebP via LoadImage)+ # Pad both axes to a multiple of 32 and smear the border so the alignment padding never bleeds into the cropped edgesif align_graph is None:pad_w = ((frame.width + 31) // 32) * 32pad_h = ((frame.height + 31) // 32) * 32diff --git a/tests-unit/comfy_api_test/video_types_test.py b/tests-unit/comfy_api_test/video_types_test.pyindex 713088998..b0b21f4cb 100644--- a/tests-unit/comfy_api_test/video_types_test.py+++ b/tests-unit/comfy_api_test/video_types_test.py@@ -135,6 +135,44 @@ def test_video_from_file_get_dimensions(simple_video_file):assert height == 4[email protected]("pixel_type,storage_dtype", [("half", np.float16), ("float", np.float32)])[email protected]("channels,pixel_format", [(1, "grayf32le"), (3, "gbrpf32le"), (4, "gbrapf32le")])[email protected]("width", [7, 32])[email protected]("crop", [None, (2, 2, 4, 4)])+def test_video_from_file_preserves_exr_float_pixels(tmp_path, pixel_type, storage_dtype, channels, pixel_format, width, crop):+ values = [-3.0, -0.125, 0.0, 2 ** -24, 0.12345679, 0.5, 1.0, 2.0, 12.0, 65504.0]+ pixels = np.resize(np.array(values), (19, width, channels))+ if channels == 4:+ pixels[..., 3] = np.linspace(0.0, 1.0, width)+ pixels = pixels.astype(storage_dtype).astype(np.float32)++ codec = av.CodecContext.create("exr", "w")+ codec.width = width+ codec.height = pixels.shape[0]+ codec.pix_fmt = pixel_format+ codec.time_base = Fraction(1, 1)+ codec.options = {"format": pixel_type, "compression": "zip16"}+ frame = av.VideoFrame.from_ndarray(pixels[..., 0] if channels == 1 else pixels, format=pixel_format)+ frame.pts = 0+ frame.time_base = codec.time_base+ encoded = b"".join(bytes(packet) for packet in list(codec.encode(frame)) + list(codec.encode(None)))+ path = tmp_path / "float.exr"+ path.write_bytes(encoded)++ components = VideoFromFile(str(path), crop=crop).get_components()+ if crop is not None:+ x, y, width, height = crop+ pixels = pixels[y:y + height, x:x + width]+ expected_rgb = np.repeat(pixels, 3, axis=-1) if channels == 1 else pixels[..., :3]+ assert components.images.dtype == torch.float32+ np.testing.assert_array_equal(components.images.numpy(), expected_rgb[None])+ if channels == 4:+ assert components.alpha.dtype == torch.float32+ np.testing.assert_array_equal(components.alpha.numpy(), pixels[None, ..., 3:])+ else:+ assert components.alpha is None++def test_video_color_space_defaults_to_srgb(simple_video_file, video_components):assert VideoFromFile(simple_video_file).get_color_space() == "sRGB"assert VideoFromComponents(video_components).get_color_space() == "sRGB"