Skip to content

Commit 1e19551

Browse files
author
Dmitry Rogozhkin
committed
Share ffmpeg version setups and other fixes
Signed-off-by: Dmitry Rogozhkin <dmitry.v.rogozhkin@gmail.com> tmp Signed-off-by: Dmitry Rogozhkin <dmitry.v.rogozhkin@gmail.com>
1 parent b1c077f commit 1e19551

File tree

7 files changed

+325
-397
lines changed

7 files changed

+325
-397
lines changed

src/torchcodec/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
import os.path as _osp
7+
from pathlib import Path
88

99
# Note: usort wants to put Frame and FrameBatch after decoders and samplers,
1010
# but that results in circular import.
11-
from ._core import core_library_path, variant
11+
from ._core import core_library_path, ffmpeg_major_version
1212
from ._frame import AudioSamples, Frame, FrameBatch # usort:skip # noqa
1313
from . import decoders, samplers # noqa
1414

@@ -18,4 +18,9 @@
1818
except Exception:
1919
pass
2020

21-
cmake_prefix_path = _osp.join(_osp.dirname(__file__), "share", "cmake")
21+
# `torchcodec.cmake_prefix_path` is a Python-based way to programmatically
22+
# obtain the correct CMAKE_PREFIX_PATH value for the TorchCodec installation.
23+
# It can be used in a build system of a C++ application to ensure that CMake
24+
# can successfully find TorchCodec C++ libraries. This variable is exposed
25+
# as TorchCodec API.
26+
cmake_prefix_path = Path(__file__).parent / "share" / "cmake"

src/torchcodec/_core/CMakeLists.txt

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,16 @@ if(DEFINED ENV{BUILD_AGAINST_ALL_FFMPEG_FROM_S3})
245245
you still need a different FFmpeg to be installed for run time!"
246246
)
247247

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

253-
make_torchcodec_libraries(8 ffmpeg8)
254-
make_torchcodec_libraries(7 ffmpeg7)
255-
make_torchcodec_libraries(6 ffmpeg6)
256-
make_torchcodec_libraries(4 ffmpeg4)
257-
make_torchcodec_libraries(5 ffmpeg5)
253+
make_torchcodec_libraries(8 torchcodec::ffmpeg8)
254+
make_torchcodec_libraries(7 torchcodec::ffmpeg7)
255+
make_torchcodec_libraries(6 torchcodec::ffmpeg6)
256+
make_torchcodec_libraries(4 torchcodec::ffmpeg4)
257+
make_torchcodec_libraries(5 torchcodec::ffmpeg5)
258258
else()
259259
message(
260260
STATUS
@@ -263,40 +263,12 @@ else()
263263
installed FFmpeg from conda, make sure pkg-config is installed from
264264
conda as well."
265265
)
266-
find_package(PkgConfig REQUIRED)
267-
pkg_check_modules(LIBAV REQUIRED IMPORTED_TARGET
268-
libavdevice
269-
libavfilter
270-
libavformat
271-
libavcodec
272-
libavutil
273-
libswresample
274-
libswscale
275-
)
276266

277-
# Split libavcodec's version string by '.' and convert it to a list
278-
string(REPLACE "." ";" libavcodec_version_list ${LIBAV_libavcodec_VERSION})
279-
# Get the first element of the list, which is the major version
280-
list(GET libavcodec_version_list 0 libavcodec_major_version)
281-
282-
if (${libavcodec_major_version} STREQUAL "58")
283-
set(ffmpeg_major_version "4")
284-
elseif (${libavcodec_major_version} STREQUAL "59")
285-
set(ffmpeg_major_version "5")
286-
elseif (${libavcodec_major_version} STREQUAL "60")
287-
set(ffmpeg_major_version "6")
288-
elseif (${libavcodec_major_version} STREQUAL "61")
289-
set(ffmpeg_major_version "7")
290-
elseif (${libavcodec_major_version} STREQUAL "62")
291-
set(ffmpeg_major_version "8")
292-
else()
293-
message(
294-
FATAL_ERROR
295-
"Unsupported libavcodec version: ${libavcodec_major_version}"
296-
)
297-
endif()
267+
# This will expose `add_ffmpeg_target_with_pkg_config`
268+
include("${CMAKE_CURRENT_SOURCE_DIR}/../share/cmake/TorchCodec/ffmpeg_versions.cmake")
298269

299-
make_torchcodec_libraries(${ffmpeg_major_version} PkgConfig::LIBAV)
270+
add_ffmpeg_target_with_pkg_config(ffmpeg_major_version)
271+
make_torchcodec_libraries(${ffmpeg_major_version} torchcodec::ffmpeg${ffmpeg_major_version})
300272

301273
# Expose these values updwards so that the test compilation does not need
302274
# to re-figure it out. FIXME: it's not great that we just copy-paste the

src/torchcodec/_core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
encode_video_to_file,
3131
encode_video_to_file_like,
3232
encode_video_to_tensor,
33+
ffmpeg_major_version,
3334
get_ffmpeg_library_versions,
3435
get_frame_at_index,
3536
get_frame_at_pts,
@@ -42,5 +43,4 @@
4243
get_next_frame,
4344
scan_all_streams_to_update_metadata,
4445
seek_to_pts,
45-
variant,
4646
)

src/torchcodec/_core/fetch_and_expose_non_gpl_ffmpeg_libs.cmake

Lines changed: 6 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ set(
2121
)
2222

2323
if (LINUX)
24-
set(lib_dir "lib")
25-
2624
set(
2725
platform_url
2826
${base_url}/linux_x86_64
@@ -48,58 +46,7 @@ if (LINUX)
4846
f8_sha256
4947
c55b3c1a4b5e4d5fdd7c632bea3ab6f45b4e37cc8e0999dda3f84a8ed8defad8
5048
)
51-
set(
52-
f4_library_file_names
53-
libavutil.so.56
54-
libavcodec.so.58
55-
libavformat.so.58
56-
libavdevice.so.58
57-
libavfilter.so.7
58-
libswscale.so.5
59-
libswresample.so.3
60-
)
61-
set(
62-
f5_library_file_names
63-
libavutil.so.57
64-
libavcodec.so.59
65-
libavformat.so.59
66-
libavdevice.so.59
67-
libavfilter.so.8
68-
libswscale.so.6
69-
libswresample.so.4
70-
)
71-
set(
72-
f6_library_file_names
73-
libavutil.so.58
74-
libavcodec.so.60
75-
libavformat.so.60
76-
libavdevice.so.60
77-
libavfilter.so.9
78-
libswscale.so.7
79-
libswresample.so.4
80-
)
81-
set(
82-
f7_library_file_names
83-
libavutil.so.59
84-
libavcodec.so.61
85-
libavformat.so.61
86-
libavdevice.so.61
87-
libavfilter.so.10
88-
libswscale.so.8
89-
libswresample.so.5
90-
)
91-
set(
92-
f8_library_file_names
93-
libavutil.so.60
94-
libavcodec.so.62
95-
libavformat.so.62
96-
libavdevice.so.62
97-
libavfilter.so.11
98-
libswscale.so.9
99-
libswresample.so.6
100-
)
10149
elseif (APPLE)
102-
set(lib_dir "lib")
10350
set(
10451
platform_url
10552
${base_url}/macos_arm64
@@ -124,60 +71,7 @@ elseif (APPLE)
12471
f8_sha256
12572
beb936b76f25d2621228a12cdb67c9ae3d1eff7aa713ef8d1167ebf0c25bd5ec
12673
)
127-
128-
set(
129-
f4_library_file_names
130-
libavutil.56.dylib
131-
libavcodec.58.dylib
132-
libavformat.58.dylib
133-
libavdevice.58.dylib
134-
libavfilter.7.dylib
135-
libswscale.5.dylib
136-
libswresample.3.dylib
137-
)
138-
set(
139-
f5_library_file_names
140-
libavutil.57.dylib
141-
libavcodec.59.dylib
142-
libavformat.59.dylib
143-
libavdevice.59.dylib
144-
libavfilter.8.dylib
145-
libswscale.6.dylib
146-
libswresample.4.dylib
147-
)
148-
set(
149-
f6_library_file_names
150-
libavutil.58.dylib
151-
libavcodec.60.dylib
152-
libavformat.60.dylib
153-
libavdevice.60.dylib
154-
libavfilter.9.dylib
155-
libswscale.7.dylib
156-
libswresample.4.dylib
157-
)
158-
set(
159-
f7_library_file_names
160-
libavutil.59.dylib
161-
libavcodec.61.dylib
162-
libavformat.61.dylib
163-
libavdevice.61.dylib
164-
libavfilter.10.dylib
165-
libswscale.8.dylib
166-
libswresample.5.dylib
167-
)
168-
set(
169-
f8_library_file_names
170-
libavutil.60.dylib
171-
libavcodec.62.dylib
172-
libavformat.62.dylib
173-
libavdevice.62.dylib
174-
libavfilter.11.dylib
175-
libswscale.9.dylib
176-
libswresample.6.dylib
177-
)
178-
17974
elseif (WIN32)
180-
set(lib_dir "bin")
18175
set(
18276
platform_url
18377
${base_url}/windows_x86_64
@@ -202,57 +96,6 @@ elseif (WIN32)
20296
f8_sha256
20397
bac845ac79876b104959cb0e7b9dec772a261116344dd17d2f97e7ddfac4a73f
20498
)
205-
206-
set(
207-
f4_library_file_names
208-
avutil.lib
209-
avcodec.lib
210-
avformat.lib
211-
avdevice.lib
212-
avfilter.lib
213-
swscale.lib
214-
swresample.lib
215-
)
216-
set(
217-
f5_library_file_names
218-
avutil.lib
219-
avcodec.lib
220-
avformat.lib
221-
avdevice.lib
222-
avfilter.lib
223-
swscale.lib
224-
swresample.lib
225-
)
226-
set(
227-
f6_library_file_names
228-
avutil.lib
229-
avcodec.lib
230-
avformat.lib
231-
avdevice.lib
232-
avfilter.lib
233-
swscale.lib
234-
swresample.lib
235-
)
236-
set(
237-
f7_library_file_names
238-
avutil.lib
239-
avcodec.lib
240-
avformat.lib
241-
avdevice.lib
242-
avfilter.lib
243-
swscale.lib
244-
swresample.lib
245-
)
246-
set(
247-
f8_library_file_names
248-
avutil.lib
249-
avcodec.lib
250-
avformat.lib
251-
avdevice.lib
252-
avfilter.lib
253-
swscale.lib
254-
swresample.lib
255-
)
25699
else()
257100
message(
258101
FATAL_ERROR
@@ -293,68 +136,11 @@ FetchContent_Declare(
293136

294137
FetchContent_MakeAvailable(f4 f5 f6 f7 f8)
295138

296-
add_library(ffmpeg4 INTERFACE)
297-
add_library(ffmpeg5 INTERFACE)
298-
add_library(ffmpeg6 INTERFACE)
299-
add_library(ffmpeg7 INTERFACE)
300-
add_library(ffmpeg8 INTERFACE)
139+
include("${CMAKE_CURRENT_SOURCE_DIR}/../share/cmake/TorchCodec/ffmpeg_versions.cmake")
301140

302141
# Note: the f?_SOURCE_DIR variables were set by FetchContent_MakeAvailable
303-
target_include_directories(ffmpeg4 INTERFACE ${f4_SOURCE_DIR}/include)
304-
target_include_directories(ffmpeg5 INTERFACE ${f5_SOURCE_DIR}/include)
305-
target_include_directories(ffmpeg6 INTERFACE ${f6_SOURCE_DIR}/include)
306-
target_include_directories(ffmpeg7 INTERFACE ${f7_SOURCE_DIR}/include)
307-
target_include_directories(ffmpeg8 INTERFACE ${f8_SOURCE_DIR}/include)
308-
309-
310-
list(
311-
TRANSFORM f4_library_file_names
312-
PREPEND ${f4_SOURCE_DIR}/${lib_dir}/
313-
OUTPUT_VARIABLE f4_library_paths
314-
)
315-
list(
316-
TRANSFORM f5_library_file_names
317-
PREPEND ${f5_SOURCE_DIR}/${lib_dir}/
318-
OUTPUT_VARIABLE f5_library_paths
319-
)
320-
list(
321-
TRANSFORM f6_library_file_names
322-
PREPEND ${f6_SOURCE_DIR}/${lib_dir}/
323-
OUTPUT_VARIABLE f6_library_paths
324-
)
325-
list(
326-
TRANSFORM f7_library_file_names
327-
PREPEND ${f7_SOURCE_DIR}/${lib_dir}/
328-
OUTPUT_VARIABLE f7_library_paths
329-
)
330-
list(
331-
TRANSFORM f8_library_file_names
332-
PREPEND ${f8_SOURCE_DIR}/${lib_dir}/
333-
OUTPUT_VARIABLE f8_library_paths
334-
)
335-
336-
target_link_libraries(
337-
ffmpeg4
338-
INTERFACE
339-
${f4_library_paths}
340-
)
341-
target_link_libraries(
342-
ffmpeg5
343-
INTERFACE
344-
${f5_library_paths}
345-
)
346-
target_link_libraries(
347-
ffmpeg6
348-
INTERFACE
349-
${f6_library_paths}
350-
)
351-
target_link_libraries(
352-
ffmpeg7
353-
INTERFACE
354-
${f7_library_paths}
355-
)
356-
target_link_libraries(
357-
ffmpeg8
358-
INTERFACE
359-
${f8_library_paths}
360-
)
142+
add_ffmpeg_target(4 "${f4_SOURCE_DIR}")
143+
add_ffmpeg_target(5 "${f5_SOURCE_DIR}")
144+
add_ffmpeg_target(6 "${f6_SOURCE_DIR}")
145+
add_ffmpeg_target(7 "${f7_SOURCE_DIR}")
146+
add_ffmpeg_target(8 "${f8_SOURCE_DIR}")

0 commit comments

Comments
 (0)