Skip to content

Commit bca6af3

Browse files
committed
Cleanup video decoder build stuff
1 parent be7cdf1 commit bca6af3

File tree

2 files changed

+57
-65
lines changed

2 files changed

+57
-65
lines changed

setup.py

+56-64
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@
2121
USE_WEBP = os.getenv("TORCHVISION_USE_WEBP", "1") == "1"
2222
USE_NVJPEG = os.getenv("TORCHVISION_USE_NVJPEG", "1") == "1"
2323
NVCC_FLAGS = os.getenv("NVCC_FLAGS", None)
24-
USE_FFMPEG = os.getenv("TORCHVISION_USE_FFMPEG", "1") == "1"
25-
USE_VIDEO_CODEC = os.getenv("TORCHVISION_USE_VIDEO_CODEC", "1") == "1"
24+
# Note: the GPU video decoding stuff used to be called "video codec", which
25+
# isn't an accurate or descriptive name considering there are at least 2 other
26+
# video deocding backends in torchvision. I'm renaming this to "gpu video
27+
# decoder" where possible, keeping user facing names (like the env var below) to
28+
# the old scheme for BC.
29+
USE_GPU_VIDEO_DECODER = os.getenv("TORCHVISION_USE_VIDEO_CODEC", "1") == "1"
30+
# Same here: "use ffmpeg" was used to denote "use cpu video decoder".
31+
USE_CPU_VIDEO_DECODER = os.getenv("TORCHVISION_USE_FFMPEG", "1") == "1"
2632

2733
TORCHVISION_INCLUDE = os.environ.get("TORCHVISION_INCLUDE", "")
2834
TORCHVISION_LIBRARY = os.environ.get("TORCHVISION_LIBRARY", "")
@@ -45,8 +51,8 @@
4551
print(f"{USE_WEBP = }")
4652
print(f"{USE_NVJPEG = }")
4753
print(f"{NVCC_FLAGS = }")
48-
print(f"{USE_FFMPEG = }")
49-
print(f"{USE_VIDEO_CODEC = }")
54+
print(f"{USE_CPU_VIDEO_DECODER = }")
55+
print(f"{USE_GPU_VIDEO_DECODER = }")
5056
print(f"{TORCHVISION_INCLUDE = }")
5157
print(f"{TORCHVISION_LIBRARY = }")
5258
print(f"{IS_ROCM = }")
@@ -351,28 +357,21 @@ def make_image_extension():
351357
def make_video_decoders_extensions():
352358
print("Building video decoder extensions")
353359

354-
# Locating ffmpeg
355-
ffmpeg_exe = shutil.which("ffmpeg")
356-
has_ffmpeg = ffmpeg_exe is not None
357-
ffmpeg_version = None
358-
# FIXME: Building torchvision with ffmpeg on MacOS or with Python 3.9
359-
# FIXME: causes crash. See the following GitHub issues for more details.
360-
# FIXME: https://github.com/pytorch/pytorch/issues/65000
361-
# FIXME: https://github.com/pytorch/vision/issues/3367
360+
build_without_extensions_msg = "Building without video decoders extensions."
362361
if sys.platform != "linux" or (sys.version_info.major == 3 and sys.version_info.minor == 9):
363-
has_ffmpeg = False
364-
if has_ffmpeg:
365-
try:
366-
# This is to check if ffmpeg is installed properly.
367-
ffmpeg_version = subprocess.check_output(["ffmpeg", "-version"])
368-
except subprocess.CalledProcessError:
369-
print("Building torchvision without ffmpeg support")
370-
print(" Error fetching ffmpeg version, ignoring ffmpeg.")
371-
has_ffmpeg = False
362+
# FIXME: Building torchvision with ffmpeg on MacOS or with Python 3.9
363+
# FIXME: causes crash. See the following GitHub issues for more details.
364+
# FIXME: https://github.com/pytorch/pytorch/issues/65000
365+
# FIXME: https://github.com/pytorch/vision/issues/3367
366+
print("Can only build video decoder extensions on linux and Python != 3.9")
367+
return []
372368

373-
use_ffmpeg = USE_FFMPEG and has_ffmpeg
369+
ffmpeg_exe = shutil.which("ffmpeg")
370+
if ffmpeg_exe is None:
371+
print(f"{build_without_extensions_msg} Couldn't find ffmpeg binary.")
372+
return []
374373

375-
if use_ffmpeg:
374+
def find_ffmpeg_libraries():
376375
ffmpeg_libraries = {"libavcodec", "libavformat", "libavutil", "libswresample", "libswscale"}
377376

378377
ffmpeg_bin = os.path.dirname(ffmpeg_exe)
@@ -399,18 +398,23 @@ def make_video_decoders_extensions():
399398
library_found |= len(glob.glob(full_path)) > 0
400399

401400
if not library_found:
402-
print("Building torchvision without ffmpeg support")
403-
print(f" {library} header files were not found, disabling ffmpeg support")
404-
use_ffmpeg = False
405-
else:
406-
print("Building torchvision without ffmpeg support")
401+
print(f"{build_without_extensions_msg}")
402+
print(f"{library} header files were not found.")
403+
return None, None
404+
405+
return ffmpeg_include_dir, ffmpeg_library_dir
406+
407+
ffmpeg_include_dir, ffmpeg_library_dir = find_ffmpeg_libraries()
408+
if ffmpeg_include_dir is None or ffmpeg_library_dir is None:
409+
return []
410+
411+
print("Found ffmpeg:")
412+
print(f" ffmpeg include path: {ffmpeg_include_dir}")
413+
print(f" ffmpeg library_dir: {ffmpeg_library_dir}")
407414

408415
extensions = []
409-
if use_ffmpeg:
410-
print("Building torchvision with ffmpeg support")
411-
print(f" ffmpeg version: {ffmpeg_version}")
412-
print(f" ffmpeg include path: {ffmpeg_include_dir}")
413-
print(f" ffmpeg library_dir: {ffmpeg_library_dir}")
416+
if USE_CPU_VIDEO_DECODER:
417+
print("Building with CPU video decoder support")
414418

415419
# TorchVision base decoder + video reader
416420
video_reader_src_dir = os.path.join(ROOT_DIR, "torchvision", "csrc", "io", "video_reader")
@@ -427,6 +431,7 @@ def make_video_decoders_extensions():
427431

428432
extensions.append(
429433
CppExtension(
434+
# This is an aweful name. It should be "cpu_video_decoder". Keeping for BC.
430435
"torchvision.video_reader",
431436
combined_src,
432437
include_dirs=[
@@ -450,25 +455,24 @@ def make_video_decoders_extensions():
450455
)
451456
)
452457

453-
# Locating video codec
454-
# CUDA_HOME should be set to the cuda root directory.
455-
# TORCHVISION_INCLUDE and TORCHVISION_LIBRARY should include the location to
456-
# video codec header files and libraries respectively.
457-
video_codec_found = (
458-
BUILD_CUDA_SOURCES
459-
and CUDA_HOME is not None
460-
and any([os.path.exists(os.path.join(folder, "cuviddec.h")) for folder in TORCHVISION_INCLUDE])
461-
and any([os.path.exists(os.path.join(folder, "nvcuvid.h")) for folder in TORCHVISION_INCLUDE])
462-
and any([os.path.exists(os.path.join(folder, "libnvcuvid.so")) for folder in TORCHVISION_LIBRARY])
463-
)
458+
if USE_GPU_VIDEO_DECODER:
459+
# Locating GPU video decoder headers and libraries
460+
# CUDA_HOME should be set to the cuda root directory.
461+
# TORCHVISION_INCLUDE and TORCHVISION_LIBRARY should include the locations
462+
# to the headers and libraries below
463+
if not (
464+
BUILD_CUDA_SOURCES
465+
and CUDA_HOME is not None
466+
and any([os.path.exists(os.path.join(folder, "cuviddec.h")) for folder in TORCHVISION_INCLUDE])
467+
and any([os.path.exists(os.path.join(folder, "nvcuvid.h")) for folder in TORCHVISION_INCLUDE])
468+
and any([os.path.exists(os.path.join(folder, "libnvcuvid.so")) for folder in TORCHVISION_LIBRARY])
469+
and any([os.path.exists(os.path.join(folder, "libavcodec", "bsf.h")) for folder in ffmpeg_include_dir])
470+
):
471+
print("Could not find necessary dependencies. Refer the setup.py to check which ones are needed.")
472+
print("Building without GPU video decoder support")
473+
return extensions
474+
print("Building torchvision with GPU video decoder support")
464475

465-
use_video_codec = USE_VIDEO_CODEC and video_codec_found
466-
if (
467-
use_video_codec
468-
and use_ffmpeg
469-
and any([os.path.exists(os.path.join(folder, "libavcodec", "bsf.h")) for folder in ffmpeg_include_dir])
470-
):
471-
print("Building torchvision with video codec support")
472476
gpu_decoder_path = os.path.join(CSRS_DIR, "io", "decoder", "gpu")
473477
gpu_decoder_src = glob.glob(os.path.join(gpu_decoder_path, "*.cpp"))
474478
cuda_libs = os.path.join(CUDA_HOME, "lib64")
@@ -477,7 +481,7 @@ def make_video_decoders_extensions():
477481
_, extra_compile_args = get_macros_and_flags()
478482
extensions.append(
479483
CUDAExtension(
480-
"torchvision.Decoder",
484+
"torchvision.gpu_decoder",
481485
gpu_decoder_src,
482486
include_dirs=[CSRS_DIR] + TORCHVISION_INCLUDE + [gpu_decoder_path] + [cuda_inc] + ffmpeg_include_dir,
483487
library_dirs=ffmpeg_library_dir + TORCHVISION_LIBRARY + [cuda_libs],
@@ -498,18 +502,6 @@ def make_video_decoders_extensions():
498502
extra_compile_args=extra_compile_args,
499503
)
500504
)
501-
else:
502-
print("Building torchvision without video codec support")
503-
if (
504-
use_video_codec
505-
and use_ffmpeg
506-
and not any([os.path.exists(os.path.join(folder, "libavcodec", "bsf.h")) for folder in ffmpeg_include_dir])
507-
):
508-
print(
509-
" The installed version of ffmpeg is missing the header file 'bsf.h' which is "
510-
" required for GPU video decoding. Please install the latest ffmpeg from conda-forge channel:"
511-
" `conda install -c conda-forge ffmpeg`."
512-
)
513505

514506
return extensions
515507

torchvision/io/_load_gpu_decoder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
try:
5-
_load_library("Decoder")
5+
_load_library("gpu_decoder")
66
_HAS_GPU_VIDEO_DECODER = True
77
except (ImportError, OSError):
88
_HAS_GPU_VIDEO_DECODER = False

0 commit comments

Comments
 (0)