forked from jocover/jetson-ffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 29
/
ffpatch.sh
executable file
·213 lines (173 loc) · 11.2 KB
/
ffpatch.sh
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
#path to root ffmpeg soyrces dir must be passed as first arg to the script
FF_DIR_ROOT=${1}
FF_DIR_LIBAVCODEC=${FF_DIR_ROOT}"/libavcodec"
FF_FILE_CONFIGURE=${FF_DIR_ROOT}"/configure"
FF_FILE_LIBAVCODEC_MAKEFILE=${FF_DIR_LIBAVCODEC}"/Makefile"
FF_FILE_LIBAVCODEC_ALLCODECSC=${FF_DIR_LIBAVCODEC}"/allcodecs.c"
FF_FILE_LIBAVCODEC_VERSIONH=${FF_DIR_LIBAVCODEC}"/version.h"
FF_FILE_LIBAVCODEC_VERSIONMAJORH=${FF_DIR_LIBAVCODEC}"/version_major.h"
FF_LIBAVCODEC_VERSION_MAJOR=0
BKP_DIR=./bkp
BKP_FILE_CONFIGURE=${BKP_DIR}/configure
BKP_FILE_LIBAVCODEC_MAKEFILE=${BKP_DIR}/Makefile
BKP_FILE_LIBAVCODEC_ALLCODECSC=${BKP_DIR}/allcodecs.c
if [ ! -d "$FF_DIR_ROOT" ]; then
echo "[E]: $FF_DIR_ROOT does not exist or not a directory. You must specify path to ffmpeg sources directory."
exit 1
fi
if [ ! -d "$FF_DIR_LIBAVCODEC" ]; then
echo "[E]: $FF_DIR_LIBAVCODEC does not exist or not a directory. Your ffmpeg sources are not complete or this ffmpeg version not supported by the script yet."
exit 1
fi
if [ ! -f "$FF_FILE_CONFIGURE" ]; then
echo "[E]: $FF_FILE_CONFIGURE does not exist or not a file. Your ffmpeg sources are not complete or this ffmpeg version not supported by the script yet."
exit 1
fi
if [ ! -f "$FF_FILE_LIBAVCODEC_MAKEFILE" ]; then
echo "[E]: $FF_FILE_LIBAVCODEC_MAKEFILE does not exist or not a file. Your ffmpeg sources are not complete or this ffmpeg version not supported by the script yet."
exit 1
fi
if [ ! -f "$FF_FILE_LIBAVCODEC_ALLCODECSC" ]; then
echo "[E]: $FF_FILE_LIBAVCODEC_ALLCODECSC does not exist or not a file. Your ffmpeg sources are not complete or this ffmpeg version not supported by the script yet."
exit 1
fi
#read and check ffmpeg libavcodec version
if [ -f "$FF_FILE_LIBAVCODEC_VERSIONMAJORH" ]; then
FF_LIBAVCODEC_VERSION_MAJOR=$(grep '#define LIBAVCODEC_VERSION_MAJOR' $FF_FILE_LIBAVCODEC_VERSIONMAJORH | awk '{printf "%d",$3}')
elif [ -f "$FF_FILE_LIBAVCODEC_VERSIONH" ]; then
FF_LIBAVCODEC_VERSION_MAJOR=$(grep '#define LIBAVCODEC_VERSION_MAJOR' $FF_FILE_LIBAVCODEC_VERSIONH | awk '{printf "%d",$3}')
else
echo "[E]: Can't find ffmpeg libavcodec version file. Your ffmpeg sources are not complete or this ffmpeg version not supported by the script yet."
exit 1
fi
if [ "$FF_LIBAVCODEC_VERSION_MAJOR" -eq 0 ]; then
echo "[E]: Can't read ffmpeg libavcodec version. Your ffmpeg sources are not complete or this ffmpeg version not supported by the script yet."
exit 1
fi
rm -rf "$BKP_DIR" 2>&1 > /dev/null
if ! mkdir "$BKP_DIR" 2>&1 > /dev/null; then
echo "Can not create backup dir $BKP_DIR"
exit 1
fi
cp "$FF_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE"
cp "$FF_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE"
cp "$FF_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC"
################## MODIFY configure ############################
function path_ff_configure ()
{
#add nvmpi to ffmpeg configure show_help() function.
cp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"
sed -i '/--disable-videotoolbox/a \ \ --enable-nvmpi enable nvmpi code [no]' "$BKP_FILE_CONFIGURE"
if cmp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"; then return 1; fi;
#add nvmpi to ffmpeg configure HWACCEL_LIBRARY_LIST.
cp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"
sed -i '/HWACCEL_LIBRARY_LIST="/a \ \ \ \ nvmpi' "$BKP_FILE_CONFIGURE"
if cmp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"; then return 1; fi;
#add nvmpi avc/h264 deps. insert before h264_nvenc_encoder_deps="nvenc"
cp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"
sed -i '/h264_nvenc_encoder_deps="nvenc"/i h264_nvmpi_encoder_deps="nvmpi"\nh264_nvmpi_decoder_deps="nvmpi"\nh264_nvmpi_decoder_select="h264_mp4toannexb_bsf"' "$BKP_FILE_CONFIGURE"
if cmp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"; then return 1; fi;
#add nvmpi hevc/h265 deps. insert before hevc_nvenc_encoder_deps="nvenc"
cp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"
sed -i '/hevc_nvenc_encoder_deps="nvenc"/i hevc_nvmpi_encoder_deps="nvmpi"\nhevc_nvmpi_decoder_deps="nvmpi"\nhevc_nvmpi_decoder_select="hevc_mp4toannexb_bsf"' "$BKP_FILE_CONFIGURE"
if cmp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"; then return 1; fi;
#add nvmpi mpeg2 deps. insert after mpeg2_cuvid_decoder_deps="cuvid"
cp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"
sed -i '/mpeg2_cuvid_decoder_deps="cuvid"/a mpeg2_nvmpi_decoder_deps="nvmpi"' "$BKP_FILE_CONFIGURE"
if cmp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"; then return 1; fi;
#add nvmpi mpeg4 deps. insert after mpeg4_cuvid_decoder_deps="cuvid"
cp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"
sed -i '/mpeg4_cuvid_decoder_deps="cuvid"/a mpeg4_nvmpi_decoder_deps="nvmpi"' "$BKP_FILE_CONFIGURE"
if cmp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"; then return 1; fi;
#add nvmpi vp8 deps. insert after vp8_cuvid_decoder_deps="cuvid"
cp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"
sed -i '/vp8_cuvid_decoder_deps="cuvid"/a vp8_nvmpi_decoder_deps="nvmpi"' "$BKP_FILE_CONFIGURE"
if cmp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"; then return 1; fi;
#add nvmpi vp8 deps. insert after vp9_cuvid_decoder_deps="cuvid"
cp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"
sed -i '/vp9_cuvid_decoder_deps="cuvid"/a vp9_nvmpi_decoder_deps="nvmpi"' "$BKP_FILE_CONFIGURE"
if cmp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"; then return 1; fi;
#insert before enabled libx264 line.
cp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"
sed -i '/enabled libx264/i enabled nvmpi && require_pkg_config nvmpi nvmpi nvmpi.h nvmpi_create_decoder' "$BKP_FILE_CONFIGURE"
if cmp "$BKP_FILE_CONFIGURE" "$BKP_FILE_CONFIGURE.1"; then return 1; fi;
return 0;
}
################## MODIFY configure ############################
################## MODIFY libavcodec/Makefile ############################
function path_ff_libavcodec_Makefile ()
{
#add nvmpi avc/h264 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"
sed -i '/OBJS-\$(CONFIG_H264_NVENC_ENCODER)/i OBJS-\$(CONFIG_H264_NVMPI_DECODER) += nvmpi_dec.o\nOBJS-$(CONFIG_H264_NVMPI_ENCODER) += nvmpi_enc.o' "$BKP_FILE_LIBAVCODEC_MAKEFILE"
if cmp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"; then return 1; fi;
#add nvmpi hevc/h265 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"
sed -i '/OBJS-\$(CONFIG_HEVC_NVENC_ENCODER)/i OBJS-\$(CONFIG_HEVC_NVMPI_DECODER) += nvmpi_dec.o\nOBJS-$(CONFIG_HEVC_NVMPI_ENCODER) += nvmpi_enc.o' "$BKP_FILE_LIBAVCODEC_MAKEFILE"
if cmp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"; then return 1; fi;
#add nvmpi mpeg2 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"
sed -i '/OBJS-\$(CONFIG_MPEG2_CUVID_DECODER)/i OBJS-\$(CONFIG_MPEG2_NVMPI_DECODER) += nvmpi_dec.o' "$BKP_FILE_LIBAVCODEC_MAKEFILE"
if cmp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"; then return 1; fi;
#add nvmpi mpeg4 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"
sed -i '/OBJS-\$(CONFIG_MPEG4_CUVID_DECODER)/i OBJS-\$(CONFIG_MPEG4_NVMPI_DECODER) += nvmpi_dec.o' "$BKP_FILE_LIBAVCODEC_MAKEFILE"
if cmp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"; then return 1; fi;
#add nvmpi vp8 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"
sed -i '/OBJS-\$(CONFIG_VP8_CUVID_DECODER)/i OBJS-\$(CONFIG_VP8_NVMPI_DECODER) += nvmpi_dec.o' "$BKP_FILE_LIBAVCODEC_MAKEFILE"
if cmp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"; then return 1; fi;
#add nvmpi vp9 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"
sed -i '/OBJS-\$(CONFIG_VP9_CUVID_DECODER)/i OBJS-\$(CONFIG_VP9_NVMPI_DECODER) += nvmpi_dec.o' "$BKP_FILE_LIBAVCODEC_MAKEFILE"
if cmp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$BKP_FILE_LIBAVCODEC_MAKEFILE.1"; then return 1; fi;
return 0;
}
################## MODIFY libavcodec/Makefile ############################
################## MODIFY libavcodec/allcodecs.c ############################
function path_ff_libavcodec_allcodecsc ()
{
FF_CODEC_INTERFACE="extern AVCodec"
if [ "$FF_LIBAVCODEC_VERSION_MAJOR" -gt 59 ]; then
FF_CODEC_INTERFACE="extern const FFCodec"
fi
#add nvmpi avc/h264 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"
sed -i "/$FF_CODEC_INTERFACE ff_h264_decoder;/a $FF_CODEC_INTERFACE ff_h264_nvmpi_decoder;\n$FF_CODEC_INTERFACE ff_h264_nvmpi_encoder;" "$BKP_FILE_LIBAVCODEC_ALLCODECSC"
if cmp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"; then return 1; fi;
#add nvmpi hevc/h265 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"
sed -i "/$FF_CODEC_INTERFACE ff_hevc_decoder;/a $FF_CODEC_INTERFACE ff_hevc_nvmpi_decoder;\n$FF_CODEC_INTERFACE ff_hevc_nvmpi_encoder;" "$BKP_FILE_LIBAVCODEC_ALLCODECSC"
if cmp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"; then return 1; fi;
#add nvmpi mpeg2 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"
sed -i "/$FF_CODEC_INTERFACE ff_mpeg2video_decoder;/a $FF_CODEC_INTERFACE ff_mpeg2_nvmpi_decoder;" "$BKP_FILE_LIBAVCODEC_ALLCODECSC"
if cmp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"; then return 1; fi;
#add nvmpi mpeg4 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"
sed -i "/$FF_CODEC_INTERFACE ff_mpeg4_decoder;/a $FF_CODEC_INTERFACE ff_mpeg4_nvmpi_decoder;" "$BKP_FILE_LIBAVCODEC_ALLCODECSC"
if cmp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"; then return 1; fi;
#add nvmpi vp8 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"
sed -i "/$FF_CODEC_INTERFACE ff_vp8_decoder;/a $FF_CODEC_INTERFACE ff_vp8_nvmpi_decoder;" "$BKP_FILE_LIBAVCODEC_ALLCODECSC"
if cmp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"; then return 1; fi;
#add nvmpi vp9 encoder and decoder
cp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"
sed -i "/$FF_CODEC_INTERFACE ff_vp9_decoder;/a $FF_CODEC_INTERFACE ff_vp9_nvmpi_decoder;" "$BKP_FILE_LIBAVCODEC_ALLCODECSC"
if cmp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$BKP_FILE_LIBAVCODEC_ALLCODECSC.1"; then return 1; fi;
return 0;
}
################## MODIFY libavcodec/allcodecs.c ############################
if path_ff_configure 2>&1 > /dev/null; then echo "$FF_FILE_CONFIGURE is successfully patched!"; else echo "Patching $FF_FILE_CONFIGURE failed!"; exit 1; fi;
if path_ff_libavcodec_Makefile 2>&1 > /dev/null; then echo "$FF_FILE_LIBAVCODEC_MAKEFILE is successfully patched!"; else echo "Patching $FF_FILE_LIBAVCODEC_MAKEFILE failed!"; exit 1; fi;
if path_ff_libavcodec_allcodecsc 2>&1 > /dev/null; then echo "$FF_FILE_LIBAVCODEC_ALLCODECSC is successfully patched!"; else echo "Patching $FF_FILE_LIBAVCODEC_ALLCODECSC failed!"; exit 1; fi;
cp "$BKP_FILE_CONFIGURE" "$FF_FILE_CONFIGURE"
cp "$BKP_FILE_LIBAVCODEC_MAKEFILE" "$FF_FILE_LIBAVCODEC_MAKEFILE"
cp "$BKP_FILE_LIBAVCODEC_ALLCODECSC" "$FF_FILE_LIBAVCODEC_ALLCODECSC"
#copy nvmpi enc and dec files to ffmpeg libavcodec dir
cp ffmpeg_dev/common/libavcodec/nvmpi_dec.c ${FF_DIR_LIBAVCODEC}"/nvmpi_dec.c"
cp ffmpeg_dev/common/libavcodec/nvmpi_enc.c ${FF_DIR_LIBAVCODEC}"/nvmpi_enc.c"
echo "Success!"
rm -rf "$BKP_DIR" 2>&1 > /dev/null
exit 0