• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1if(CMAKE_SYSTEM_NAME MATCHES "Linux")
2    set(FFMPEG_FLAGS
3        --disable-programs
4        --disable-doc
5        --disable-postproc
6        --disable-decoder=av1
7        --disable-libxcb
8        --disable-hwaccels
9        --disable-static
10        --enable-shared
11        --disable-x86asm
12        --extra-cflags="-D_FORTIFY_SOURCE=2 -fstack-protector-all"
13        --extra-ldflags="-Wl,-z,relro,-z,now")
14else()
15    set(FFMPEG_FLAGS
16        --disable-programs
17        --disable-doc
18        --disable-postproc
19        --disable-decoder=av1
20        --disable-libxcb
21        --disable-hwaccels
22        --disable-static
23        --enable-shared
24        --disable-x86asm)
25endif()
26
27set(REQ_URL "https://ffmpeg.org/releases/ffmpeg-5.1.2.tar.gz")
28set(SHA256 "87fe8defa37ce5f7449e36047171fed5e4c3f4bb73eaccea8c954ee81393581c")
29
30if(CMAKE_SYSTEM_NAME MATCHES "Windows")
31    set(FFMPEG_DLL_DIR $ENV{FFMPEG_CACHE_DIR})
32    add_library(mindspore::avcodec SHARED IMPORTED GLOBAL)
33    add_library(mindspore::avdevice SHARED IMPORTED GLOBAL)
34    add_library(mindspore::avfilter SHARED IMPORTED GLOBAL)
35    add_library(mindspore::avformat SHARED IMPORTED GLOBAL)
36    add_library(mindspore::avutil SHARED IMPORTED GLOBAL)
37    add_library(mindspore::swresample SHARED IMPORTED GLOBAL)
38    add_library(mindspore::swscale SHARED IMPORTED GLOBAL)
39    set_target_properties(mindspore::avcodec PROPERTIES IMPORTED_IMPLIB_RELEASE ${FFMPEG_DLL_DIR}/bin/avcodec.lib)
40    set_target_properties(mindspore::avdevice PROPERTIES IMPORTED_IMPLIB_RELEASE ${FFMPEG_DLL_DIR}/bin/avdevice.lib)
41    set_target_properties(mindspore::avfilter PROPERTIES IMPORTED_IMPLIB_RELEASE ${FFMPEG_DLL_DIR}/bin/avfilter.lib)
42    set_target_properties(mindspore::avformat PROPERTIES IMPORTED_IMPLIB_RELEASE ${FFMPEG_DLL_DIR}/bin/avformat.lib)
43    set_target_properties(mindspore::avutil PROPERTIES IMPORTED_IMPLIB_RELEASE ${FFMPEG_DLL_DIR}/bin/avutil.lib)
44    set_target_properties(mindspore::swresample PROPERTIES IMPORTED_IMPLIB_RELEASE ${FFMPEG_DLL_DIR}/bin/swresample.lib)
45    set_target_properties(mindspore::swscale PROPERTIES IMPORTED_IMPLIB_RELEASE ${FFMPEG_DLL_DIR}/bin/swscale.lib)
46    include_directories(${FFMPEG_DLL_DIR}/include)
47else()
48    mindspore_add_pkg(ffmpeg
49            VER 5.1.2
50            LIBS avcodec avdevice avfilter avformat avutil swresample swscale
51            URL ${REQ_URL}
52            SHA256 ${SHA256}
53            PATCHES ${TOP_DIR}/third_party/patch/ffmpeg/CVE-2022-3964.patch
54            PATCHES ${TOP_DIR}/third_party/patch/ffmpeg/CVE-2022-3965.patch
55            PATCHES ${TOP_DIR}/third_party/patch/ffmpeg/CVE-2023-47342.patch
56            CONFIGURE_COMMAND ./configure ${FFMPEG_FLAGS}
57            )
58
59    include_directories(${ffmpeg_INC})
60
61    if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
62        add_custom_target(
63                link_ffmpeg ALL
64                COMMAND echo "modify ffmpeg install name"
65                COMMENT "modify ffmpeg install name"
66        )
67
68        # rename self
69        function(change_ffmpeg_dylib_name lib_name lib)
70            add_custom_command(
71                TARGET link_ffmpeg
72                POST_BUILD
73                COMMAND install_name_tool -id @rpath/lib${lib_name}.dylib $<TARGET_FILE:ffmpeg::${lib}>
74                VERBATIM
75            )
76        endfunction()
77
78        change_ffmpeg_dylib_name("avutil.57" "avutil")
79        change_ffmpeg_dylib_name("swresample.4" "swresample")
80        change_ffmpeg_dylib_name("swscale.6" "swscale")
81        change_ffmpeg_dylib_name("avformat.59" "avformat")
82        change_ffmpeg_dylib_name("avfilter.8" "avfilter")
83        change_ffmpeg_dylib_name("avdevice.59" "avdevice")
84        change_ffmpeg_dylib_name("avcodec.59" "avcodec")
85
86        # depend
87        set(FFMPEG_PATH ${ffmpeg_LIBPATH})
88
89        function(change_ffmpeg_dylib_depend target_dylib link_dylibs)
90            foreach(dylib ${link_dylibs})
91              set(SRC_NAME ${FFMPEG_PATH}/lib${dylib}.dylib)
92              set(DST_NAME @rpath/lib${dylib}.dylib)
93              add_custom_command(
94                TARGET link_ffmpeg
95                POST_BUILD
96                COMMAND install_name_tool -change ${SRC_NAME} ${DST_NAME} $<TARGET_FILE:ffmpeg::${target_dylib}>
97                VERBATIM
98              )
99            endforeach()
100        endfunction()
101
102        change_ffmpeg_dylib_depend("swresample" "avutil.57")
103        change_ffmpeg_dylib_depend("swscale" "avutil.57")
104        change_ffmpeg_dylib_depend("avformat" "avutil.57;swresample.4;avcodec.59")
105        change_ffmpeg_dylib_depend("avfilter" "avutil.57;swresample.4;avcodec.59;avformat.59;swscale.6")
106        change_ffmpeg_dylib_depend("avdevice" "avutil.57;swresample.4;avcodec.59;avformat.59;swscale.6;avfilter.8")
107        change_ffmpeg_dylib_depend("avcodec" "avutil.57;swresample.4")
108    endif()
109
110    add_library(mindspore::avcodec ALIAS ffmpeg::avcodec)
111    add_library(mindspore::avdevice ALIAS ffmpeg::avdevice)
112    add_library(mindspore::avfilter ALIAS ffmpeg::avfilter)
113    add_library(mindspore::avformat ALIAS ffmpeg::avformat)
114    add_library(mindspore::avutil ALIAS ffmpeg::avutil)
115    add_library(mindspore::swresample ALIAS ffmpeg::swresample)
116    add_library(mindspore::swscale ALIAS ffmpeg::swscale)
117endif()
118