FFmpeg的代碼結構和編譯系統

FFmpeg的目錄結構

目錄結構
  1. compact目錄下主要是頭文件,是為了兼容不同平臺或系統的數據類型定義,例如compat/msvcrt/snprintf.h和snprintf.c是兼容C99的snprintf()和vsnprintf()聲明及實現;

  2. libavcodec目錄下是編碼/解碼庫所在的代碼路徑;

  3. libavdevice目錄下是和一些特定設備相關的接口實現,比如sdl顯示設備,alsa音頻輸出設備,opengl圖形加速設備,v4l2視頻設備(比如攝像頭);

  4. libavfilter是一個類似DirecShow機制的幀處理函數集,通過graph-based機制,將不同處理功能的filter函數,鏈接(link)起來;

  5. libavformat是復用/解復用庫,一般用于多媒體文件的處理;

  6. libavresample顧名思義,是一個重采樣庫,比如音頻采樣率從44.1KHz到96KHz;

  7. libavutil是支撐FFmpeg的工具集,像fifo管理,內存管理,時間管理等;

  8. libpostproc是后期處理庫;

  9. libswresample是音頻重采樣,格式轉換和混合;功能和libavresample十分類似,是FFmpeg相對早期的實現方法;

  10. libswscale是顏色空間轉換和大小尺寸縮放的庫;

  11. cmdutils.c是用戶使用命令行進行ffmpeg操作時用到的工具;

  12. ffmpeg.c是ffmpeg工具的源碼;

  13. ffplay.c是一個基于FFmpeg庫文件的播放器實例;

  14. ffprobe.c是解析媒體文件格式信息的例子;

  15. ffserver.c是RTSP/HTTP流媒體服務器的源代碼;

  16. 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.mak,library.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來完成,其跨平臺性得到了玲離盡致的體現。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,763評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,238評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,823評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,604評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,339評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,713評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,712評論 3 445
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,893評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,448評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,201評論 3 357
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,397評論 1 372
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,944評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,631評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,033評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,321評論 1 293
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,128評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,347評論 2 377

推薦閱讀更多精彩內容

  • 下載FFmpeg源碼 首先登陸FFmpeg的官方網站https://ffmpeg.org/,打開下載地址:http...
    張俊峰0613閱讀 12,254評論 0 7
  • 1. 安裝編譯環境 2. 下載源碼 3. 配置選項 通過configure來配置需要編譯的ffmpeg庫 3.1 ...
    Weller0閱讀 1,380評論 2 4
  • 1.FFmepg編譯環境及結構 下載FFmepg FFmpeg配置選項介紹 下載gas-preprocessor....
    Jackey_song閱讀 2,279評論 2 2
  • 下面列舉我們需要用到的一些編譯參數 openssl --with-openssl mysql,pdo --wit...
    黑魔術師閱讀 1,382評論 0 0
  • 我二十一二歲的時候,所得到的情感大部分被性左右著,所以很多戀情都不長久。這一段時間大概是內心荒蕪到極點,病急亂投醫...
    妙鋒閱讀 118評論 0 0