03《FFMPEG架构简单分析》

《FFMPEG架构简单分析》


另:gitee markdown暂不支持mermaid流程图显示,移步CSDN看完整版

作者日期
将狼才鲸2021-08-14

CSDN阅读完整版:
b站其它视频:才鲸嵌入式
Gitee源码下载地址:才鲸/音视频编解码

ffmpeg源码架构

1. ffmpeg提供应用程序:

ffmpeg、ffplay、ffprobe  

2. 提供库:

libavutil、libavcodec、libavformat、libavdevice、libavfilter、libswscale、libswresample、libpostproc

提供的服务
所有API讲解
开发Demo
wiki及编解码
一些书籍
An ffmpeg and SDL Tutorial
FFmpeg book 命令大全

2.1 模块依赖

图1 模块依赖关系

相互依赖
依赖于
依赖于
依赖于
依赖于
相互依赖
依赖于
依赖于
依赖于
依赖于
依赖于
依赖于
依赖于
依赖于
依赖于
依赖于
依赖关系
libavformat
libavcodec
libavutil
libswresample
libavfilter
libswscale
libpostproc
libavdevice

3. 源码结构:

DIR          compat	操作系统和数学库有关  
DIR          fftools		ffmpeg ffplay ffprobe应用程序  
DIR          libavcodec		库  编解码
DIR          libavdevice	库  设备
DIR          libavfilter	库  滤镜
DIR          libavformat	库  封装
DIR          libavresample	库  音频重采样、样本格式转换和混合
DIR          libavutil		库	字符串函数、随机数生成器、数据结构、附加数学函数、加密和多媒体相关
DIR          libpostproc	库  用于在视频看起来很糟糕时进行视频后期处理
DIR          libswresample	库	高度优化的音频重采样、样本格式转换和混合
DIR          libswscale		库  高度优化的图像缩放、颜色和像素格式转换
其它的文件夹和主目录的下的文件与程序源码没有关系  

4. 功能结构

ffmpeg用C语言实现面向对象底层结构

图1 底层结构
用C语言实现面向对象的功能
libavutil/opt.h
objects
AVClass
AVOptions
av_opt_set_defaults初始化
av_opt_free自动释放
AVOptions-enabled
嵌套
AVOptions-enabled
......
AVClass
priv_data指针字段
AVClass.child_next
AVClass.child_class_next
child_next迭代实际存在的对象
child_class_next迭代所有可能的子类
AVOptions
av_opt_next遍历对象
av_opt_find查找对象
av_opt_child_class_next
av_opt_child_next使用子项
AVOptions提供了通用的option设置和获取机制,可适用于任意struct(通常要求该结构体的第一个成员必须是AVClass指针,该AVClass.options必须指向一个AVOptions的静态数组,以NULL作为结束)
图2 结构体和API
播放
格式转换
格式预览
功能
代码
结构体
libavformat
AVInputFormat
AVOutputFormat
AVFormatContext
AVOptions
AVClass
其它库......
API
libavformat
av_register_all
avformat_network_init
av_iformat_next
av_oformat_next
avio_enum_protocols
avformat_alloc_context
avformat_open_input
其他库......
applibavformatlibavcodeclibavfilterlibavutil
ffplayDemuxingDecodingBuffer sinkCrypto and Hashing
ffprobeMuxingEncodingBuffer sourceMathematics
ffmpegI/O Read/WriteCodecsString Manipulation
DemuxersInternalMemory Management
I/O ProtocolsCoreData Structures
InternalUtilityVideo related
Public Metadata APIAudio related
Core functionsError Codes
Utility functionsLogging Facility
表1 模块结构

5. 模块概述

1) libavformat

图3 封装(mp4 mkv等)
libavformat
结构体
AVInputFormat
AVOutputFormat
AVFormatContext
AVOptions
AVClass
API
av_register_all
avformat_network_init
av_iformat_next
av_oformat_next
avio_enum_protocols
avformat_alloc_context
avformat_open_input

libavformat::Demuxing API

libavformatDemuxingStruct
AVProbeDataAVIOContextAVInputFormatAVFormatContextAVDictionaryAVProgramAVMediaTypeAVCodec
libavformatDemuxingAPI
av_find_input_formatav_probe_input_formatav_probe_input_format2
av_probe_input_format3av_probe_input_buffer2av_probe_input_buffer
avformat_open_inputav_demuxer_openavformat_find_stream_info
av_find_program_from_streamav_program_add_stream_indexav_find_best_stream
av_read_frameav_seek_frameavformat_seek_file
avformat_flushav_read_playav_read_pause
avformat_close_input
libavformatMuxingStruct
AVFormatContextAVDictionaryAVPacketAVFrameAVOutputFormatAVCodecIDAVMediaType
libavformatMuxingAPI
avformat_write_headeravformat_init_outputav_write_frame
av_interleaved_write_frameav_write_uncoded_frameav_interleaved_write_uncoded_frame
av_write_uncoded_frame_queryav_write_trailerav_guess_format
av_guess_codecav_get_output_timestamp

2) libavcodec libavfilter libswscale libswresample libavdevice libavutil libpostproc

libavcodec API
Modules
Decoding
Encoding
Codecs
Internal
send/receive encoding and decoding API overview
Core functions/structures
Utility functions

libavfilter API
Modules
Buffer sink API
Buffer source API

libswscale

libswresample

libavdevice

libavutil
Modules
Crypto and Hashing
Mathematics
Mathematical utilities for working with timestamp and time base.
String Manipulation
Memory Management
Utilities for manipulating memory
Data Structures
Video related
Audio related
Error Codes
Logging Facility
Other

libpostproc

6. ffmpeg架构

7. ffplay架构

8. ffprobe架构

9. 驱动

  • 通过ffmpeg libavdevice 打开音频设备编译时会链接错误,网上找了很多文章没有办法解决,于是干脆自己从底层直接读写音频和显示驱动。

9.1 Linux驱动

9.1.1 音频驱动
  • Linux主流音频结构体ALSA,使用库libasound。
  • 以前用的是OSS音频架构
  • ALSA接口,proc接口,/proc/asound/
  • 同时在应用层,ALSA为我们提供了alsa-lib
  • /dev/snd/ 目录
  • 从8.10开始,ubuntu默认声卡驱动是pulseaudio
  • 编程时都使用libasound
9.1.2 显示驱动
  • 早期的Linux使用ramebuffer显示框架,后面是DRM框架
  • 桌面环境有gnome kde unity
  • X Window System,简称X,或者X11
  • OpenGl libGL
  • libDRM

9.2 Windows驱动

9.3 播放和录制

  • ffmpeg lib 播放与录制大全
  • 播放:文件(MP4 MKV …) --解封装–> (h264 hevc mepg4…) 和 (aac mp3…) --解码–> (rgb yuv YCrCb) 和 (pcm)
  • 录制:反过来

10. QEMU

  • 只能在Linux环境中安装
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值