日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FFMPEG详解(完整版)

發布時間:2023/12/31 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FFMPEG详解(完整版) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文出自:?http://3xin2yi.info/wwwroot/tech/doku.php/tech:multimedia:ffmpeg

轉自:FFMPEG詳解 [C.L's Tech Notes]

根據多處轉載的記錄,原文寫于2012年12月,原文出處已經訪問不到,這里做一個完整版的備份,供參考。

認識FFMPEG

FFMPEG堪稱自由軟件中最完備的一套多媒體支持庫,它幾乎實現了所有當下常見的數據封裝格式、多媒體傳輸協議以及音視頻編解碼器,堪稱多媒體業界的瑞士軍刀。因此,對于從事多媒體技術開發的工程師來說,深入研究FFMPEG成為一門必不可少的工作,可以這樣說,FFMPEG之于多媒體開發工程師的重要性正如kernel之于嵌入式系統工程師一般。

幾個小知識:

  • FFMPEG項目是由法國人Fabrice Bellard發起的,此人也是著名的CPU模擬器項目QEMU的發起者,同時還是圓周率算法紀錄的保持者。

  • FF是Fast Forward的意思,翻譯成中文是“快進”。

  • FFMPEG的LOGO是一個”Z字掃描”示意圖,Z字掃描用于將圖像的二維頻域數據一維化,同時保證了一維化的數據具備良好的統計特性,從而提高其后要進行的一維熵編碼的效率。

關于恥辱廳(Hall of Shame):FFMPEG大部分代碼遵循LGPL許可證,如果使用者對FFMpeg進行了修改,要求公布修改的源代碼;有少部分代碼遵循GPL許可證,要求使用者同時公開使用FFMpeg的軟件的源代碼。實際上,除去部分具備系統軟件開發能力的大型公司(Microsoft、Apple等)以及某些著名的音視頻技術提供商(Divx、Real等)提供的自有播放器之外,絕大部分第三方開發的播放器都離不開FFMpeg的支持,像Linux桌面環境中的開源播放器VLC、MPlayer,Windows下的KMPlayer、暴風影音以及Android下幾乎全部第三方播放器都是基于FFMPEG的。也有許多看似具備自主技術的播放器,其實也都不聲不響地使用了FFMPEG,這種行為被稱為“盜竊”,參與“盜竊”的公司則被請入恥辱廳,如于2009年上榜的國產播放器暴風影音、QQ影音。

關于FFMPEG的商業應用:與其他開源軟件不同的是,FFMPEG所觸及的多媒體編解碼算法中有相當一部分處于大量的專利涵蓋范圍之內,因此,在商業軟件中使用FFMPEG必須考慮可能造成的對專利所有者的權利侵犯,這一點在FFMPEG的官方網站也有所提及,所涉及的風險需使用者自行評估應對。

FFMPEG從功能上劃分為幾個模塊,分別為核心工具(libutils)、媒體格式(libavformat)、編解碼(libavcodec)、設備(libavdevice)和后處理(libavfilter, libswscale, libpostproc),分別負責提供公用的功能函數、實現多媒體文件的讀包和寫包、完成音視頻的編解碼、管理音視頻設備的操作以及進行音視頻后處理。

使用FFMPEG

這里指FFMPEG提供的命令行(CLI)工具ffmpeg,其使用方法如下(方括號表示可選項,花括號表示必選項目):

ffmpeg [global options] {[infile options]['-i' 'infile'] ...} {[outfile options] 'outfile' ...}

參數選項由三部分組成:可選的一組全局參數、一組或多組輸入文件參數、一組或多組輸出文件參數,其中,每組輸入文件參數以‘-i’為結束標記;每組輸出文件參數以輸出文件名為結束標記。

基本選項

能力集列表

  • -formats:列出支持的文件格式。

  • -codecs:列出支持的編解碼器。

  • -decoders:列出支持的解碼器。

  • -encoders:列出支持的編碼器。

  • -protocols:列出支持的協議。

  • -bsfs:列出支持的比特流過濾器。

  • -filters:列出支持的濾鏡。

  • -pix_fmts:列出支持的圖像采樣格式。

  • -sample_fmts:列出支持的聲音采樣格式。

常用輸入選項

  • -i filename:指定輸入文件名。

  • -f fmt:強制設定文件格式,需使用能力集列表中的名稱(缺省是根據擴展名選擇的)。

  • -ss hh:mm:ss[.xxx]:設定輸入文件的起始時間點,啟動后將跳轉到此時間點然后開始讀取數據。

對于輸入,以下選項通常是自動識別的,但也可以強制設定。

  • -c codec:指定解碼器,需使用能力集列表中的名稱。

  • -acodec codec:指定聲音的解碼器,需使用能力集列表中的名稱。

  • -vcodec codec:指定視頻的解碼器,需使用能力集列表中的名稱。

  • -b:v bitrate:設定視頻流的比特率,整數,單位bps。

  • -r fps:設定視頻流的幀率,整數,單位fps。

  • -s WxH : 設定視頻的畫面大小。也可以通過掛載畫面縮放濾鏡實現。

  • -pix_fmt format:設定視頻流的圖像格式(如RGB還是YUV)。

  • -ar sample rate:設定音頻流的采樣率,整數,單位Hz。

  • -ab bitrate:設定音頻流的比特率,整數,單位bps。

  • -ac channels:設置音頻流的聲道數目。

常用輸出選項

  • -f fmt:強制設定文件格式,需使用能力集列表中的名稱(缺省是根據擴展名選擇的)。

  • -c codec:指定編碼器,需使用能力集列表中的名稱(編碼器設定為”copy“表示不進行編解碼)。

  • -acodec codec:指定聲音的編碼器,需使用能力集列表中的名稱(編碼器設定為”copy“表示不進行編解碼)。

  • -vcodec codec:指定視頻的編碼器,需使用能力集列表中的名稱(編解碼器設定為”copy“表示不進行編解碼)。

  • -r fps:設定視頻編碼器的幀率,整數,單位fps。

  • -pix_fmt format:設置視頻編碼器使用的圖像格式(如RGB還是YUV)。

  • -ar sample rate:設定音頻編碼器的采樣率,整數,單位Hz。

  • -b bitrate:設定音視頻編碼器輸出的比特率,整數,單位bps。

  • -ab bitrate:設定音頻編碼器輸出的比特率,整數,單位bps。

  • -ac channels:設置音頻編碼器的聲道數目。

  • -an 忽略任何音頻流。

  • -vn 忽略任何視頻流。

  • -t hh:mm:ss[.xxx]:設定輸出文件的時間長度。

  • -to hh:mm:ss[.xxx]:如果沒有設定輸出文件的時間長度的畫可以設定終止時間點。

流標識

FFMPEG的某些選項可以對一個特定的媒體流起作用,這種情況下需要在選項后面增加一個流標識。流標識允許以下幾種格式:

  • 流序號。譬如“:1”表示第二個流。

  • 流類型。譬如“:a“表示音頻流,流類型可以和流序號合并使用,譬如“:a:1”表示第二個音頻流。

  • 節目。節目和流序號可以合并使用。

  • 流標識。流標識是一個內部標識號。

假如要設定第二個音頻流為copy,則需要指定-codec:a:1 copy

音頻選項

  • -aframes:等價于frames:a,輸出選項,用于指定輸出的音頻幀數目。

  • -aq:等價于q:a,老版本為qscale:a,用于設定音頻質量。

  • -atag:等價于tag:a,用于設定音頻流的標簽。

  • -af:等價于filter:a,用于設定一個聲音的后處理過濾鏈,其參數為一個描述聲音后處理鏈的字符串。

視頻選項

  • -vframes:等價于frames:v,輸出選項,用于指定輸出的視頻幀數目。

  • -aspect:設置寬高比,如4:3、16:9、1.3333、1.7777等。

  • -bits_per_raw_sample:設置每個像素點的比特數。

  • -vstats:產生video統計信息。

  • -vf:等價于filter:v,用于設定一個圖像的后處理過濾鏈,其參數為一個描述圖像后處理鏈的字符串。

  • -vtag:等價于tag:v,用于設定視頻流的標簽。

  • -force_fps:強制設定視頻幀率。

  • -force_key_frames:顯式控制關鍵幀的插入,參數為字符串,可以是一個時間戳,也可以是一個“expr:”前綴的表達式。如“-force_key_frames 0:05:00”、“-force_key_frames expr:gte(t,n_forced*5)”

濾鏡選項

高級選項

  • -re:要求按照既定速率處理輸入數據,這個速率即是輸入文件的幀率。

  • -map:指定輸出文件的流映射關系。例如 “-map 1:0 -map 1:1”要求將第二個輸入文件的第一個流和第二個流寫入到輸出文件。如果沒有-map選項,ffmpeg采用缺省的映射關系。

用例

1、將一個老式的avi文件轉成mp4

ffmpeg -i final.avi -acodec copy -vcodec copy final.mp4

2、從一個視頻文件中抽取一幀圖像:

ffmpeg -y -i test.mp4 -ss 00:03:22.000 -vframes 1 -an test.jpg

3

ffmpeg -i final.avi -vf scale=640:640 square.avi

4、使用alsa接口錄制一段音頻存放到某個wav文件中

ffmpeg -f alsa -i hw:0 -t 100 alsaout.wav

5、使用alsa接口搭建一個個人網絡電臺

ffmpeg -f alsa -i default -acodec aac -strict -2 -b:a 128k -r 44100 /var/www/data/main.m3u8

6、將一個mp4文件的音視頻流實時轉碼之后發送給某個遠程設備,遠程設備可以通過http獲取的sdp文件來接收rtp媒體數據。

ffmpeg -re -i example.mp4 -acodec copy -vcodec libx264 -s 480x270 -map 0:0 -f rtp rtp://10.131.202.62:1234 -map 0:1 -f rtp rtp://10.131.202.62:1238 > /var/www/live.sdp

編譯和裁剪

FFMpeg與大部分GNU軟件的編譯方式類似,是通過configure腳本來實現編譯前定制的。這種途徑允許用戶在編譯前對軟件進行裁剪,同時通過對宿主系統和目標系統的自動檢測來篩選參與編譯的模塊并為其設定合適的配置。但是,FFMpeg的configure腳本并非如通常的GNU軟件一樣通過配置工具自動生成,而是完全由人工編寫的。configure腳本生成的config.mak和config.h分別在Makefile和源代碼的層次上實現編譯的控制。

通過運行“./configure –help”可以了解到腳本支持的參數,這些參數大體分為下面幾類:

  • 標準選項——GNU軟件例行配置項目如安裝路徑等。例:–prefix=…,……

  • 列出當前源代碼支持的能力集,如編解碼器,解復用器,輸入輸出設備,文件系統等。例:–list-decoders,–list-encoders,……

  • 授權選項:–enable-version3,–enable-gpl,–enable-nofree。代碼的缺省授權是LGPL v2,如果要使用以LGPL v3、GPL授權的模塊或者某些不遵循自有軟件授權協議的模塊,必須在運行configure時顯式使能相應的選項。

  • 編譯、鏈接選項。例:–disable-static,–enable-shared,…… 缺省配置是生成靜態庫而不生成動態庫,如果希望禁止靜態庫、生成動態庫都需要顯式指定。

  • 可執行程序控制選項,決定是否生成ffmpeg、ffplay、ffprobe和ffserver。

  • 模塊控制選項,篩選參與編譯的模塊,包括整個庫的篩選,例如:–disable-avdevice;一組模塊的篩選,例如:–disable-decoders,單個模塊的篩選,如:–disable-decoder=… 等。

  • 專家級選項,允許開發者進行深度定制,如交叉編譯環境的配置、自定義編譯器參數的設定、指令級優化、debug控制等。

對于–disable、–enable類的控制選項,如果以–disable為前綴,則缺省是enable的,反之亦然。

總之,無論從商業角度還是技術角度出發,使用configure腳本對FFMpeg進行裁剪是最安全的方式,只有針對于某些configure無法滿足的定制要求,才需要考慮修改configure腳本——甚至修改configure生成的配置文件。

以下是一個配置實例,實現運行與Android系統中的ffmpeg庫的編譯:

./configure --prefix=. --cross-prefix=$NDK_TOOLCHAIN_PREFIX --enable-cross-compile --arch=arm --target-os=linux --cpu=cortex-a8 \--disable-static --enable-shared --enable-pic --disable-ffmpeg --disable-ffplay --disable-ffserver --disable-ffprobe \--extra-cflags="-I$NDK_PLATFORM/usr/include" \--extra-ldflags="-nostdlib -Wl,-T,$NDK_PREBUILT/arm-linux-androideabi/lib/ldscripts/armelf_linux_eabi.x \-L$NDK_PLATFORM/usr/lib \$NDK_PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/crtbegin.o $NDK_PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/crtend.o \-lc -lm -ldl"

缺省的編譯會生成4個可執行文件和9個庫:可執行文件包括用于轉碼的ffmpeg、用于獲取媒體信息的ffprobe、用于播放媒體的ffplay和用于推送媒體流的ffserver;庫包括avutil、avformat、avcodec、avfilter、avdevice、swresample、swscale、postproc及avresample,其中,核心庫有5個,分別為基礎庫avutil、文件格式及協議庫avformat、編解碼庫avcodec、輸入輸出設備庫avdevice和過濾器avfilter。

深入FFMPEG

示例程序

解碼

#include <stdio.h> #include <string.h> #include <stdlib.h>#include <sys/time.h>#include "libavutil/avstring.h" #include "libavformat/avformat.h" #include "libavdevice/avdevice.h" #include "libavutil/opt.h" #include "libswscale/swscale.h"#define DECODED_AUDIO_BUFFER_SIZE 192000struct options {int streamId;int frames;int nodec;int bplay;int thread_count;int64_t lstart;char finput[256];char foutput1[256];char foutput2[256]; };int parse_options(struct options *opts, int argc, char** argv) {int optidx;char *optstr;if (argc < 2) return -1;opts->streamId = -1;opts->lstart = -1;opts->frames = -1;opts->foutput1[0] = 0;opts->foutput2[0] = 0;opts->nodec = 0;opts->bplay = 0;opts->thread_count = 0;strcpy(opts->finput, argv[1]);optidx = 2;while (optidx < argc){optstr = argv[optidx++];if (*optstr++ != '-') return -1;switch (*optstr++){case 's': //< stream idopts->streamId = atoi(optstr);break;case 'f': //< framesopts->frames = atoi(optstr);break;case 'k': //< skippedopts->lstart = atoll(optstr);break;case 'o': //< outputstrcpy(opts->foutput1, optstr);strcat(opts->foutput1, ".mpg");strcpy(opts->foutput2, optstr);strcat(opts->foutput2, ".raw");break;case 'n': //decoding and output optionsif (strcmp("dec", optstr) == 0)opts->nodec = 1;break;case 'p':opts->bplay = 1;break;case 't':opts->thread_count = atoi(optstr);break;default:return -1;}}return 0; }void show_help(char* program) {printf("Simple FFMPEG test program\n");printf("Usage: %s inputfile [-sstreamid [-fframes] [-kskipped] [-ooutput_filename(without extension)] [-ndec] [-p] [-tthread_count]]\n",program);return; }static void log_callback(void* ptr, int level, const char* fmt, va_list vl) {vfprintf(stdout, fmt, vl); }/* * audio renderer code (oss) */ #include <sys/ioctl.h> #include <unistd.h> #include <fcntl.h> #include <sys/soundcard.h>#define OSS_DEVICE "/dev/dsp0"struct audio_dsp {int audio_fd;int channels;int format;int speed; }; int map_formats(enum AVSampleFormat format) {switch(format){case AV_SAMPLE_FMT_U8:return AFMT_U8;case AV_SAMPLE_FMT_S16:return AFMT_S16_LE;default:return AFMT_U8; } } int set_audio(struct audio_dsp* dsp) {if (dsp->audio_fd == -1){printf("Invalid audio dsp id!\n");return -1;} if (-1 == ioctl(dsp->audio_fd, SNDCTL_DSP_SETFMT, &dsp->format)){printf("Failed to set dsp format!\n");return -1;}if (-1 == ioctl(dsp->audio_fd, SNDCTL_DSP_CHANNELS, &dsp->channels)){printf("Failed to set dsp format!\n");return -1;}if (-1 == ioctl(dsp->audio_fd, SNDCTL_DSP_SPEED, &dsp->speed)){printf("Failed to set dsp format!\n");return -1;} return 0; }int play_pcm(struct audio_dsp* dsp, unsigned char *buf, int size) {if (dsp->audio_fd == -1){printf("Invalid audio dsp id!\n");return -1;}if (-1 == write(dsp->audio_fd, buf, size)){printf("Failed to write audio dsp!\n");return -1;}return 0; } /* audio renderer code end *//* video renderer code*/ #include <linux/fb.h> #include <sys/mman.h>#define FB_DEVICE "/dev/fb0"enum pic_format {eYUV_420_Planer, }; struct video_fb {int video_fd;struct fb_var_screeninfo vinfo;struct fb_fix_screeninfo finfo;unsigned char *fbp;AVFrame *frameRGB;struct {int x;int y;} video_pos; };int open_video(struct video_fb *fb, int x, int y) {int screensize;fb->video_fd = open(FB_DEVICE, O_WRONLY);if (fb->video_fd == -1) return -1;if (ioctl(fb->video_fd, FBIOGET_FSCREENINFO, &fb->finfo)) return -2;if (ioctl(fb->video_fd, FBIOGET_VSCREENINFO, &fb->vinfo)) return -2;printf("video device: resolution %dx%d, %dbpp\n", fb->vinfo.xres, fb->vinfo.yres, fb->vinfo.bits_per_pixel);screensize = fb->vinfo.xres * fb->vinfo.yres * fb->vinfo.bits_per_pixel / 8;fb->fbp = (unsigned char *) mmap(0, screensize, PROT_READ|PROT_WRITE, MAP_SHARED, fb->video_fd, 0);if (fb->fbp == -1) return -3;if (x >= fb->vinfo.xres || y >= fb->vinfo.yres){return -4;}else{fb->video_pos.x = x;fb->video_pos.y = y;}fb->frameRGB = avcodec_alloc_frame();if (!fb->frameRGB) return -5;return 0; }/* only 420P supported now */ int show_picture(struct video_fb *fb, AVFrame *frame, int width, int height, enum pic_format format) {struct SwsContext *sws;int i;unsigned char *dest;unsigned char *src;if (fb->video_fd == -1) return -1;if ((fb->video_pos.x >= fb->vinfo.xres) || (fb->video_pos.y >= fb->vinfo.yres)) return -2;if (fb->video_pos.x + width > fb->vinfo.xres){width = fb->vinfo.xres - fb->video_pos.x;}if (fb->video_pos.y + height > fb->vinfo.yres){height = fb->vinfo.yres - fb->video_pos.y;}if (format == PIX_FMT_YUV420P){sws = sws_getContext(width, height, format, width, height, PIX_FMT_RGB32, SWS_FAST_BILINEAR, NULL, NULL, NULL);if (sws == 0){return -3;}if (sws_scale(sws, frame->data, frame->linesize, 0, height, fb->frameRGB->data, fb->frameRGB->linesize)){return -3;}dest = fb->fbp + (fb->video_pos.x+fb->vinfo.xoffset) * (fb->vinfo.bits_per_pixel/8) +(fb->video_pos.y+fb->vinfo.yoffset) * fb->finfo.line_length;for (i = 0; i < height; i++){memcpy(dest, src, width*4);src += fb->frameRGB->linesize[0];dest += fb->finfo.line_length;}}return 0; }void close_video(struct video_fb *fb) {if (fb->video_fd != -1) {munmap(fb->fbp, fb->vinfo.xres * fb->vinfo.yres * fb->vinfo.bits_per_pixel / 8);close(fb->video_fd);fb->video_fd = -1;} } /* video renderer code end */int main(int argc, char **argv) {AVFormatContext* pCtx = 0;AVCodecContext *pCodecCtx = 0;AVCodec *pCodec = 0;AVPacket packet;AVFrame *pFrame = 0;FILE *fpo1 = NULL;FILE *fpo2 = NULL;int nframe;int err;int got_picture;int picwidth, picheight, linesize;unsigned char *pBuf;int i;int64_t timestamp;struct options opt;int usefo = 0;struct audio_dsp dsp;struct video_fb fb;int dusecs;float usecs1 = 0;float usecs2 = 0;struct timeval elapsed1, elapsed2;int decoded = 0;av_register_all();av_log_set_callback(log_callback);av_log_set_level(50);if (parse_options(&opt, argc, argv) < 0 || (strlen(opt.finput) == 0)){show_help(argv[0]);return 0;}err = avformat_open_input(&pCtx, opt.finput, 0, 0);if (err < 0){printf("\n->(avformat_open_input)\tERROR:\t%d\n", err);goto fail;}err = avformat_find_stream_info(pCtx, 0);if (err < 0){printf("\n->(avformat_find_stream_info)\tERROR:\t%d\n", err);goto fail;}if (opt.streamId < 0){av_dump_format(pCtx, 0, pCtx->filename, 0);goto fail;}else{printf("\n extra data in Stream %d (%dB):", opt.streamId, pCtx->streams[opt.streamId]->codec->extradata_size);for (i = 0; i < pCtx->streams[opt.streamId]->codec->extradata_size; i++){if (i%16 == 0) printf("\n");printf("%2x ", pCtx->streams[opt.streamId]->codec->extradata[i]);}}/* try opening output files */if (strlen(opt.foutput1) && strlen(opt.foutput2)){fpo1 = fopen(opt.foutput1, "wb");fpo2 = fopen(opt.foutput2, "wb");if (!fpo1 || !fpo2){printf("\n->error opening output files\n");goto fail;}usefo = 1;}else{usefo = 0;}if (opt.streamId >= pCtx->nb_streams){printf("\n->StreamId\tERROR\n");goto fail;}if (opt.lstart > 0){err = av_seek_frame(pCtx, opt.streamId, opt.lstart, AVSEEK_FLAG_ANY);if (err < 0){printf("\n->(av_seek_frame)\tERROR:\t%d\n", err);goto fail;}}/* for decoder configuration */if (!opt.nodec){/* prepare codec */pCodecCtx = pCtx->streams[opt.streamId]->codec;if (opt.thread_count <= 16 && opt.thread_count > 0 ){pCodecCtx->thread_count = opt.thread_count;pCodecCtx->thread_type = FF_THREAD_FRAME;}pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (!pCodec){printf("\n->can not find codec!\n");goto fail;}err = avcodec_open2(pCodecCtx, pCodec, 0);if (err < 0){printf("\n->(avcodec_open)\tERROR:\t%d\n", err);goto fail;}pFrame = avcodec_alloc_frame(); /* prepare device */if (opt.bplay){/* audio devices */dsp.audio_fd = open(OSS_DEVICE, O_WRONLY);if (dsp.audio_fd == -1){printf("\n-> can not open audio device\n");goto fail;}dsp.channels = pCodecCtx->channels;dsp.speed = pCodecCtx->sample_rate;dsp.format = map_formats(pCodecCtx->sample_fmt);if (set_audio(&dsp) < 0){printf("\n-> can not set audio device\n");goto fail;}/* video devices */if (open_video(&fb, 0, 0) != 0){printf("\n-> can not open video device\n");goto fail;}}}nframe = 0;while(nframe < opt.frames || opt.frames == -1){gettimeofday(&elapsed1, NULL);err = av_read_frame(pCtx, &packet);if (err < 0){printf("\n->(av_read_frame)\tERROR:\t%d\n", err);break;}gettimeofday(&elapsed2, NULL);dusecs = (elapsed2.tv_sec - elapsed1.tv_sec)*1000000 + (elapsed2.tv_usec - elapsed1.tv_usec);usecs2 += dusecs; timestamp = av_rescale_q(packet.dts, pCtx->streams[packet.stream_index]->time_base, (AVRational){1, AV_TIME_BASE});printf("\nFrame No %5d stream#%d\tsize %6dB, timestamp:%6lld, dts:%6lld, pts:%6lld, ", nframe++, packet.stream_index, packet.size, timestamp, packet.dts, packet.pts);if (packet.stream_index == opt.streamId){ #if 0 for (i = 0; i < 16; /*packet.size;*/ i++){if (i%16 == 0) printf("\n pktdata: ");printf("%2x ", packet.data[i]);}printf("\n"); #endif if (usefo){fwrite(packet.data, packet.size, 1, fpo1);fflush(fpo1);}if (pCtx->streams[opt.streamId]->codec->codec_type == AVMEDIA_TYPE_VIDEO && !opt.nodec){picheight = pCtx->streams[opt.streamId]->codec->height;picwidth = pCtx->streams[opt.streamId]->codec->width;gettimeofday(&elapsed1, NULL); avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);decoded++;gettimeofday(&elapsed2, NULL);dusecs = (elapsed2.tv_sec - elapsed1.tv_sec)*1000000 + (elapsed2.tv_usec - elapsed1.tv_usec);usecs1 += dusecs;if (got_picture){printf("[Video: type %d, ref %d, pts %lld, pkt_pts %lld, pkt_dts %lld]", pFrame->pict_type, pFrame->reference, pFrame->pts, pFrame->pkt_pts, pFrame->pkt_dts);if (pCtx->streams[opt.streamId]->codec->pix_fmt == PIX_FMT_YUV420P){if (usefo){linesize = pFrame->linesize[0];pBuf = pFrame->data[0];for (i = 0; i < picheight; i++){fwrite(pBuf, picwidth, 1, fpo2);pBuf += linesize;}linesize = pFrame->linesize[1];pBuf = pFrame->data[1];for (i = 0; i < picheight/2; i++){fwrite(pBuf, picwidth/2, 1, fpo2);pBuf += linesize;} linesize = pFrame->linesize[2];pBuf = pFrame->data[2];for (i = 0; i < picheight/2; i++){fwrite(pBuf, picwidth/2, 1, fpo2);pBuf += linesize;} fflush(fpo2);} if (opt.bplay){/* show picture */show_picture(&fb, pFrame, picheight, picwidth, PIX_FMT_YUV420P);}}}av_free_packet(&packet);}else if (pCtx->streams[opt.streamId]->codec->codec_type == AVMEDIA_TYPE_AUDIO && !opt.nodec){int got;gettimeofday(&elapsed1, NULL);avcodec_decode_audio4(pCodecCtx, pFrame, &got, &packet);decoded++;gettimeofday(&elapsed2, NULL);dusecs = (elapsed2.tv_sec - elapsed1.tv_sec)*1000000 + (elapsed2.tv_usec - elapsed1.tv_usec);usecs1 += dusecs;if (got){printf("[Audio: %5dB raw data, decoding time: %d]", pFrame->linesize[0], dusecs);if (usefo){fwrite(pFrame->data[0], pFrame->linesize[0], 1, fpo2);fflush(fpo2);}if (opt.bplay){play_pcm(&dsp, pFrame->data[0], pFrame->linesize[0]);}}}}} if (!opt.nodec && pCodecCtx){avcodec_close(pCodecCtx);}printf("\n%d frames parsed, average %.2f us per frame\n", nframe, usecs2/nframe);printf("%d frames decoded, average %.2f us per frame\n", decoded, usecs1/decoded);fail:if (pCtx){avformat_close_input(&pCtx);}if (fpo1){fclose(fpo1);}if (fpo2){fclose(fpo2);}if (!pFrame){av_free(pFrame);}if (!usefo && (dsp.audio_fd != -1)){close(dsp.audio_fd);}if (!usefo && (fb.video_fd != -1)){close_video(&fb);}return 0; }

這一小段代碼可以實現的功能包括:

  • 打開一個多媒體文件并獲取基本的媒體信息。

  • 獲取編碼器句柄。

  • 根據給定的時間標簽進行一個跳轉。

  • 讀取數據幀。

  • 解碼音頻幀或者視頻幀。

  • 關閉多媒體文件。

這些功能足以支持一個功能強大的多媒體播放器,因為最復雜的解復用、解碼、數據分析過程已經在FFMpeg內部實現了,需要關注的僅剩同步問題。

用戶接口

數據結構

基本概念

編解碼器、數據幀、媒體流和容器是數字媒體處理系統的四個基本概念。

首先需要統一術語:

  • 容器/文件(Conainer/File):即特定格式的多媒體文件。

  • 媒體流(Stream):指時間軸上的一段連續數據,如一段聲音數據,一段視頻數據或一段字幕數據,可以是壓縮的,也可以是非壓縮的,壓縮的數據需要關聯特定的編解碼器。

  • 數據幀/數據包(Frame/Packet):通常,一個媒體流由大量的數據幀組成,對于壓縮數據,幀對應著編解碼器的最小處理單元。通常,分屬于不同媒體流的數據幀交錯復用于容器之中,參見交錯。

  • 編解碼器:編解碼器以幀為單位實現壓縮數據和原始數據之間的相互轉換。

在FFMPEG中,使用AVFormatContext、AVStream、AVCodecContext、AVCodec及AVPacket等結構來抽象這些基本要素,它們的關系如下圖所示:?

AVCodecContext

這是一個描述編解碼器上下文的數據結構,包含了眾多編解碼器需要的參數信息,如下列出了部分比較重要的域:

typedef struct AVCodecContext {....../*** some codecs need / can use extradata like Huffman tables.* mjpeg: Huffman tables* rv10: additional flags* mpeg4: global headers (they can be in the bitstream or here)* The allocated memory should be FF_INPUT_BUFFER_PADDING_SIZE bytes larger* than extradata_size to avoid prolems if it is read with the bitstream reader.* The bytewise contents of extradata must not depend on the architecture or CPU endianness.* - encoding: Set/allocated/freed by libavcodec.* - decoding: Set/allocated/freed by user.*/uint8_t *extradata;int extradata_size;/*** This is the fundamental unit of time (in seconds) in terms* of which frame timestamps are represented. For fixed-fps content,* timebase should be 1/framerate and timestamp increments should be* identically 1.* - encoding: MUST be set by user.* - decoding: Set by libavcodec.*/AVRational time_base;/* video only *//*** picture width / height.* - encoding: MUST be set by user.* - decoding: Set by libavcodec.* Note: For compatibility it is possible to set this instead of* coded_width/height before decoding.*/int width, height;....../* audio only */int sample_rate; ///< samples per secondint channels; ///< number of audio channels/*** audio sample format* - encoding: Set by user.* - decoding: Set by libavcodec.*/enum SampleFormat sample_fmt; ///< sample format/* The following data should not be initialized. *//*** Samples per packet, initialized when calling 'init'.*/int frame_size;int frame_number; ///< audio or video frame number......char codec_name[32];enum AVMediaType codec_type; /* see AVMEDIA_TYPE_xxx */enum CodecID codec_id; /* see CODEC_ID_xxx *//*** fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').* This is used to work around some encoder bugs.* A demuxer should set this to what is stored in the field used to identify the codec.* If there are multiple such fields in a container then the demuxer should choose the one* which maximizes the information about the used codec.* If the codec tag field in a container is larger then 32 bits then the demuxer should* remap the longer ID to 32 bits with a table or other structure. Alternatively a new* extra_codec_tag + size could be added but for this a clear advantage must be demonstrated* first.* - encoding: Set by user, if not then the default based on codec_id will be used.* - decoding: Set by user, will be converted to uppercase by libavcodec during init.*/unsigned int codec_tag; ....../*** Size of the frame reordering buffer in the decoder.* For MPEG-2 it is 1 IPB or 0 low delay IP.* - encoding: Set by libavcodec.* - decoding: Set by libavcodec.*/int has_b_frames;/*** number of bytes per packet if constant and known or 0* Used by some WAV based audio codecs.*/int block_align;....../*** bits per sample/pixel from the demuxer (needed for huffyuv).* - encoding: Set by libavcodec.* - decoding: Set by user.*/int bits_per_coded_sample; ......} AVCodecContext;

如果是單純使用libavcodec,這部分信息需要調用者進行初始化;如果是使用整個FFMPEG庫,這部分信息在調用avformat_open_input和avformat_find_stream_info的過程中根據文件的頭信息及媒體流內的頭部信息完成初始化。其中幾個主要域的釋義如下:

  • extradata/extradata_size:這個buffer中存放了解碼器可能會用到的額外信息,在av_read_frame中填充。一般來說,首先,某種具體格式的demuxer在讀取格式頭信息的時候會填充extradata,其次,如果demuxer沒有做這個事情,比如可能在頭部壓根兒就沒有相關的編解碼信息,則相應的parser會繼續從已經解復用出來的媒體流中繼續尋找。在沒有找到任何額外信息的情況下,這個buffer指針為空。

  • time_base:編解碼器的時間基準,實際上就是視頻的幀率(或場率)。

  • width/height:視頻的寬和高。

  • sample_rate/channels:音頻的采樣率和信道數目。

  • sample_fmt: 音頻的原始采樣格式。

  • codec_name/codec_type/codec_id/codec_tag:編解碼器的信息。

  • AVStream

    該結構體描述一個媒體流,定義如下:

    typedef struct AVStream {int index; /**< stream index in AVFormatContext */int id; /**< format-specific stream ID */AVCodecContext *codec; /**< codec context *//*** Real base framerate of the stream.* This is the lowest framerate with which all timestamps can be* represented accurately (it is the least common multiple of all* framerates in the stream). Note, this value is just a guess!* For example, if the time base is 1/90000 and all frames have either* approximately 3600 or 1800 timer ticks, then r_frame_rate will be 50/1.*/AVRational r_frame_rate;....../*** This is the fundamental unit of time (in seconds) in terms* of which frame timestamps are represented. For fixed-fps content,* time base should be 1/framerate and timestamp increments should be 1.*/AVRational time_base;....../*** Decoding: pts of the first frame of the stream, in stream time base.* Only set this if you are absolutely 100% sure that the value you set* it to really is the pts of the first frame.* This may be undefined (AV_NOPTS_VALUE).* @note The ASF header does NOT contain a correct start_time the ASF* demuxer must NOT set this.*/int64_t start_time;/*** Decoding: duration of the stream, in stream time base.* If a source file does not specify a duration, but does specify* a bitrate, this value will be estimated from bitrate and file size.*/int64_t duration;#if LIBAVFORMAT_VERSION_INT < (53<<16)char language[4]; /** ISO 639-2/B 3-letter language code (empty string if undefined) */ #endif/* av_read_frame() support */enum AVStreamParseType need_parsing;struct AVCodecParserContext *parser;....../* av_seek_frame() support */AVIndexEntry *index_entries; /**< Only used if the format does notsupport seeking natively. */int nb_index_entries;unsigned int index_entries_allocated_size;int64_t nb_frames; ///< number of frames in this stream if known or 0....../*** Average framerate*/AVRational avg_frame_rate;...... } AVStream;

    主要域的釋義如下,其中大部分域的值可以由avformat_open_input根據文件頭的信息確定,缺少的信息需要通過調用avformat_find_stream_info讀幀及軟解碼進一步獲取:

  • index/id:index對應流的索引,這個數字是自動生成的,根據index可以從AVFormatContext::streams表中索引到該流;而id則是流的標識,依賴于具體的容器格式。比如對于MPEG TS格式,id就是pid。

  • time_base:流的時間基準,是一個實數,該流中媒體數據的pts和dts都將以這個時間基準為粒度。通常,使用av_rescale/av_rescale_q可以實現不同時間基準的轉換。

  • start_time:流的起始時間,以流的時間基準為單位,通常是該流中第一個幀的pts。

  • duration:流的總時間,以流的時間基準為單位。

  • need_parsing:對該流parsing過程的控制域。

  • nb_frames:流內的幀數目。

  • r_frame_rate/framerate/avg_frame_rate:幀率相關。

  • codec:指向該流對應的AVCodecContext結構,調用avformat_open_input時生成。

  • parser:指向該流對應的AVCodecParserContext結構,調用avformat_find_stream_info時生成。。

  • AVFormatContext

    這個結構體描述了一個媒體文件或媒體流的構成和基本信息,定義如下:

    typedef struct AVFormatContext {const AVClass *av_class; /**< Set by avformat_alloc_context. *//* Can only be iformat or oformat, not both at the same time. */struct AVInputFormat *iformat;struct AVOutputFormat *oformat;void *priv_data;ByteIOContext *pb;unsigned int nb_streams;AVStream *streams[MAX_STREAMS];char filename[1024]; /**< input or output filename *//* stream info */int64_t timestamp; #if LIBAVFORMAT_VERSION_INT < (53<<16)char title[512];char author[512];char copyright[512];char comment[512];char album[512];int year; /**< ID3 year, 0 if none */int track; /**< track number, 0 if none */char genre[32]; /**< ID3 genre */ #endifint ctx_flags; /**< Format-specific flags, see AVFMTCTX_xx *//* private data for pts handling (do not modify directly). *//** This buffer is only needed when packets were already buffered butnot decoded, for example to get the codec parameters in MPEGstreams. */struct AVPacketList *packet_buffer;/** Decoding: position of the first frame of the component, inAV_TIME_BASE fractional seconds. NEVER set this value directly:It is deduced from the AVStream values. */int64_t start_time;/** Decoding: duration of the stream, in AV_TIME_BASE fractionalseconds. Only set this value if you know none of the individual streamdurations and also dont set any of them. This is deduced from theAVStream values if not set. */int64_t duration;/** decoding: total file size, 0 if unknown */int64_t file_size;/** Decoding: total stream bitrate in bit/s, 0 if notavailable. Never set it directly if the file_size and theduration are known as FFmpeg can compute it automatically. */int bit_rate;/* av_read_frame() support */AVStream *cur_st; #if LIBAVFORMAT_VERSION_INT < (53<<16)const uint8_t *cur_ptr_deprecated;int cur_len_deprecated;AVPacket cur_pkt_deprecated; #endif/* av_seek_frame() support */int64_t data_offset; /** offset of the first packet */int index_built;int mux_rate;unsigned int packet_size;int preload;int max_delay;#define AVFMT_NOOUTPUTLOOP -1 #define AVFMT_INFINITEOUTPUTLOOP 0/** number of times to loop output in formats that support it */int loop_output;int flags; #define AVFMT_FLAG_GENPTS 0x0001 ///< Generate missing pts even if it requires parsing future frames. #define AVFMT_FLAG_IGNIDX 0x0002 ///< Ignore index. #define AVFMT_FLAG_NONBLOCK 0x0004 ///< Do not block when reading packets from input. #define AVFMT_FLAG_IGNDTS 0x0008 ///< Ignore DTS on frames that contain both DTS & PTS #define AVFMT_FLAG_NOFILLIN 0x0010 ///< Do not infer any values from other values, just return what is stored in the container #define AVFMT_FLAG_NOPARSE 0x0020 ///< Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled #define AVFMT_FLAG_RTP_HINT 0x0040 ///< Add RTP hinting to the output fileint loop_input;/** decoding: size of data to probe; encoding: unused. */unsigned int probesize;/*** Maximum time (in AV_TIME_BASE units) during which the input should* be analyzed in avformat_find_stream_info().*/int max_analyze_duration;const uint8_t *key;int keylen;unsigned int nb_programs;AVProgram **programs;/*** Forced video codec_id.* Demuxing: Set by user.*/enum CodecID video_codec_id;/*** Forced audio codec_id.* Demuxing: Set by user.*/enum CodecID audio_codec_id;/*** Forced subtitle codec_id.* Demuxing: Set by user.*/enum CodecID subtitle_codec_id;/*** Maximum amount of memory in bytes to use for the index of each stream.* If the index exceeds this size, entries will be discarded as* needed to maintain a smaller size. This can lead to slower or less* accurate seeking (depends on demuxer).* Demuxers for which a full in-memory index is mandatory will ignore* this.* muxing : unused* demuxing: set by user*/unsigned int max_index_size;/*** Maximum amount of memory in bytes to use for buffering frames* obtained from realtime capture devices.*/unsigned int max_picture_buffer;unsigned int nb_chapters;AVChapter **chapters;/*** Flags to enable debugging.*/int debug; #define FF_FDEBUG_TS 0x0001/*** Raw packets from the demuxer, prior to parsing and decoding.* This buffer is used for buffering packets until the codec can* be identified, as parsing cannot be done without knowing the* codec.*/struct AVPacketList *raw_packet_buffer;struct AVPacketList *raw_packet_buffer_end;struct AVPacketList *packet_buffer_end;AVMetadata *metadata;/*** Remaining size available for raw_packet_buffer, in bytes.* NOT PART OF PUBLIC API*/ #define RAW_PACKET_BUFFER_SIZE 2500000int raw_packet_buffer_remaining_size;/*** Start time of the stream in real world time, in microseconds* since the unix epoch (00:00 1st January 1970). That is, pts=0* in the stream was captured at this real world time.* - encoding: Set by user.* - decoding: Unused.*/int64_t start_time_realtime; } AVFormatContext;

    這是FFMpeg中最為基本的一個結構,是其他所有結構的根,是一個多媒體文件或流的根本抽象。其中:

    • nb_streams和streams所表示的AVStream結構指針數組包含了所有內嵌媒體流的描述;

    • iformat和oformat指向對應的demuxer和muxer指針;

    • pb則指向一個控制底層數據讀寫的ByteIOContext結構。

    • start_time和duration是從streams數組的各個AVStream中推斷出的多媒體文件的起始時間和長度,以微妙為單位。

    通常,這個結構由avformat_open_input在內部創建并以缺省值初始化部分成員。但是,如果調用者希望自己創建該結構,則需要顯式為該結構的一些成員置缺省值——如果沒有缺省值的話,會導致之后的動作產生異常。以下成員需要被關注:

    • probesize

    • mux_rate

    • packet_size

    • flags

    • max_analyze_duration

    • key

    • max_index_size

    • max_picture_buffer

    • max_delay

    AVPacket

    AVPacket定義在avcodec.h中,如下:

    typedef struct AVPacket {/*** Presentation timestamp in AVStream->time_base units; the time at which* the decompressed packet will be presented to the user.* Can be AV_NOPTS_VALUE if it is not stored in the file.* pts MUST be larger or equal to dts as presentation cannot happen before* decompression, unless one wants to view hex dumps. Some formats misuse* the terms dts and pts/cts to mean something different. Such timestamps* must be converted to true pts/dts before they are stored in AVPacket.*/int64_t pts;/*** Decompression timestamp in AVStream->time_base units; the time at which* the packet is decompressed.* Can be AV_NOPTS_VALUE if it is not stored in the file.*/int64_t dts;uint8_t *data;int size;int stream_index;int flags;/*** Duration of this packet in AVStream->time_base units, 0 if unknown.* Equals next_pts - this_pts in presentation order.*/int duration;void (*destruct)(struct AVPacket *);void *priv;int64_t pos; ///< byte position in stream, -1 if unknown/*** Time difference in AVStream->time_base units from the pts of this* packet to the point at which the output from the decoder has converged* independent from the availability of previous frames. That is, the* frames are virtually identical no matter if decoding started from* the very first frame or from this keyframe.* Is AV_NOPTS_VALUE if unknown.* This field is not the display duration of the current packet.** The purpose of this field is to allow seeking in streams that have no* keyframes in the conventional sense. It corresponds to the* recovery point SEI in H.264 and match_time_delta in NUT. It is also* essential for some types of subtitle streams to ensure that all* subtitles are correctly displayed after seeking.*/int64_t convergence_duration; } AVPacket;

    FFMPEG使用AVPacket來暫存媒體數據包及附加信息(解碼時間戳、顯示時間戳、時長等),這樣的媒體數據包所承載的往往不是原始格式的音視頻數據,而是以某種方式編碼后的數據,編碼信息由對應的媒體流結構AVStream給出。AVPacket包含以下數據域:

    • dts表示解碼時間戳,pts表示顯示時間戳,它們的單位是所屬媒體流的時間基準。

    • stream_index給出所屬媒體流的索引;

    • data為數據緩沖區指針,size為長度;

    • duration為數據的時長,也是以所屬媒體流的時間基準為單位;

    • pos表示該數據在媒體流中的字節偏移量;

    • destruct為用于釋放數據緩沖區的函數指針;

    • flags為標志域,其中,最低為置1表示該數據是一個關鍵幀。

    AVPacket結構本身只是個容器,它使用data成員引用實際的數據緩沖區。這個緩沖區的管理方式有兩種,其一是通過調用av_new_packet直接創建緩沖區,其二是引用已經存在的緩沖區。緩沖區的釋放通過調用av_free_packet實現,其內部實現也采用了兩種不同的釋放方式,第一種方式是調用AVPacket的destruct函數,這個destruct函數可能是缺省的av_destruct_packet,對應av_new_packet或av_dup_packet創建的緩沖區,也可能是某個自定義的釋放函數,表示緩沖區的提供者希望使用者在結束緩沖區的時候按照提供者期望的方式將其釋放,第二種方式是僅僅將data和size的值清0,這種情況下往往是引用了一個已經存在的緩沖區,AVPacket的destruct指針為空。

    在使用AVPacket時,對于緩沖區的提供者,必須注意通過設置destruct函數指針指定正確的釋放方式,如果緩沖區提供者打算自己釋放緩沖區,則切記將destruct置空;而對于緩沖區的使用者,務必在使用結束時調用av_free_packet釋放緩沖區(雖然釋放操作可能只是一個假動作)。如果某個使用者打算較長時間內占用一個AVPacket——比如不打算在函數返回之前釋放它——最好調用av_dup_packet進行緩沖區的克隆,將其轉化為自有分配的緩沖區,以免對緩沖區的不當占用造成異常錯誤。av_dup_packet會為destruct指針為空的AVPacket新建一個緩沖區,然后將原緩沖區的數據拷貝至新緩沖區,置data的值為新緩沖區的地址,同時設destruct指針為av_destruct_packet。

    上述媒體結構可以通過FFMPEG提供的av_dump_format方法直觀展示出來,以下例子以一個MPEG-TS文件為輸入,其展示結果為:

    Input #0, mpegts, from '/Videos/suite/ts/H.264_High_L3.1_720x480_23.976fps_AAC-LC.ts':Duration: 00:01:43.29, start: 599.958300, bitrate: 20934 kb/sProgram 1 Stream #0.0[0x1011]: Video: h264 (High), yuv420p, 720x480 [PAR 32:27 DAR 16:9], 23.98 fps, 23.98 tbr, 90k tbn, 47.95 tbcStream #0.1[0x1100]: Audio: aac, 48000 Hz, stereo, s16, 159 kb/s

    從中可以找到媒體的格式、路徑、時長、開始時間、全局比特率。此外,列出了媒體包含的一個節目,由兩個媒體流組成,第一個媒體流是視頻流,id為0x1011;第二個為音頻流,id為0x1100。視頻流是h264的High Profile編碼,色彩空間為420p,大小為720×480,平均幀率23.98,參考幀率23.98,流時間基準是90000,編碼的時間基準為47.95;音頻流是aac編碼,48kHz的采樣,立體聲,每個樣點16比特,流的比特率是159kbps。

    時間信息

    時間信息用于實現多媒體同步。

    同步的目的在于展示多媒體信息時,能夠保持媒體對象之間固有的時間關系。同步有兩類,一類是流內同步,其主要任務是保證單個媒體流內的時間關系,以滿足感知要求,如按照規定的幀率播放一段視頻;另一類是流間同步,主要任務是保證不同媒體流之間的時間關系,如音頻和視頻之間的關系(lipsync)。

    對于固定速率的媒體,如固定幀率的視頻或固定比特率的音頻,可以將時間信息(幀率或比特率)置于文件首部(header),如AVI的hdrl List、MP4的moov box,還有一種相對復雜的方案是將時間信息嵌入媒體流的內部,如MPEG TS和Real video,這種方案可以處理變速率的媒體,亦可有效避免同步過程中的時間漂移。

    FFMPEG會為每一個數據包打上時間標簽,以更有效地支持上層應用的同步機制。時間標簽有兩種,一種是DTS,稱為解碼時間標簽,另一種是PTS,稱為顯示時間標簽。對于聲音來說 ,這兩個時間標簽是相同的,但對于某些視頻編碼格式,由于采用了雙向預測技術,會造成DTS和PTS的不一致。

    無雙向預測幀的情況:

    圖像類型: I P P P P P P ... I P P DTS: 0 1 2 3 4 5 6... 100 101 102 PTS: 0 1 2 3 4 5 6... 100 101 102

    有雙向預測幀的情況:

    圖像類型: I P B B P B B ... I P B DTS: 0 1 2 3 4 5 6 ... 100 101 102 PTS: 0 3 1 2 6 4 5 ... 100 104 102

    對于存在雙向預測幀的情況,通常要求解碼器對圖像重排序,以保證輸出的圖像順序為顯示順序:

    解碼器輸入:I P B B P B B(DTS) 0 1 2 3 4 5 6 (PTS) 0 3 1 2 6 4 5 解碼器輸出:X I B B P B B P(PTS) X 0 1 2 3 4 5 6

    時間信息的獲取:

    通過調用avformat_find_stream_info,多媒體應用可以從AVFormatContext對象中拿到媒體文件的時間信息:主要是總時間長度和開始時間,此外還有與時間信息相關的比特率和文件大小。其中時間信息的單位是AV_TIME_BASE:微秒。

    typedef struct AVFormatContext {....../** Decoding: position of the first frame of the component, inAV_TIME_BASE fractional seconds. NEVER set this value directly:It is deduced from the AVStream values. */int64_t start_time;/** Decoding: duration of the stream, in AV_TIME_BASE fractionalseconds. Only set this value if you know none of the individual streamdurations and also dont set any of them. This is deduced from theAVStream values if not set. */int64_t duration;/** decoding: total file size, 0 if unknown */int64_t file_size;/** Decoding: total stream bitrate in bit/s, 0 if notavailable. Never set it directly if the file_size and theduration are known as FFmpeg can compute it automatically. */int bit_rate;......} AVFormatContext;

    以上4個成員變量都是只讀的,基于FFMpeg的中間件需要將其封裝到某個接口中,如:

    LONG GetDuratioin(IntfX*); LONG GetStartTime(IntfX*); LONG GetFileSize(IntfX*); LONG GetBitRate(IntfX*);

    APIs

    FFMpeg的API大部分以0作為成功返回值而一個負數作為錯誤碼。

    讀系列

    讀系列API的主要功能是根據某個指定的源獲取媒體數據包,這個源可以是一個本地文件、一個RTSP或HTTP源、一個攝像頭驅動或者其它。

    avformat_open_input

    int avformat_open_input(AVFormatContext **ic_ptr, const char *filename, AVInputFormat *fmt, AVDictionary **options);

    avformat_open_input完成兩個任務:

  • 打開一個文件或URL,基于字節流的底層輸入模塊得到初始化。

  • 解析多媒體文件或多媒體流的頭信息,創建AVFormatContext結構并填充其中的關鍵字段,依次為各個原始流建立AVStream結構。

  • 一個多媒體文件或多媒體流與其包含的原始流的關系如下:

    多媒體文件/多媒體流 (movie.mkv)原始流 1 (h.264 video)原始流 2 (aac audio for Chinese)原始流 3 (aac audio for english)原始流 4 (Chinese Subtitle)原始流 5 (English Subtitle)...

    關于輸入參數:

    • ic_ptr,這是一個指向指針的指針,用于返回avformat_open_input內部構造的一個AVFormatContext結構體。

    • filename,指定文件名。

    • fmt,用于顯式指定輸入文件的格式,如果設為空則自動判斷其輸入格式。

    • options

    這個函數通過解析多媒體文件或流的頭信息及其他輔助數據,能夠獲取足夠多的關于文件、流和編解碼器的信息,但由于任何一種多媒體格式提供的信息都是有限的,而且不同的多媒體內容制作軟件對頭信息的設置不盡相同,此外這些軟件在產生多媒體內容時難免會引入一些錯誤,因此這個函數并不保證能夠獲取所有需要的信息,在這種情況下,則需要考慮另一個函數:avformat_find_stream_info。

    avformat_find_stream_info

    int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);

    這個函數主要用于獲取必要的編解碼器參數,設置到ic→streams[i]→codec中。

    首先必須得到各媒體流對應編解碼器的類型和id,這是兩個定義在avutils.h和avcodec.h中的枚舉:

    enum AVMediaType {AVMEDIA_TYPE_UNKNOWN = -1,AVMEDIA_TYPE_VIDEO,AVMEDIA_TYPE_AUDIO,AVMEDIA_TYPE_DATA,AVMEDIA_TYPE_SUBTITLE,AVMEDIA_TYPE_ATTACHMENT,AVMEDIA_TYPE_NB }; enum CodecID {CODEC_ID_NONE,/* video codecs */CODEC_ID_MPEG1VIDEO,CODEC_ID_MPEG2VIDEO, ///< preferred ID for MPEG-1/2 video decodingCODEC_ID_MPEG2VIDEO_XVMC,CODEC_ID_H261,CODEC_ID_H263,... };

    通常,如果某種媒體格式具備完備而正確的頭信息,調用avformat_open_input即可以得到這兩個參數,但若是因某種原因avformat_open_input無法獲取它們,這一任務將由avformat_find_stream_info完成。

    其次還要獲取各媒體流對應編解碼器的時間基準。

    此外,對于音頻編解碼器,還需要得到:

  • 采樣率,

  • 聲道數,

  • 位寬,

  • 幀長度(對于某些編解碼器是必要的),

  • 對于視頻編解碼器,則是:

  • 圖像大小,

  • 色彩空間及格式,

  • av_read_frame

    int av_read_frame(AVFormatContext *s, AVPacket *pkt);

    這個函數用于從多媒體文件或多媒體流中讀取媒體數據,獲取的數據由AVPacket結構pkt來存放。對于音頻數據,如果是固定比特率,則pkt中裝載著一個或多個音頻幀;如果是可變比特率,則pkt中裝載有一個音頻幀。對于視頻數據,pkt中裝載有一個視頻幀。需要注意的是:再次調用本函數之前,必須使用av_free_packet釋放pkt所占用的資源。

    通過pkt→stream_index可以查到獲取的媒體數據的類型,從而將數據送交相應的解碼器進行后續處理。

    av_seek_frame

    int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags);

    這個函數通過改變媒體文件的讀寫指針來實現對媒體文件的隨機訪問,支持以下三種方式:

    • 基于時間的隨機訪問:具體而言就是將媒體文件讀寫指針定位到某個給定的時間點上,則之后調用av_read_frame時能夠讀到時間標簽等于給定時間點的媒體數據,通常用于實現媒體播放器的快進、快退等功能。

    • 基于文件偏移的隨機訪問:相當于普通文件的seek函數,timestamp也成為文件的偏移量。

    • 基于幀號的隨機訪問:timestamp為要訪問的媒體數據的幀號。

    關于參數:

    • s:是個AVFormatContext指針,就是avformat_open_input返回的那個結構。

    • stream_index:指定媒體流,如果是基于時間的隨機訪問,則第三個參數timestamp將以此媒體流的時間基準為單位;如果設為負數,則相當于不指定具體的媒體流,FFMPEG會按照特定的算法尋找缺省的媒體流,此時,timestamp的單位為AV_TIME_BASE(微秒)。

    • timestamp:時間標簽,單位取決于其他參數。

    • flags:定位方式,AVSEEK_FLAG_BYTE表示基于字節偏移,AVSEEK_FLAG_FRAME表示基于幀號,其它表示基于時間。

    av_close_input_file

    void av_close_input_file(AVFormatContext *s);

    關閉一個媒體文件:釋放資源,關閉物理IO。

    編解碼系列

    編解碼API負責實現媒體原始數據的編碼和媒體壓縮數據的解碼。

    avcodec_find_decoder

    AVCodec *avcodec_find_decoder(enum CodecID id); AVCodec *avcodec_find_decoder_by_name(const char *name);

    根據給定的codec id或解碼器名稱從系統中搜尋并返回一個AVCodec結構的指針。

    avcodec_open2

    int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);

    此函數根據輸入的AVCodec指針具體化AVCodecContext結構。在調用該函數之前,需要首先調用avcodec_alloc_context分配一個AVCodecContext結構,或調用avformat_open_input獲取媒體文件中對應媒體流的AVCodecContext結構;此外還需要通過avcodec_find_decoder獲取AVCodec結構。

    這一函數還將初始化對應的解碼器。

    avcodec_decode_video2

    int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt);

    解碼一個視頻幀。got_picture_ptr指示是否有解碼數據輸出。

    輸入數據在AVPacket結構中,輸出數據在AVFrame結構中。AVFrame是定義在avcodec.h中的一個數據結構:

    typedef struct AVFrame {FF_COMMON_FRAME } AVFrame;

    FF_COMMON_FRAME定義了諸多數據域,大部分由FFMpeg內部使用,對于用戶來說,比較重要的主要包括:

    #define FF_COMMON_FRAME \ ......uint8_t *data[4];\int linesize[4];\int key_frame;\int pict_type;\int64_t pts;\ int reference;\ ......

    FFMpeg內部以planar的方式存儲原始圖像數據,即將圖像像素分為多個平面(R/G/B或Y/U/V),data數組內的指針分別指向四個像素平面的起始位置,linesize數組則存放各個存貯各個平面的緩沖區的行寬:

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++data[0]->#################################++++++++++++ ++++++++++++###########picture data##########++++++++++++ ++++++++++++#################################++++++++++++ ++++++++++++#################################++++++++++++ ........................ ++++++++++++#################################++++++++++++ |<-------------------line_size[0]---------------------->|

    此外,key_frame標識該圖像是否是關鍵幀;pict_type表示該圖像的編碼類型:I(1)/P(2)/B(3)……;pts是以time_base為單位的時間標簽,對于部分解碼器如H.261、H.263和MPEG4,可以從頭信息中獲取;reference表示該圖像是否被用作參考。

    avcodec_decode_audio4

    int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt);

    解碼一個音頻幀。輸入數據在AVPacket結構中,輸出數據在frame中,got_frame_ptr表示是否有數據輸出。

    • avcodec_close

    int avcodec_close(AVCodecContext *avctx);

    關閉解碼器,釋放avcodec_open中分配的資源。

    寫系列

    寫系列API負責將媒體數據以包的形式分發到指定的目標,這個目標可以是一個本地文件、一個RTSP或HTTP流或者其他。

    avformat_alloc_output_context2

    int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat,const char *format_name, const char *filename);

    這個函數負責分配一個用于輸出目的的AVFormatContext,輸出格式由oformat、format_name和filename決定,oformat優先級最高,如果oformat為空則依據format_name,如果format_name為空則依據filename。

    avformat_write_header

    int avformat_write_header(AVFormatContext *s, AVDictionary **options);

    此函數負責產生一個文件頭并寫入到輸出目標中,調用之前,AVFormatContext結構中的oformat、pb必須正確設置,并且streams列表不能為空,列表中各stream的codec參數需要配置正確。

    av_write_frame

    int av_write_frame(AVFormatContext *s, AVPacket *pkt);

    此函數負責輸出一個媒體包。AVFormatContext參數給出輸出目標及媒體流的格式設置,pkt是一個包含媒體數據的結構,其內部成員如dts/pts、stream_index等必須被正確設置。

    av_interleaved_write_frame

    int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);

    此函數負責交錯地輸出一個媒體包。如果調用者無法保證來自各個媒體流的包正確交錯,則最好調用此函數輸出媒體包,反之,可以調用av_write_frame以提高性能。

    av_write_trailer

    int av_write_trailer(AVFormatContext *s);

    其他

    avformat_new_stream

    AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c);

    這個函數負責創建一個AVStream結構,并將其添加到指定的AVFormatContext中,此時,AVStream的大部分域處于非法狀態。如果輸入參數c不為空,AVStream的codec域根據c實現初始化。

    核心架構和機制

    過濾鏈

    FFMPEG支持由多個過濾器結構組成的過濾鏈,以實現對視頻幀和音頻采樣數據的后續處理,如圖像縮放、圖像增強、聲音的重采樣等。

    數據結構

    FFMPEG使用了的四個主要的數據結構來構造過濾鏈,如上圖所示。其中,AVFilter是一個過濾器的抽象,它擁有若干AVFilterPad,分別實現過濾器數據的輸入和輸出。而AVFilterLink表示各個過濾器之間的連接,這種連接的實現是基于AVFilterPad的,每個連接都有一個實現輸入的AVFilterPad和一個實現輸出的AVFilterPad以及對應的源過濾器和目的過濾器。

    此外,還有兩個數據結構AVFilterBuffer和AVFilterBufferRef來表示過濾器的輸入輸出數據,其中AVFilterBufferRef是AVFilterBuffer的引用,這種設計的目的是避免不必要的數據拷貝。通過avfilter_get_buffer_ref_from_frame可以獲得一個AVFrame結構的引用,從而實現過濾器對來自AVFrame的圖像原數據或聲音采樣序列的處理。

    APIs

    avfilter_open

    avfilter_free

    avfilter_init_filter

    avfilter_link

    avfilter_link_free

    avfilter_ref_buffer

    運作原理

    總結

    以上是生活随笔為你收集整理的FFMPEG详解(完整版)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。