FFmpeg的代碼結構和編譯系統

FFmpeg的目錄結構

打開FFmpeg的源代碼目錄,可以發現目錄結構相對比較簡單,

目錄結構

compact目錄下主要是頭文件,是爲了兼容不同平臺或系統的數據類型定義,例如compat/msvcrt/snprintf.h和snprintf.c是兼容C99的snprintf()和vsnprintf()聲明及實現;
libavcodec目錄下是編碼/解碼庫所在的代碼路徑;
libavdevice目錄下是和一些特定設備相關的接口實現,比如sdl顯示設備,alsa音頻輸出設備,opengl圖形加速設備,v4l2視頻設備(比如攝像頭);
libavfilter是一個類似DirecShow機制的幀處理函數集,通過graph-based機制,將不同處理功能的filter函數,鏈接(link)起來;
libavformat是複用/解複用庫,一般用於多媒體文件的處理;
libavresample顧名思義,是一個重採樣庫,比如音頻採樣率從44.1KHz到96KHz;
libavutil是支撐FFmpeg的工具集,像fifo管理,內存管理,時間管理等;
libpostproc是後期處理庫;
libswresample是音頻重採樣,格式轉換和混合;功能和libavresample十分類似,是FFmpeg相對早期的實現方法;
libswscale是顏色空間轉換和大小尺寸縮放的庫;

cmdutils.c是用戶使用命令行進行ffmpeg操作時用到的工具;
ffmpeg.c是ffmpeg工具的源碼;
ffplay.c是一個基於FFmpeg庫文件的播放器實例;
ffprobe.c是解析媒體文件格式信息的例子;
ffserver.c是RTSP/HTTP流媒體服務器的源代碼;
configure是編譯前執行配置的腳本;

FFmpeg的配置

FFmpeg在編譯前需要進行配置,它的配置選項也極其豐富,可以通過如下命令來查看詳細的配置信息,

./configure --help

下面,我們來分類瞭解一下FFmpeg的配置詳情。

幫助選項,可以通過列表顯示編解碼器信息,複用/解複用信息,硬件加速,解析器,協議,過濾器,輸入輸出設備等信息,

Help options:
  --help                   print this message
  --list-decoders          show all available decoders
  --list-encoders          show all available encoders
  --list-hwaccels          show all available hardware accelerators
  --list-demuxers          show all available demuxers
  --list-muxers            show all available muxers
  --list-parsers           show all available parsers
  --list-protocols         show all available protocols
  --list-bsfs              show all available bitstream filters
  --list-indevs            show all available input devices
  --list-outdevs           show all available output devices
  --list-filters           show all available filters

標準選項,比如把log輸出重定向到某路徑,編譯後的庫文件的安裝路徑等,[ ]裏的內容是默認的配置,

Standard options:
  --logfile=FILE           log tests and output to FILE [config.log]
  --disable-logging        do not log configure debug information
  --fatal-warnings         fail if any configure warning is generated
  --prefix=PREFIX          install in PREFIX []
  --bindir=DIR             install binaries in DIR [PREFIX/bin]
  --datadir=DIR            install data files in DIR [PREFIX/share/ffmpeg]
  --docdir=DIR             install documentation in DIR [PREFIX/share/doc/ffmpeg]
  --libdir=DIR             install libs in DIR [PREFIX/lib]
  --shlibdir=DIR           install shared libs in DIR [LIBDIR]
  --incdir=DIR             install includes in DIR [PREFIX/include]
  --mandir=DIR             install man page in DIR [PREFIX/share/man]
  --pkgconfigdir=DIR       install pkg-config files in DIR [LIBDIR/pkgconfig]
  --enable-rpath           use rpath to allow installing libraries in paths
                           not part of the dynamic linker search path
                           use rpath when linking programs [USE WITH CARE]
  --install-name-dir=DIR   Darwin directory name for installed targets

開源許可的相關選項,

Licensing options:
  --enable-gpl             allow use of GPL code, the resulting libs
                           and binaries will be under GPL [no]
  --enable-version3        upgrade (L)GPL to version 3 [no]
  --enable-nonfree         allow use of nonfree code, the resulting libs
                           and binaries will be unredistributable [no]

配置選項,比如靜態庫和動態庫的選擇,編譯優化等,

Configuration options:
  --disable-static         do not build static libraries [no]
  --enable-shared          build shared libraries [no]
  --enable-small           optimize for size instead of speed
  --disable-runtime-cpudetect disable detecting cpu capabilities at runtime (smaller binary)
  --enable-gray            enable full grayscale support (slower color)
  --disable-swscale-alpha  disable alpha channel support in swscale
  --disable-all            disable building components, libraries and programs
  --enable-raise-major     increase major version numbers in sonames [no]

程序選項,每個選項對應一個可單獨執行的程序,可選擇編譯還是不編譯,

Program options:
  --disable-programs       do not build command line programs
  --disable-ffmpeg         disable ffmpeg build
  --disable-ffplay         disable ffplay build
  --disable-ffprobe        disable ffprobe build
  --disable-ffserver       disable ffserver build

文檔選項,是否關閉生成文檔,

Documentation options:
  --disable-doc            do not build documentation
  --disable-htmlpages      do not build HTML documentation pages
  --disable-manpages       do not build man documentation pages
  --disable-podpages       do not build POD documentation pages
  --disable-txtpages       do not build text documentation pages

模塊功能配置,比如是否編譯某些模塊,壓縮算法等配置,

Component options:
  --disable-avdevice       disable libavdevice build
  --disable-avcodec        disable libavcodec build
  --disable-avformat       disable libavformat build
  --disable-swresample     disable libswresample build
  --disable-swscale        disable libswscale build
  --disable-postproc       disable libpostproc build
  --disable-avfilter       disable libavfilter build
  --enable-avresample      enable libavresample build [no]
  --disable-pthreads       disable pthreads [autodetect]
  --disable-w32threads     disable Win32 threads [autodetect]
  --disable-os2threads     disable OS/2 threads [autodetect]
  --disable-network        disable network support [no]
  --disable-dct            disable DCT code
  --disable-dwt            disable DWT code
  --disable-error-resilience disable error resilience code
  --disable-lsp            disable LSP code
  --disable-lzo            disable LZO decoder code
  --disable-mdct           disable MDCT code
  --disable-rdft           disable RDFT code
  --disable-fft            disable FFT code
  --disable-faan           disable floating point AAN (I)DCT code
  --disable-pixelutils     disable pixel utils in libavutil

獨立功能選項,比如編解碼開關,可以配置到某一編解碼類型,

Individual component options:
  --disable-everything     disable all components listed below
  --disable-encoder=NAME   disable encoder NAME
  --enable-encoder=NAME    enable encoder NAME
  --disable-encoders       disable all encoders
  --disable-decoder=NAME   disable decoder NAME
  --enable-decoder=NAME    enable decoder NAME
  --disable-decoders       disable all decoders
  --disable-hwaccel=NAME   disable hwaccel NAME
  --enable-hwaccel=NAME    enable hwaccel NAME
  --disable-hwaccels       disable all hwaccels
  --disable-muxer=NAME     disable muxer NAME
  --enable-muxer=NAME      enable muxer NAME
  --disable-muxers         disable all muxers
  --disable-demuxer=NAME   disable demuxer NAME
  --enable-demuxer=NAME    enable demuxer NAME
  --disable-demuxers       disable all demuxers
  --enable-parser=NAME     enable parser NAME
  --disable-parser=NAME    disable parser NAME
  --disable-parsers        disable all parsers
  --enable-bsf=NAME        enable bitstream filter NAME
  --disable-bsf=NAME       disable bitstream filter NAME
  --disable-bsfs           disable all bitstream filters
  --enable-protocol=NAME   enable protocol NAME
  --disable-protocol=NAME  disable protocol NAME
  --disable-protocols      disable all protocols
  --enable-indev=NAME      enable input device NAME
  --disable-indev=NAME     disable input device NAME
  --disable-indevs         disable input devices
  --enable-outdev=NAME     enable output device NAME
  --disable-outdev=NAME    disable output device NAME
  --disable-outdevs        disable output devices
  --disable-devices        disable all devices
  --enable-filter=NAME     enable filter NAME
  --disable-filter=NAME    disable filter NAME
  --disable-filters        disable all filters

擴展庫支持的配置,也就是依賴第三方的庫文件,由FFmpeg提供接口調用相關的庫,比如要開啓x264的支持 ,
–enable-libx264,要關閉Intel硬件加速 , –disable-vaapi,

External library support:

  Using any of the following switches will allow FFmpeg to link to the
  corresponding external library. All the components depending on that library
  will become enabled, if all their other dependencies are met and they are not
  explicitly disabled. E.g. --enable-libwavpack will enable linking to
  libwavpack and allow the libwavpack encoder to be built, unless it is
  specifically disabled with --disable-encoder=libwavpack.

  Note that only the system libraries are auto-detected. All the other external
  libraries must be explicitly enabled.

  Also note that the following help text describes the purpose of the libraries
  themselves, not all their features will necessarily be usable by FFmpeg.

  --enable-avisynth        enable reading of AviSynth script files [no]
  --disable-bzlib          disable bzlib [autodetect]
  --enable-chromaprint     enable audio fingerprinting with chromaprint [no]
  --enable-frei0r          enable frei0r video filtering [no]
  --enable-gcrypt          enable gcrypt, needed for rtmp(t)e support
                           if openssl, librtmp or gmp is not used [no]
  --enable-gmp             enable gmp, needed for rtmp(t)e support

………………

工具鏈配置選項

Toolchain options:
  --arch=ARCH              select architecture []
  --cpu=CPU                select the minimum required CPU (affects
                           instruction selection, may crash on older CPUs)
  --cross-prefix=PREFIX    use PREFIX for compilation tools []
  --progs-suffix=SUFFIX    program name suffix []
  --enable-cross-compile   assume a cross-compiler is used
  --sysroot=PATH           root of cross-build tree
  --sysinclude=PATH        location of cross-build system headers
  --target-os=OS           compiler targets OS []
  --target-exec=CMD        command to run executables on target
  --target-path=DIR        path to view of build directory on target
  --target-samples=DIR     path to samples directory on target
  --tempprefix=PATH        force fixed dir/prefix instead of mktemp for checks
  --toolchain=NAME         set tool defaults according to NAME
  --nm=NM                  use nm tool NM [nm -g]
  --ar=AR                  use archive tool AR [ar]
  --as=AS                  use assembler AS []
  --ln_s=LN_S              use symbolic link tool LN_S [ln -s -f]
  --strip=STRIP            use strip tool STRIP [strip]

高級配置選項(專家級),主要是和內存分配,編解碼相關的一些高級選項,一般無需涉及,

Advanced options (experts only):
  --malloc-prefix=PREFIX   prefix malloc and related names with PREFIX
  --custom-allocator=NAME  use a supported custom allocator
  --disable-symver         disable symbol versioning
  --enable-hardcoded-tables use hardcoded tables instead of runtime generation
  --disable-safe-bitstream-reader
                           disable buffer boundary checking in bitreaders
                           (faster, but may crash)
  --enable-memalign-hack   emulate memalign, interferes with memory debuggers
  --sws-max-filter-size=N  the max filter size swscale uses [256]

優化選項(也是專家級),是和目標平臺target有關的優化,比如音視頻相關的指令及彙編代碼,

Optimization options (experts only):
  --disable-asm            disable all assembly optimizations
  --disable-altivec        disable AltiVec optimizations
  --disable-vsx            disable VSX optimizations
  --disable-power8         disable POWER8 optimizations
  --disable-amd3dnow       disable 3DNow! optimizations
  --disable-amd3dnowext    disable 3DNow! extended optimizations
  --disable-mmx            disable MMX optimizations
  --disable-mmxext         disable MMXEXT optimizations
  --disable-sse            disable SSE optimizations
  --disable-sse2           disable SSE2 optimizations
  --disable-sse3           disable SSE3 optimizations
  --disable-ssse3          disable SSSE3 optimizations

………………

開發者配置選項,在調試FFmpeg源代碼時使用,

Developer options (useful when working on FFmpeg itself):
  --disable-debug          disable debugging symbols
  --enable-debug=LEVEL     set the debug level []
  --disable-optimizations  disable compiler optimizations
  --enable-extra-warnings  enable more compiler warnings
  --disable-stripping      disable stripping of executables and shared libraries
  --assert-level=level     0(default), 1 or 2, amount of assertion testing,
                           2 causes a slowdown at runtime.
  --enable-memory-poisoning fill heap uninitialized allocated space with arbitrary data
  --valgrind=VALGRIND      run "make fate" tests through valgrind to detect memory
                           leaks and errors, using the specified valgrind binary.
                           Cannot be combined with --target-exec
  --enable-ftrapv          Trap arithmetic overflows
  --samples=PATH           location of test samples for FATE, if not set use
                           $FATE_SAMPLES at make invocation time.
  --enable-neon-clobber-test check NEON registers for clobbering (should be
                           used only for debugging purposes)
  --enable-xmm-clobber-test check XMM registers for clobbering (Win64-only;
                           should be used only for debugging purposes)
  --enable-random          randomly enable/disable components
  --disable-random
  --enable-random=LIST     randomly enable/disable specific components or
  --disable-random=LIST    component groups. LIST is a comma-separated list
                           of NAME[:PROB] entries where NAME is a component
                           (group) and PROB the probability associated with
                           NAME (default 0.5).
  --random-seed=VALUE      seed value for --enable/disable-random
  --disable-valgrind-backtrace do not print a backtrace under Valgrind
                           (only applies to --disable-optimizations builds)

從上可知,FFmpeg源碼的配置選項十分豐富,但是條理十分清晰,執行配置的操作也十分簡單,只需要在configure腳本後跟上相關的選項即可,比如要開啓x264的支持,可以如下,

./configure --enable-libx264 --enable-gpl

整個配置結束後,會在FFmpeg源代碼的根目錄下生成config.mak文件,其內容是configure腳本根據前述配置(可能很多選項是默認)自動生成的key-value值,供後面make編譯使用。

FFmpeg的編譯系統

FFmpeg的編譯是依賴於源代碼根目錄下的Makefile進行的,首先通過include config.mak 將前述配置變量config.mak引入,這些配置將在整個編譯過程中生效。同時還會引入common.mak,主要是一些公共變量定義以及方法定義,像變量ALLFFLIBS = avcodec avdevice avfilter avformat avresample avutil postproc swscale swresample 就定義了所有的庫集合。另外,主Makefile還會引入library.maklibrary.mak的主要作用是編譯各個庫的規則,如libavformat
整個make的目標是如下語句,

all: $(AVPROGS)

而變量$(AVPROGS)則由下列語句獲得,

AVPROGS := $(AVPROGS-yes:%=%$(PROGSSUF)$(EXESUF))

變量$(AVPROGS-yes)的定義如下,

AVPROGS-$(CONFIG_FFMPEG)   += ffmpeg
AVPROGS-$(CONFIG_FFPLAY)   += ffplay
AVPROGS-$(CONFIG_FFPROBE)  += ffprobe
AVPROGS-$(CONFIG_FFSERVER) += ffserver

即配置程序的集合,make install的規則如下,

install: install-libs install-headers

完成lib庫的安裝以及頭文件的安裝。

總體來看,整個FFmpeg的編譯系統也比較龐大,但是條理十分清晰,爲了讓FFmpeg可以跨多個平臺進行編譯,Makefile系統封裝了很多的變量。通過配置文件,將不同平臺不同編譯選項的需求,統一用一套Makefile來完成,其跨平臺性得到了玲離盡致的體現。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章