FFmpeg在Windows上编译动态库以及解决报错
- 下载FFmpeg和X264源码
- 安装MSYS2
- 编译X264
- 编译FFmpeg
- 编译报错"Error: operand type mismatch for `shr' "
- 编译报错"makeinfo: error parsing ./doc/t2h.pm: Undefined subroutine &Texinfo::Config::set_from_init_file call "
下载FFmpeg和X264源码
可通过git下载ffmpeg
git clone https://github.com/FFmpeg/FFmpeg.git
也可在github下载ffmpeg压缩包
https://github.com/FFmpeg/FFmpeg/tree/master
通过git下载X264
git clone https://code.videolan.org/videolan/x264.git
也可在这下载压缩包
https://code.videolan.org/videolan/x264
安装MSYS2
在MSYS2官网下载安装包,安装即可。
在安装目录下找到mingw64.exe双击打开shell(如果是32位的就打开mingw32.exe)
安装mingw64编译链和依赖
#安装
pacman -S mingw-w64-x86_64-toolchain
pacman -S base-devel
pacman -S yasm nasm
#测试
gcc -v
测试没问题后需要添加系统环境变量
编译X264
在X264源码同级目录创建编译结果输出文件夹X264_install
然后在同级目录创建build-x264.txt文本写入以下内容后更改后缀为build-x264.sh
#!/bin/sh
basepath=$(cd `dirname $0`;pwd)
echo ${basepath}cd ${basepath}/x264 # X264源码路径名称
pwd./configure --prefix=${basepath}/x264_install --enable-shared
make -j8
make install
然后在mingw64的shell中中cd到脚本目录下
运行脚本文件即可
./build-x264.sh
编译FFmpeg
在FFmpeg源码同级目录创建编译结果输出文件夹FFmpeg_install
然后在同级目录创建build-ffmpeg.txt文本写入以下内容后更改后缀为build-ffmpeg.sh
#!/bin/sh
basepath=$(cd `dirname $0`;pwd)
echo ${basepath}cd ${basepath}/FFmpeg-n5.1.2 ## ffmpeg源码路径名称
pwdexport PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:/d/FFmpeg-n5.1.2/x264_install/lib/pkgconfig ##x264_install编译后生成的x264.pc的目录
echo ${PKG_CONFIG_PATH}./configure --prefix=${basepath}/FFmpeg_install \
--enable-gpl --enable-libx264 --disable-static --enable-shared \
--extra-cflags=-l${basepath}/x264_install/include --extra-ldflags=-L${basepath}/x264_install/lib make -j8
make install
然后在mingw64的shell中cd到脚本目录下
运行脚本文件即可
./build-ffmpeg.sh
编译报错"Error: operand type mismatch for `shr’ "
这是因为旧版ffmpeg的在libavcodec/x86/mathops.h中有bug,在新的版本已经修复了,所以只需要替换一下新版本的libavcodec/x86/mathops.h文件即可解决。
libavcodec/x86/mathops.h文件内容如下
/** simple math operations* Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> et al** This file is part of FFmpeg.** FFmpeg is free software; you can redistribute it and/or* modify it under the terms of the GNU Lesser General Public* License as published by the Free Software Foundation; either* version 2.1 of the License, or (at your option) any later version.** FFmpeg is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU* Lesser General Public License for more details.** You should have received a copy of the GNU Lesser General Public* License along with FFmpeg; if not, write to the Free Software* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA*/#ifndef AVCODEC_X86_MATHOPS_H
#define AVCODEC_X86_MATHOPS_H#include "config.h"#include "libavutil/common.h"
#include "libavutil/x86/asm.h"#if HAVE_INLINE_ASM#if ARCH_X86_32#define MULL MULL
static av_always_inline av_const int MULL(int a, int b, unsigned shift)
{int rt, dummy;if (__builtin_constant_p(shift))__asm__ ("imull %3 \n\t""shrdl %4, %%edx, %%eax \n\t":"=a"(rt), "=d"(dummy):"a"(a), "rm"(b), "i"(shift & 0x1F));else__asm__ ("imull %3 \n\t""shrdl %4, %%edx, %%eax \n\t":"=a"(rt), "=d"(dummy):"a"(a), "rm"(b), "c"((uint8_t)shift));return rt;
}#define MULH MULH
static av_always_inline av_const int MULH(int a, int b)
{int rt, dummy;__asm__ ("imull %3":"=d"(rt), "=a"(dummy):"a"(a), "rm"(b));return rt;
}#define MUL64 MUL64
static av_always_inline av_const int64_t MUL64(int a, int b)
{int64_t rt;__asm__ ("imull %2":"=A"(rt):"a"(a), "rm"(b));return rt;
}#endif /* ARCH_X86_32 */#if HAVE_I686
/* median of 3 */
#define mid_pred mid_pred
static inline av_const int mid_pred(int a, int b, int c)
{int i=b;__asm__ ("cmp %2, %1 \n\t""cmovg %1, %0 \n\t""cmovg %2, %1 \n\t""cmp %3, %1 \n\t""cmovl %3, %1 \n\t""cmp %1, %0 \n\t""cmovg %1, %0 \n\t":"+&r"(i), "+&r"(a):"r"(b), "r"(c));return i;
}#if HAVE_6REGS
#define COPY3_IF_LT(x, y, a, b, c, d)\
__asm__ volatile(\"cmpl %0, %3 \n\t"\"cmovl %3, %0 \n\t"\"cmovl %4, %1 \n\t"\"cmovl %5, %2 \n\t"\: "+&r" (x), "+&r" (a), "+r" (c)\: "r" (y), "r" (b), "r" (d)\
);
#endif /* HAVE_6REGS */#endif /* HAVE_I686 */#define MASK_ABS(mask, level) \__asm__ ("cdq \n\t" \"xorl %1, %0 \n\t" \"subl %1, %0 \n\t" \: "+a"(level), "=&d"(mask))// avoid +32 for shift optimization (gcc should do that ...)
#define NEG_SSR32 NEG_SSR32
static inline int32_t NEG_SSR32( int32_t a, int8_t s){if (__builtin_constant_p(s))__asm__ ("sarl %1, %0\n\t": "+r" (a): "i" (-s & 0x1F));else__asm__ ("sarl %1, %0\n\t": "+r" (a): "c" ((uint8_t)(-s)));return a;
}#define NEG_USR32 NEG_USR32
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){if (__builtin_constant_p(s))__asm__ ("shrl %1, %0\n\t": "+r" (a): "i" (-s & 0x1F));else__asm__ ("shrl %1, %0\n\t": "+r" (a): "c" ((uint8_t)(-s)));return a;
}#endif /* HAVE_INLINE_ASM */
#endif /* AVCODEC_X86_MATHOPS_H */
编译报错"makeinfo: error parsing ./doc/t2h.pm: Undefined subroutine &Texinfo::Config::set_from_init_file call "
新版本的html好像也有bug,解决办法是在编译选项中添加下面语句。
--disable-htmlpages
添加编译选项后的脚本文件
#!/bin/sh
basepath=$(cd `dirname $0`;pwd)
echo ${basepath}cd ${basepath}/FFmpeg-n5.1.2 ## ffmpeg源码路径名称
pwdexport PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:/d/FFmpeg-n5.1.2/x264_install/lib/pkgconfig ##x264_install编译后生成的x264.pc的目录
echo ${PKG_CONFIG_PATH}./configure --prefix=${basepath}/FFmpeg_install \
--enable-gpl --enable-libx264 --disable-static --enable-shared --disable-htmlpages \
--extra-cflags=-l${basepath}/x264_install/include --extra-ldflags=-L${basepath}/x264_install/lib make -j8
make install
再次执行脚本
./build-ffmpeg.sh
编译成功的动态库
参考博文