-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathffmpeg.cmake
More file actions
199 lines (187 loc) · 8.62 KB
/
Copy pathffmpeg.cmake
File metadata and controls
199 lines (187 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# SPDX-License-Identifier: BSD-3-Clause
#
# Cross-build decoder-only FFmpeg static libraries for the ffmpeg_dec module.
#
# Invokes FFmpeg's autotools build as an ExternalProject, cross-compiled with the
# Zephyr target toolchain, enabling only the decoders selected in Kconfig. The
# source comes from the west-pinned FFmpeg project (see west.yml). Produces
# libavcodec/libavutil/libswresample .a under ${FFMPEG_INSTALL_DIR} for the LLEXT
# to link, and defines the 'ffmpeg_ext' target it must depend on.
include(ExternalProject)
# --- 1. Locate the FFmpeg source (west module), overridable for local trees ---
if(NOT DEFINED SOF_FFMPEG_SRC_DIR)
set(SOF_FFMPEG_SRC_DIR "${sof_top_dir}/../modules/audio/ffmpeg"
CACHE PATH "FFmpeg source tree (west-pinned)")
endif()
cmake_path(NORMAL_PATH SOF_FFMPEG_SRC_DIR)
if(NOT EXISTS "${SOF_FFMPEG_SRC_DIR}/configure")
message(FATAL_ERROR
"ffmpeg_dec: FFmpeg source not found at '${SOF_FFMPEG_SRC_DIR}'.\n"
"Run 'west update' (FFmpeg is pinned in west.yml) or pass "
"-DSOF_FFMPEG_SRC_DIR=<path-to-clean-ffmpeg-checkout>.")
endif()
# --- 2. Kconfig -> --enable-decoder / --enable-parser lists ---
set(_ff_decoders "")
set(_ff_parsers "")
if(CONFIG_FFMPEG_DEC_FLAC)
list(APPEND _ff_decoders flac)
list(APPEND _ff_parsers flac)
endif()
if(CONFIG_FFMPEG_DEC_AAC)
list(APPEND _ff_decoders aac aac_latm)
list(APPEND _ff_parsers aac aac_latm)
endif()
if(CONFIG_FFMPEG_DEC_OPUS)
list(APPEND _ff_decoders opus)
list(APPEND _ff_parsers opus)
endif()
if(CONFIG_FFMPEG_DEC_MP3)
list(APPEND _ff_decoders mp3)
list(APPEND _ff_parsers mpegaudio)
endif()
if(NOT _ff_decoders)
message(FATAL_ERROR "ffmpeg_dec: no decoder selected (Kconfig FFMPEG_DEC_*)")
endif()
list(JOIN _ff_decoders "," _ff_dec_csv)
list(JOIN _ff_parsers "," _ff_par_csv)
# --- 2b. Kconfig -> libavfilter audio filters (off unless a filter is chosen) ---
set(_ff_avfilter_cfg --disable-avfilter)
if(CONFIG_FFMPEG_BUILD_AVFILTER)
set(_ff_filters "")
if(CONFIG_FFMPEG_FILTER_AFFTDN)
list(APPEND _ff_filters afftdn)
endif()
list(JOIN _ff_filters "," _ff_filt_csv)
# abuffer/abuffersink feed/drain a filter graph; aformat negotiates format.
set(_ff_avfilter_cfg
--enable-avfilter
--enable-filter=abuffer,abuffersink,aformat,${_ff_filt_csv})
message(STATUS "ffmpeg_dec: enabling avfilter, filters [${_ff_filt_csv}]")
endif()
# --- 3. Derive the cross toolchain prefix from the Zephyr target compiler ---
# e.g. .../bin/xtensa-intel_ace30_ptl_zephyr-elf-gcc
# -> prefix .../bin/xtensa-intel_ace30_ptl_zephyr-elf-
get_filename_component(_tc_dir "${CMAKE_C_COMPILER}" DIRECTORY)
get_filename_component(_tc_name "${CMAKE_C_COMPILER}" NAME)
string(REGEX REPLACE "gcc$" "" _tc_prefix_name "${_tc_name}")
set(_ff_cross_prefix "${_tc_dir}/${_tc_prefix_name}")
# GCC 14 (Zephyr SDK) promotes these to errors; FFmpeg 7.x trips them.
set(_ff_extra_cflags "-fPIC -Wno-error=incompatible-pointer-types -Wno-error=implicit-function-declaration")
set(FFMPEG_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-install" CACHE INTERNAL "ffmpeg_dec libs")
# Hot/cold split: GCC emits av_cold (init/setup) functions into .text.unlikely.
# We rename that to SOF's .cold (post-install, below) so the LLEXT loader places
# it in DRAM. -mtext-section-literals interleaves each function's Xtensa L32R
# literals into its text section (as SOF does for LLEXT module code) so the
# renamed section carries its literals and L32R stays in range. We deliberately
# do NOT use -ffunction-sections: keeping one .text.unlikely per object lets a
# single objcopy --rename-section catch it, and lets .cold be placed by SOF's
# existing orphan .cold handling (a hand-written linker script that forces .cold
# ahead of .text overruns the SRAM budget and overlaps .rodata for big codecs).
if(CONFIG_FFMPEG_DEC_COLD_SPLIT)
# -mlongcalls: cold code lives in DRAM (.cold, relocated by the loader) far
# from the hot SRAM .text, so calls between them exceed call8 range; longcalls
# emit an indirect L32R+CALLX with unlimited range. -mtext-section-literals
# keeps each function's literals in its own text section so the rename carries
# them. Coarse split: rename the whole FFmpeg .text (not just av_cold) into
# .cold so essentially all of libav* executes from DRAM, freeing SRAM.
set(_ff_extra_cflags "${_ff_extra_cflags} -mtext-section-literals -mlongcalls")
set(_ff_objcopy "${_ff_cross_prefix}objcopy")
set(_ff_cold_rename
COMMAND ${_ff_objcopy} --rename-section .text=.cold
${FFMPEG_INSTALL_DIR}/lib/libavcodec.a
COMMAND ${_ff_objcopy} --rename-section .text=.cold
${FFMPEG_INSTALL_DIR}/lib/libavutil.a
COMMAND ${_ff_objcopy} --rename-section .text=.cold
${FFMPEG_INSTALL_DIR}/lib/libswresample.a)
endif()
# --- 3b. MP3 encoder via libshine (fixed-point; FFmpeg has no native one) ---
set(_ff_cfg_env "PATH=${_tc_dir}:$ENV{PATH}")
set(_ff_shine_cfg "")
set(_ff_shine_dep "")
if(CONFIG_FFMPEG_ENC_MP3)
set(SHINE_SRC "${sof_top_dir}/../modules/audio/shine")
if(NOT EXISTS "${SHINE_SRC}/src/lib/layer3.h")
message(FATAL_ERROR "ffmpeg_dec: libshine source not at '${SHINE_SRC}'; run 'west update'")
endif()
set(SHINE_INSTALL "${CMAKE_CURRENT_BINARY_DIR}/shine-install")
file(MAKE_DIRECTORY "${SHINE_INSTALL}/lib/pkgconfig" "${SHINE_INSTALL}/include/shine")
# pkg-config file for FFmpeg's require_pkg_config libshine.
file(WRITE "${SHINE_INSTALL}/lib/pkgconfig/shine.pc"
"prefix=${SHINE_INSTALL}
libdir=\${prefix}/lib
includedir=\${prefix}/include
Name: shine
Description: Shine fixed-point MP3 encoder
Version: 3.1.1
Libs: -L\${libdir} -lshine
Cflags: -I\${includedir}
")
# FFmpeg's -lshine link *test* pulls newlib malloc -> Zephyr runtime syms
# (z_errno_wrap, ...) that only exist at module load. Provide dummies so the
# configure test links; --extra-ldflags only affects tests, not the .a build.
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.c"
"int z_errno_wrap(void){return 0;}
void *stdout;
int open(const char *p, int f, ...){return -1;}
void *sbrk(int i){return (void *)-1;}
")
# libshine's lib needs no config.h; build the objects directly and archive
# (its autotools CLI/shared link cannot work bare-metal).
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/shine-build.sh"
"#!/bin/sh
set -e
export PATH=${_tc_dir}:\$PATH
mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/shine-obj
for f in ${SHINE_SRC}/src/lib/*.c; do
${CMAKE_C_COMPILER} -O2 -fPIC -mtext-section-literals -DSHINE_HAVE_BSWAP_H \\
-I${SHINE_SRC}/src/lib -c \"\$f\" \\
-o ${CMAKE_CURRENT_BINARY_DIR}/shine-obj/\$(basename \"\$f\").o
done
${_ff_cross_prefix}ar rcs ${SHINE_INSTALL}/lib/libshine.a ${CMAKE_CURRENT_BINARY_DIR}/shine-obj/*.o
cp ${SHINE_SRC}/src/lib/layer3.h ${SHINE_INSTALL}/include/shine/layer3.h
${CMAKE_C_COMPILER} -fPIC -c ${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.c \\
-o ${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o
")
add_custom_command(
OUTPUT "${SHINE_INSTALL}/lib/libshine.a" "${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o"
COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/shine-build.sh"
VERBATIM)
add_custom_target(shine_ext DEPENDS "${SHINE_INSTALL}/lib/libshine.a")
set(_ff_cfg_env "PATH=${_tc_dir}:$ENV{PATH}" "PKG_CONFIG_PATH=${SHINE_INSTALL}/lib/pkgconfig")
set(_ff_shine_cfg --enable-libshine --enable-encoder=libshine --pkg-config=pkg-config
"--extra-ldflags=${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o")
set(_ff_shine_dep shine_ext)
message(STATUS "ffmpeg_dec: enabling MP3 encoder via libshine (${SHINE_SRC})")
endif()
# --- 4. Configure + make + install (out-of-tree; source must be clean) ---
ExternalProject_Add(ffmpeg_ext
DEPENDS ${_ff_shine_dep}
SOURCE_DIR "${SOF_FFMPEG_SRC_DIR}"
BUILD_IN_SOURCE 0
CONFIGURE_COMMAND
${CMAKE_COMMAND} -E env ${_ff_cfg_env}
<SOURCE_DIR>/configure
--prefix=${FFMPEG_INSTALL_DIR}
--enable-cross-compile --target-os=none --arch=xtensa
--cross-prefix=${_ff_cross_prefix} --cc=${CMAKE_C_COMPILER}
--disable-asm --disable-all --disable-everything --disable-autodetect
--disable-programs --disable-doc --disable-network
--disable-avformat --disable-avdevice ${_ff_avfilter_cfg}
--disable-swscale --disable-postproc
--disable-pthreads --disable-w32threads --disable-os2threads
--disable-runtime-cpudetect --disable-debug
--enable-avcodec --enable-avutil --enable-swresample
--enable-decoder=${_ff_dec_csv} --enable-parser=${_ff_par_csv}
${_ff_shine_cfg}
--enable-small --enable-pic
"--extra-cflags=${_ff_extra_cflags}"
BUILD_COMMAND
${CMAKE_COMMAND} -E env "PATH=${_tc_dir}:$ENV{PATH}" make -j 8
INSTALL_COMMAND make install
${_ff_cold_rename}
BUILD_BYPRODUCTS
${FFMPEG_INSTALL_DIR}/lib/libavcodec.a
${FFMPEG_INSTALL_DIR}/lib/libavutil.a
${FFMPEG_INSTALL_DIR}/lib/libswresample.a
)
message(STATUS "ffmpeg_dec: cross-building FFmpeg decoders [${_ff_dec_csv}] from ${SOF_FFMPEG_SRC_DIR}")