21
21
USE_WEBP = os .getenv ("TORCHVISION_USE_WEBP" , "1" ) == "1"
22
22
USE_NVJPEG = os .getenv ("TORCHVISION_USE_NVJPEG" , "1" ) == "1"
23
23
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"
26
32
27
33
TORCHVISION_INCLUDE = os .environ .get ("TORCHVISION_INCLUDE" , "" )
28
34
TORCHVISION_LIBRARY = os .environ .get ("TORCHVISION_LIBRARY" , "" )
45
51
print (f"{ USE_WEBP = } " )
46
52
print (f"{ USE_NVJPEG = } " )
47
53
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 = } " )
50
56
print (f"{ TORCHVISION_INCLUDE = } " )
51
57
print (f"{ TORCHVISION_LIBRARY = } " )
52
58
print (f"{ IS_ROCM = } " )
@@ -351,28 +357,21 @@ def make_image_extension():
351
357
def make_video_decoders_extensions ():
352
358
print ("Building video decoder extensions" )
353
359
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."
362
361
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 []
372
368
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 []
374
373
375
- if use_ffmpeg :
374
+ def find_ffmpeg_libraries () :
376
375
ffmpeg_libraries = {"libavcodec" , "libavformat" , "libavutil" , "libswresample" , "libswscale" }
377
376
378
377
ffmpeg_bin = os .path .dirname (ffmpeg_exe )
@@ -399,18 +398,23 @@ def make_video_decoders_extensions():
399
398
library_found |= len (glob .glob (full_path )) > 0
400
399
401
400
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 } " )
407
414
408
415
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" )
414
418
415
419
# TorchVision base decoder + video reader
416
420
video_reader_src_dir = os .path .join (ROOT_DIR , "torchvision" , "csrc" , "io" , "video_reader" )
@@ -427,6 +431,7 @@ def make_video_decoders_extensions():
427
431
428
432
extensions .append (
429
433
CppExtension (
434
+ # This is an aweful name. It should be "cpu_video_decoder". Keeping for BC.
430
435
"torchvision.video_reader" ,
431
436
combined_src ,
432
437
include_dirs = [
@@ -450,25 +455,24 @@ def make_video_decoders_extensions():
450
455
)
451
456
)
452
457
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" )
464
475
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" )
472
476
gpu_decoder_path = os .path .join (CSRS_DIR , "io" , "decoder" , "gpu" )
473
477
gpu_decoder_src = glob .glob (os .path .join (gpu_decoder_path , "*.cpp" ))
474
478
cuda_libs = os .path .join (CUDA_HOME , "lib64" )
@@ -477,7 +481,7 @@ def make_video_decoders_extensions():
477
481
_ , extra_compile_args = get_macros_and_flags ()
478
482
extensions .append (
479
483
CUDAExtension (
480
- "torchvision.Decoder " ,
484
+ "torchvision.gpu_decoder " ,
481
485
gpu_decoder_src ,
482
486
include_dirs = [CSRS_DIR ] + TORCHVISION_INCLUDE + [gpu_decoder_path ] + [cuda_inc ] + ffmpeg_include_dir ,
483
487
library_dirs = ffmpeg_library_dir + TORCHVISION_LIBRARY + [cuda_libs ],
@@ -498,18 +502,6 @@ def make_video_decoders_extensions():
498
502
extra_compile_args = extra_compile_args ,
499
503
)
500
504
)
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
- )
513
505
514
506
return extensions
515
507
0 commit comments