Skip to content

Commit 8bbebcb

Browse files
committed
Migrate avif and heic decoders to torchvision-extra-decoders repo
1 parent fc74637 commit 8bbebcb

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

torchvision/csrc/io/image/cpu/decode_image.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ torch::Tensor decode_image(
5050
return decode_gif(data);
5151
}
5252

53+
// TODO: We have to remove the "avif" detection logic from here, since
54+
// `decode_avif` doesn't exist in torchvision anymore. We can reimplement the
55+
// signature detection logic in Python instead. Same for heic. Longer term, we
56+
// can move the logic to the torchvision-extra-decoders repo and rely on
57+
// libmagic for a more accurate signature check.
58+
5359
// We assume the signature of an avif file is
5460
// 0000 0020 6674 7970 6176 6966
5561
// xxxx xxxx f t y p a v i f

torchvision/io/image.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ def decode_webp(
377377
return torch.ops.image.decode_webp(input, mode.value)
378378

379379

380+
# TODO: remove this, and the associated avif decoder
380381
def _decode_avif(
381382
input: torch.Tensor,
382383
mode: ImageReadMode = ImageReadMode.UNCHANGED,
@@ -408,6 +409,7 @@ def _decode_avif(
408409
return torch.ops.image.decode_avif(input, mode.value)
409410

410411

412+
# TODO: remove this, and the associated heic decoder
411413
def _decode_heic(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
412414
"""
413415
Decode an HEIC image into a 3 dimensional RGB[A] Tensor.
@@ -434,3 +436,32 @@ def _decode_heic(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHAN
434436
if isinstance(mode, str):
435437
mode = ImageReadMode[mode.upper()]
436438
return torch.ops.image.decode_heic(input, mode.value)
439+
440+
441+
_EXTRA_DECODERS_ALREADY_LOADED = False
442+
443+
444+
def _load_extra_decoders_once():
445+
global _EXTRA_DECODERS_ALREADY_LOADED
446+
if _EXTRA_DECODERS_ALREADY_LOADED:
447+
return
448+
449+
try:
450+
import torchvision_extra_decoders
451+
except ImportError as e:
452+
raise RuntimeError("You need to pip install torchvision-extra-decoders blah blah blah") from e
453+
454+
# This will expose torch.ops.extra_decoders_ns.decode_avif and torch.ops.extra_decoders_ns.decode_heic
455+
torchvision_extra_decoders.expose_extra_decoders()
456+
457+
_EXTRA_DECODERS_ALREADY_LOADED = True
458+
459+
460+
def decode_avif(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
461+
_load_extra_decoders_once()
462+
return torch.ops.extra_decoders_ns.decode_avif(input, mode.value)
463+
464+
465+
def decode_heic(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
466+
_load_extra_decoders_once()
467+
return torch.ops.extra_decoders_ns.decode_heic(input, mode.value)

0 commit comments

Comments
 (0)