FFmpeg原始碼結構圖,ffmpeg原始碼結構

來源:互聯網
上載者:User

FFmpeg原始碼結構圖,ffmpeg原始碼結構

=====================================================

FFmpeg的庫函數原始碼分析文章列表:

【通用】

FFmpeg 原始碼簡單分析:av_register_all()

FFmpeg 原始碼簡單分析:avcodec_register_all()

FFmpeg 原始碼簡單分析:記憶體的分配和釋放(av_malloc()、av_free()等)

FFmpeg 原始碼簡單分析:常見結構體的初始化和銷毀(AVFormatContext,AVFrame等)

FFmpeg 原始碼簡單分析:avio_open2()

FFmpeg 原始碼簡單分析:av_find_decoder()和av_find_encoder()

FFmpeg 原始碼簡單分析:avcodec_open2()

FFmpeg 原始碼簡單分析:avcodec_close()

【解碼】

圖解FFMPEG開啟媒體的函數avformat_open_input

FFmpeg 原始碼簡單分析:avformat_open_input()

FFmpeg 原始碼簡單分析:avformat_find_stream_info()

FFmpeg 原始碼簡單分析:av_read_frame()

FFmpeg 原始碼簡單分析:avcodec_decode_video2()

FFmpeg 原始碼簡單分析:avformat_close_input()

【編碼】

FFmpeg 原始碼簡單分析:avformat_alloc_output_context2()

FFmpeg 原始碼簡單分析:avformat_write_header()

FFmpeg 原始碼簡單分析:avcodec_encode_video()

FFmpeg 原始碼簡單分析:av_write_frame()

FFmpeg 原始碼簡單分析:av_write_trailer()

【架構圖】

FFmpeg原始碼結構圖 - 解碼

FFmpeg原始碼結構圖 - 編碼

=====================================================

上一篇文章深入分析了FFmpeg解碼過程中核心API的內部原始碼,本文繼續分析FFmpeg編碼過程中核心API的內部原始碼。本文的編碼流程可以參考程式《最簡單的基於FFmpeg的視頻編碼器》。



函數呼叫歷程圖

首先呈現分析的結果,。這張圖的尺寸很大(大於4000x4000),因此需要開啟圖片連結之後將圖片儲存為本地檔案,然後才能查看。它表明了FFmpeg在編碼一個視頻的時候的函數調用流程。為了保證結構清晰,其中僅列出了最關鍵的函數,剔除了其它不是特別重要的函數。


單擊查看更清晰的圖片


下面解釋一中關鍵標記的含義。


函數背景色函數在圖中以方框的形式表現出來。不同的背景色標誌了該函數不同的作用:
粉紅色背景函數:FFmpeg的API函數。
白色背景的函數:FFmpeg的內建函式。
黃色背景的函數:URLProtocol結構體中的函數,包含了讀寫各種協議的功能。
綠色背景的函數:AVOutputFormat結構體中的函數,包含了讀寫各種封裝格式的功能。
藍色背景的函數:AVCodec結構體中的函數,包含了編解碼的功能。

地區整個關係圖可以分為以下幾個地區:
左邊地區——架構函數地區:這些函數並不針對某一特定的視頻格式。
右上方黃色地區——協議處理函數地區:不同的協議(RTP,RTMP,FILE)會調用不同的協議處理函數。
右邊中間綠色地區——封裝格式處理函數地區:不同的封裝格式(MKV,FLV,MPEG2TS,AVI)會調用不同的封裝格式處理函數。
右邊下方藍色地區——編解碼函數地區:不同的編碼通訊協定(HEVC,H.264,MPEG2)會調用不同的編解碼函數。


箭頭線為了把調用關係表示的更明顯,圖中的箭頭線也使用了不同的顏色:
紅色的箭頭線:標誌了編碼的流程。
其他顏色的箭頭線:標誌了函數之間的調用關係。其中:
調用URLProtocol結構體中的函數用黃色箭頭線標識;
調用AVOutputFormat結構體中的函數用綠色箭頭線標識;

調用AVCodec結構體中的函數用藍色箭頭線標識。


函數所在的檔案每個函數標識了它所在的檔案路徑。



函數功能簡述下面簡單列出幾個地區中函數之間的調用關係(函數之間的調用關係使用縮排的方式表現出來)。詳細的函數分析可以參考相關的《FFmpeg原始碼分析》系列文章。

左邊地區(架構函數)

 

1. av_register_all()【函數簡單分析】

1) avcodec_register_all()

(a) REGISTER_HWACCEL()

(b) REGISTER_ENCODER()

(c) REGISTER_DECODER()

(d) REGISTER_PARSER()

(e) REGISTER_BSF()

2) REGISTER_MUXER()

3) REGISTER_DEMUXER()

4) REGISTER_PROTOCOL()

2. avformat_alloc_output_context2()【函數簡單分析】

1) avformat_alloc_context()

(a) av_malloc(sizeof(AVFormatContext))

(b) avformat_get_context_defaults()

a) av_opt_set_defaults()

2) av_guess_format()

(a) av_oformat_next()

(b) av_match_name()

(c) av_match_ext()

3. avio_open2()【函數簡單分析】

1) ffurl_open()

(a) ffurl_alloc()

a) url_find_protocol()

b) url_alloc_for_protocol()

(b) ffurl_connect()

a) URLProtocol->url_open()

2) ffio_fdopen()

(a) av_malloc(buffer_size)

(b) avio_alloc_context()

a) av_mallocz(sizeof(AVIOContext))

b) ffio_init_context()

4. avformat_new_stream()【函數簡單分析】

1) av_mallocz(sizeof(AVStream))

2) avcodec_alloc_context3()

(a) av_malloc(sizeof(AVCodecContext))

(b) avcodec_get_context_defaults3()

5. avcodec_find_encoder()【函數簡單分析】

1) find_encdec()

6. avcodec_open2()【函數簡單分析】

1) AVCodec->init()

7. avformat_write_header()【函數簡單分析】

1) init_muxer()

2) AVOutputFormat->write_header()

3) init_pts()

8. avcodec_encode_video2()【函數簡單分析】

1) AVCodec->encode2()

9. av_write_frame()【函數簡單分析】

1) check_packet()

2) compute_pkt_fields2()

3) write_packet()

(a) AVOutputFormat->write_packet()

10. av_write_trailer()【函數簡單分析】

1) write_packet()

2) AVOutputFormat->write_trailer()

11. avcodec_close()【函數簡單分析】

1) AVCodec->close()

12. avformat_free_context()【函數簡單分析】

1) ff_free_stream()

13. avio_close()【函數簡單分析】

1) avio_flush()

(a) flush_buffer()

2) ffurl_close()

(a) ffurl_closep()

a) URLProtocol->url_close()

 

右上地區(URLProtocol協議處理函數)URLProtocol結構體包含如下協議處理函數指標:
url_open():開啟
url_read():讀取
url_write():寫入
url_seek():調整進度
url_close():關閉

【例子】不同的協議對應著上述介面有不同的實現函數,舉幾個例子:

File協議(即檔案)對應的URLProtocol結構體ff_file_protocol:
url_open() -> file_open() -> open()
url_read() -> file_read() -> read()
url_write() -> file_write() -> write()
url_seek() -> file_seek() -> lseek()

url_close() -> file_close() -> close()

RTMP協議(libRTMP)對應的URLProtocol結構體ff_librtmp_protocol:
url_open() -> rtmp_open() -> RTMP_Init(), RTMP_SetupURL(), RTMP_Connect(), RTMP_ConnectStream()
url_read() -> rtmp_read() -> RTMP_Read()
url_write() -> rtmp_write() -> RTMP_Write()
url_seek() -> rtmp_read_seek() -> RTMP_SendSeek()

url_close() -> rtmp_close() -> RTMP_Close()

UDP協議對應的URLProtocol結構體ff_udp_protocol:
url_open() -> udp_open()
url_read() -> udp_read()
url_write() -> udp_write()
url_seek() -> udp_close()
url_close() -> udp_close()

右中地區(AVOutputFormat封裝格式處理函數)AVOutputFormat包含如下封裝格式處理函數指標:
write_header():寫檔案頭
write_packet():寫一幀資料
write_trailer():寫檔案尾

【例子】不同的封裝格式對應著上述介面有不同的實現函數,舉幾個例子:

FLV封裝格式對應的AVOutputFormat結構體ff_flv_muxer:
write_header() -> flv_write_header()
write_packet() –> flv_write_packet()

write_trailer() -> flv_write_trailer()

MKV封裝格式對應的AVOutputFormat結構體ff_matroska_muxer:
write_header() -> mkv_write_header()
write_packet() –> mkv_write_flush_packet()

write_trailer() -> mkv_write_trailer()

MPEG2TS封裝格式對應的AVOutputFormat結構體ff_mpegts_muxer:
write_header() -> mpegts_write_header()
write_packet() –> mpegts_write_packet()

write_trailer() -> mpegts_write_end()

AVI封裝格式對應的AVOutputFormat結構體ff_avi_muxer:
write_header() -> avi_write_header()
write_packet() –> avi_write_packet()
write_trailer() -> avi_write_trailer()

右下地區(AVCodec編解碼函數)AVCodec包含如下編解碼函數指標:
init():初始化
encode2():編碼一幀資料
close():關閉

【例子】不同的轉碼器對應著上述介面有不同的實現函數,舉幾個例子:

HEVC編碼器對應的AVCodec結構體ff_libx265_encoder:
init() -> libx265_encode_init() -> x265_param_alloc(), x265_param_default_preset(), x265_encoder_open()
encode2() -> libx265_encode_frame() -> x265_encoder_encode()

close() -> libx265_encode_close() -> x265_param_free(), x265_encoder_close()

H.264編碼器對應的AVCodec結構體ff_libx264_encoder:
init() -> X264_init() -> x264_param_default(), x264_encoder_open(), x264_encoder_headers()
encode2() -> X264_frame() -> x264_encoder_encode()

close() -> X264_close() -> x264_encoder_close()

VP8編碼器(libVPX)對應的AVCodec結構體ff_libvpx_vp8_encoder:
init() -> vpx_init() -> vpx_codec_enc_config_default()
encode2() -> vp8_encode() -> vpx_codec_enc_init(), vpx_codec_encode()

close() -> vp8_free() -> vpx_codec_destroy()

MPEG2編碼器對應的AVCodec結構體ff_mpeg2video_encoder:
init() -> encode_init()
encode2() -> ff_mpv_encode_picture()
close() -> ff_mpv_encode_end()



雷霄驊
leixiaohua1020@126.com
http://blog.csdn.net/leixiaohua1020


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.