Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
cmake_minimum_required(VERSION 3.18)
project(TorchCodec)

# Define LINUX platform variable globally
if (UNIX AND NOT APPLE)
set(LINUX TRUE)
else()
set(LINUX FALSE)
endif()

add_subdirectory(src/torchcodec/_core)


Expand Down
11 changes: 11 additions & 0 deletions src/torchcodec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from pathlib import Path

# Note: usort wants to put Frame and FrameBatch after decoders and samplers,
# but that results in circular import.
from ._frame import AudioSamples, Frame, FrameBatch # usort:skip # noqa
Expand All @@ -14,3 +16,12 @@
from .version import __version__ # noqa: F401
except Exception:
pass

# cmake_prefix_path is needed for downstream cmake-based builds that use
# torchcodec as a dependency to tell cmake where torchcodec is installed and where to find its
# CMake configuration files.
# Pytorch itself has a similar mechanism which we use in our setup.py!
cmake_prefix_path = Path(__file__).parent / "share" / "cmake"
# Similarly, these are exposed for downstream builds that use torchcodec as a
# dependency.
from ._core import core_library_path, ffmpeg_major_version # usort:skip
48 changes: 10 additions & 38 deletions src/torchcodec/_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,16 @@ if(DEFINED ENV{BUILD_AGAINST_ALL_FFMPEG_FROM_S3})
you still need a different FFmpeg to be installed for run time!"
)

# This will expose the ffmpeg4, ffmpeg5, ffmpeg6, ffmpeg7, and ffmpeg8 targets
# This will expose the torchcodec::ffmpeg{N} (N=4,5,6,7,8) targets
include(
${CMAKE_CURRENT_SOURCE_DIR}/fetch_and_expose_non_gpl_ffmpeg_libs.cmake
)

make_torchcodec_libraries(8 ffmpeg8)
make_torchcodec_libraries(7 ffmpeg7)
make_torchcodec_libraries(6 ffmpeg6)
make_torchcodec_libraries(4 ffmpeg4)
make_torchcodec_libraries(5 ffmpeg5)
make_torchcodec_libraries(8 torchcodec::ffmpeg8)
make_torchcodec_libraries(7 torchcodec::ffmpeg7)
make_torchcodec_libraries(6 torchcodec::ffmpeg6)
make_torchcodec_libraries(4 torchcodec::ffmpeg4)
make_torchcodec_libraries(5 torchcodec::ffmpeg5)
else()
message(
STATUS
Expand All @@ -263,40 +263,12 @@ else()
installed FFmpeg from conda, make sure pkg-config is installed from
conda as well."
)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBAV REQUIRED IMPORTED_TARGET
libavdevice
libavfilter
libavformat
libavcodec
libavutil
libswresample
libswscale
)

# Split libavcodec's version string by '.' and convert it to a list
string(REPLACE "." ";" libavcodec_version_list ${LIBAV_libavcodec_VERSION})
# Get the first element of the list, which is the major version
list(GET libavcodec_version_list 0 libavcodec_major_version)

if (${libavcodec_major_version} STREQUAL "58")
set(ffmpeg_major_version "4")
elseif (${libavcodec_major_version} STREQUAL "59")
set(ffmpeg_major_version "5")
elseif (${libavcodec_major_version} STREQUAL "60")
set(ffmpeg_major_version "6")
elseif (${libavcodec_major_version} STREQUAL "61")
set(ffmpeg_major_version "7")
elseif (${libavcodec_major_version} STREQUAL "62")
set(ffmpeg_major_version "8")
else()
message(
FATAL_ERROR
"Unsupported libavcodec version: ${libavcodec_major_version}"
)
endif()
# This will expose `add_ffmpeg_target_with_pkg_config`
include("${CMAKE_CURRENT_SOURCE_DIR}/../share/cmake/TorchCodec/ffmpeg_versions.cmake")

make_torchcodec_libraries(${ffmpeg_major_version} PkgConfig::LIBAV)
add_ffmpeg_target_with_pkg_config(ffmpeg_major_version)
make_torchcodec_libraries(${ffmpeg_major_version} torchcodec::ffmpeg${ffmpeg_major_version})

# Expose these values updwards so that the test compilation does not need
# to re-figure it out. FIXME: it's not great that we just copy-paste the
Expand Down
2 changes: 2 additions & 0 deletions src/torchcodec/_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
_test_frame_pts_equality,
add_audio_stream,
add_video_stream,
core_library_path,
create_from_bytes,
create_from_file,
create_from_file_like,
Expand All @@ -29,6 +30,7 @@
encode_video_to_file,
encode_video_to_file_like,
encode_video_to_tensor,
ffmpeg_major_version,
get_ffmpeg_library_versions,
get_frame_at_index,
get_frame_at_pts,
Expand Down
233 changes: 7 additions & 226 deletions src/torchcodec/_core/fetch_and_expose_non_gpl_ffmpeg_libs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@ endif()

include(FetchContent)

if (UNIX AND NOT APPLE)
set(LINUX TRUE)
else()
set(LINUX FALSE)
endif()

set(
base_url
https://pytorch.s3.amazonaws.com/torchcodec/ffmpeg/2025-03-14
)

if (LINUX)
set(lib_dir "lib")

set(
platform_url
${base_url}/linux_x86_64
Expand All @@ -48,58 +40,7 @@ if (LINUX)
f8_sha256
c55b3c1a4b5e4d5fdd7c632bea3ab6f45b4e37cc8e0999dda3f84a8ed8defad8
)
set(
f4_library_file_names
libavutil.so.56
libavcodec.so.58
libavformat.so.58
libavdevice.so.58
libavfilter.so.7
libswscale.so.5
libswresample.so.3
)
set(
f5_library_file_names
libavutil.so.57
libavcodec.so.59
libavformat.so.59
libavdevice.so.59
libavfilter.so.8
libswscale.so.6
libswresample.so.4
)
set(
f6_library_file_names
libavutil.so.58
libavcodec.so.60
libavformat.so.60
libavdevice.so.60
libavfilter.so.9
libswscale.so.7
libswresample.so.4
)
set(
f7_library_file_names
libavutil.so.59
libavcodec.so.61
libavformat.so.61
libavdevice.so.61
libavfilter.so.10
libswscale.so.8
libswresample.so.5
)
set(
f8_library_file_names
libavutil.so.60
libavcodec.so.62
libavformat.so.62
libavdevice.so.62
libavfilter.so.11
libswscale.so.9
libswresample.so.6
)
elseif (APPLE)
set(lib_dir "lib")
set(
platform_url
${base_url}/macos_arm64
Expand All @@ -124,60 +65,7 @@ elseif (APPLE)
f8_sha256
beb936b76f25d2621228a12cdb67c9ae3d1eff7aa713ef8d1167ebf0c25bd5ec
)

set(
f4_library_file_names
libavutil.56.dylib
libavcodec.58.dylib
libavformat.58.dylib
libavdevice.58.dylib
libavfilter.7.dylib
libswscale.5.dylib
libswresample.3.dylib
)
set(
f5_library_file_names
libavutil.57.dylib
libavcodec.59.dylib
libavformat.59.dylib
libavdevice.59.dylib
libavfilter.8.dylib
libswscale.6.dylib
libswresample.4.dylib
)
set(
f6_library_file_names
libavutil.58.dylib
libavcodec.60.dylib
libavformat.60.dylib
libavdevice.60.dylib
libavfilter.9.dylib
libswscale.7.dylib
libswresample.4.dylib
)
set(
f7_library_file_names
libavutil.59.dylib
libavcodec.61.dylib
libavformat.61.dylib
libavdevice.61.dylib
libavfilter.10.dylib
libswscale.8.dylib
libswresample.5.dylib
)
set(
f8_library_file_names
libavutil.60.dylib
libavcodec.62.dylib
libavformat.62.dylib
libavdevice.62.dylib
libavfilter.11.dylib
libswscale.9.dylib
libswresample.6.dylib
)

elseif (WIN32)
set(lib_dir "bin")
set(
platform_url
${base_url}/windows_x86_64
Expand All @@ -202,57 +90,6 @@ elseif (WIN32)
f8_sha256
bac845ac79876b104959cb0e7b9dec772a261116344dd17d2f97e7ddfac4a73f
)

set(
f4_library_file_names
avutil.lib
avcodec.lib
avformat.lib
avdevice.lib
avfilter.lib
swscale.lib
swresample.lib
)
set(
f5_library_file_names
avutil.lib
avcodec.lib
avformat.lib
avdevice.lib
avfilter.lib
swscale.lib
swresample.lib
)
set(
f6_library_file_names
avutil.lib
avcodec.lib
avformat.lib
avdevice.lib
avfilter.lib
swscale.lib
swresample.lib
)
set(
f7_library_file_names
avutil.lib
avcodec.lib
avformat.lib
avdevice.lib
avfilter.lib
swscale.lib
swresample.lib
)
set(
f8_library_file_names
avutil.lib
avcodec.lib
avformat.lib
avdevice.lib
avfilter.lib
swscale.lib
swresample.lib
)
else()
message(
FATAL_ERROR
Expand Down Expand Up @@ -293,68 +130,12 @@ FetchContent_Declare(

FetchContent_MakeAvailable(f4 f5 f6 f7 f8)

add_library(ffmpeg4 INTERFACE)
add_library(ffmpeg5 INTERFACE)
add_library(ffmpeg6 INTERFACE)
add_library(ffmpeg7 INTERFACE)
add_library(ffmpeg8 INTERFACE)
# makes add_ffmpeg_target available
include("${CMAKE_CURRENT_SOURCE_DIR}/../share/cmake/TorchCodec/ffmpeg_versions.cmake")

# Note: the f?_SOURCE_DIR variables were set by FetchContent_MakeAvailable
target_include_directories(ffmpeg4 INTERFACE ${f4_SOURCE_DIR}/include)
target_include_directories(ffmpeg5 INTERFACE ${f5_SOURCE_DIR}/include)
target_include_directories(ffmpeg6 INTERFACE ${f6_SOURCE_DIR}/include)
target_include_directories(ffmpeg7 INTERFACE ${f7_SOURCE_DIR}/include)
target_include_directories(ffmpeg8 INTERFACE ${f8_SOURCE_DIR}/include)


list(
TRANSFORM f4_library_file_names
PREPEND ${f4_SOURCE_DIR}/${lib_dir}/
OUTPUT_VARIABLE f4_library_paths
)
list(
TRANSFORM f5_library_file_names
PREPEND ${f5_SOURCE_DIR}/${lib_dir}/
OUTPUT_VARIABLE f5_library_paths
)
list(
TRANSFORM f6_library_file_names
PREPEND ${f6_SOURCE_DIR}/${lib_dir}/
OUTPUT_VARIABLE f6_library_paths
)
list(
TRANSFORM f7_library_file_names
PREPEND ${f7_SOURCE_DIR}/${lib_dir}/
OUTPUT_VARIABLE f7_library_paths
)
list(
TRANSFORM f8_library_file_names
PREPEND ${f8_SOURCE_DIR}/${lib_dir}/
OUTPUT_VARIABLE f8_library_paths
)

target_link_libraries(
ffmpeg4
INTERFACE
${f4_library_paths}
)
target_link_libraries(
ffmpeg5
INTERFACE
${f5_library_paths}
)
target_link_libraries(
ffmpeg6
INTERFACE
${f6_library_paths}
)
target_link_libraries(
ffmpeg7
INTERFACE
${f7_library_paths}
)
target_link_libraries(
ffmpeg8
INTERFACE
${f8_library_paths}
)
add_ffmpeg_target(4 "${f4_SOURCE_DIR}")
add_ffmpeg_target(5 "${f5_SOURCE_DIR}")
add_ffmpeg_target(6 "${f6_SOURCE_DIR}")
add_ffmpeg_target(7 "${f7_SOURCE_DIR}")
add_ffmpeg_target(8 "${f8_SOURCE_DIR}")
Loading
Loading