(Audio and video study notes): Introduction to FFmpeg library, common functions and data structure analysis

table of Contents

Introduction to FFmpeg library

Introduction to FFmpeg function

Introduction to FFmpeg function-package format related

Introduction to FFmpeg decoding function-decoder related

FFmpeg 3.x component registration method

FFmpeg 4.x component registration method

Ffmpeg 4.0.2 component registration method

Introduction to FFmpeg data structure

The relationship between FFmpeg data structures

The relationship between AVFormatContext and AVInputFormat

The relationship between AVCodecContext and AVCodec

The relationship between FFmpeg data structures

FFmpeg data structure analysis

Introduction to FFmpeg library

  • The overall structure of FFmpeg

  • FFMPEG has 8 commonly used libraries:
AVUtil Core tool library, many other modules below will rely on this library to do some basic audio and video processing operations.
AVFormat File format and protocol library. This module is one of the most important modules. It encapsulates the Protocol layer, Demuxer and Muxer layers, making the protocol and format transparent to developers.
AVCodec The codec library encapsulates the Codec layer, but some Codecs have their own licenses. FFmpeg will not add libraries like libx264, FDK-AAC, etc. by default, but FFmpeg is like a platform and can be used by other third parties. The Codec is added in the form of a plug-in, and then provides a unified interface for developers.
AVFilter Audio and video filter library, this module provides processing including audio special effects and video special effects. In the process of encoding and decoding using FFmpeg API, it is very convenient and very efficient to directly use this module to do special effects for audio and video data. a method.
AVDevice Input and output device library, for example, if you need to compile the tool ffplay to play sound or video, you need to make sure that the module is turned on, and you also need to pre-compile the SDL, because the device module uses the SDL library for sound and video playback. .
SwrRessample The resampling module can be used for audio, digital audio may frequency for digital channel, data format, sample rate plurality of basic conversion information and the like.
SWScale This module is a module that converts the image format. For example, it can convert YUV data to RGB data, and the zoom size is changed from 1280*720 to 800*480.
PostProc This module can be used for post-processing. When we use AVFilter, we need to turn on the switch of this module, because some basic functions of this module will be used in Filter.

Introduction to FFmpeg function

av_register_all () Register all components, 4.0 has been deprecated
avdevice_register_all() Register the device, such as V4L2, etc.
avformat_network_init(); Initialize the network library and libraries related to the network encryption protocol (such as openssl)

Introduction to FFmpeg function-package format related

avformat_alloc_context() Responsible for applying for the memory of an AVFormatContext structure and performing simple initialization
avformat_free_context() Release everything in the structure and the structure itself
avformat_close_input() Turn off the demultiplexer. After closing, it is no longer necessary to use avformat_free_context to release.
avformat_open_input() Open input audio and video files
avformat_find_stream_info() Get audio and video file information
av_read_frame () Read audio and video package
avformat_seek_file() Positioning file (can be positioned by time)
av_seek_frame () Locate the file (can be located by file size)
  • Function call flow:

Introduction to FFmpeg decoding function-decoder related

avcodec_alloc_context3() Assign decoder context
avcodec_find_decoder() Find decoder based on ID
avcodec_find_decoder_by_name() According to the decoder name
avcodec_open2()  Open codec
avcodec_decode_video2() 解码一帧视频数据(不建议使用)
avcodec_decode_audio4() 解码一帧音频数据(不建议使用)
avcodec_send_packet() 发送编码数据包
avcodec_receive_frame() 接收解码后数据
avcodec_free_context() 释放解码器上下文,包含了avcodec_close()
avcodec_close() 关闭解码器
  • 函数调用流程:

  • 分析:

  • 其中name字段:

  • avcodec_find_decoder():
    • 查找注册的第一个H264解码器就退出来了。
  • avcodec_find_decoder_by_name():
    • 根据指定的名字查找,如果找不到指定名字,就是没有找到。
  • 如果上下文保存在解码器里面,多路解码的时候肯定有冲突。
    • 设计解码器要做到可重入,通过AVCodexContex保存解码后的上下文数据
  • AVCodexContex,avcosex_open2():让解码器和上下文做关联。

FFmpeg 3.x 组件注册方式

  • 使用ffmpeg,首先要执行av_register_all,把全局的解码器、编码器等结构体注册到全局的对象链表里,以便后面查找调用

FFmpeg 4.x 组件注册方式

  • FFmpeg内部去做,不需要用户调用API去注册。
  • 以codec编解码器为例:
    • 1. 在configure的时候生成要注册的组件
./configure:7203:print_enabled_components libavcodec/codec_list.c AVCodec codec_list $CODEC_LIST
  • 这里会生成一个codec_list.c 文件,里面只有static const AVCodec * const codec_list[]数组。
  • 2. 在libavcodec/allcodecs.c将static const AVCodec * const codec_list[]的编解码器用链表的方式组织起来

Ffmpeg 4.0.2 组件注册方式

  • FFmepg内部去做,不需要用户调用API去注册。
  • 对于demuxer/muxer(解复用器,也称容器)则对应
    • 1. libavformat/muxer_list.c
      • libavformat/demuxer_list.c 这两个文件也是在configure的时候生成,也就是说直接下载源码是没有这两个文件的。
    • 2. 在libavformat/allformats.c将demuxer_list[]和muexr_list[]以链表的方式组织。
  • 其他组件也是类似的方式

FFmpeg数据结构简介

AVFormatContext 封装格式上下文结构体,也是统领全局的结构体,保存了视频文件封装格式相关信息。

AVInputFormat demuxer

AVOutputFormat muxer

每种封装格式(例如FLV, MKV, MP4, AVI)对应一个该结构体。
AVStream 视频文件中每个视频(音频)流对应一个该结构体。
AVCodecContext 编解码器上下文结构体,保存了视频(音频)编解码相关信息。
AVCodec 每种视频(音频)编解码器(例如H.264解码器)对应一个该结构体。
AVPacket 存储一帧压缩编码数据。
AVFrame 存储一帧解码后像素(采样)数据。

FFmpeg数据结构之间的关系

AVFormatContext和AVInputFormat之间的关系

  • AVFormatContext API调用
  • AVInputFormat 主要是FFMPEG内部调用
  • 数据
    • AVFormatContext 封装格式上下文结构体
    • struct AVInputFormat *iformat;
  • 方法:所有的方法可重入的
    • AVInputFormat 每种封装格式(例如FLV, MKV, MP4)
    • int (*read_header)(struct AVFormatContext *);
    • int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
int avformat_open_input(AVFormatContext **ps, const char *filename,
                        AVInputFormat *fmt, AVDictionary **options)

AVCodecContext和AVCodec之间的关系

  • 数据
    • AVCodecContext 编码器上下文结构体
    • struct AVCodec *codec;
  • 方法:
    • AVCodec 每种视频(音频) 编解码器
int (*decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt);
int (*encode2)(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr);

FFmpeg数据结构之间的关系

  • AVFormatContext, AVStream和AVCodecContext之间的关系

  • 区分不同的码流(AVPacket 里面也有一个index的字段)
    • AVMEDIA_TYPE_VIDEO视频流
video_index = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1,-1, NULL, 0)
  • AVMEDIA_TYPE_AUDIO音频流
audio_index = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1,-1, NULL, 0)
  • AVPacket和AVFrame之间的关系

FFmpeg数据结构分析

  • AVFormatContext

iformat 输入媒体的AVInputFormat,比如指向AVInputFormatff_flv_demuxer
nb_streams 输入媒体的AVStream 个数
streams 输入媒体的AVStream []数组
duration 输入媒体的时长(以微秒为单位),计算方式可以参考av_dump_format()函数
bit_rate 输入媒体的码率
  • AVInputFormat
name 封装格式名称
extensions 封装格式的扩展名
id 封装格式ID
一些封装格式处理的接口函数 比如read_packet()
  • AVStream
index 标识该视频/音频流
time_base 该流的时基, PTS*time_base=真正的时间(秒)
avg_frame_rate 该流的帧率
duration 该视频/音频流长度
codecpar 编解码器参数属性
  • AVCodecParameters
codec_type 媒体类型,比如AVMEDIA_TYPE_VIDEO AVMEDIA_TYPE_AUDIO等
codec_id 编解码器类型, 比如AV_CODEC_ID_H264 AV_CODEC_ID_AAC
  • AVCodecContext
codec 编解码器的AVCodec,比如指向AVCodecff_aac_latm_decoder
width, height 图像的宽高(只针对视频)
pix_fmt 像素格式(只针对视频)
sample_rate 采样率(只针对音频)
channels 声道数(只针对音频)
sample_fmt 采样格式(只针对音频)
  • AVCodec
name 编解码器名称
type 编解码器类型
id 编解码器ID
一些编解码的接口函数 比如int (*decode)()
  • AVPacket
pts 显示时间戳
dts 解码时间戳
data 压缩编码数据
size 压缩编码数据大小
pos 数据的偏移地址
stream_index 所属的AVStream
  • AVFrame
data 解码后的图像像素数据(音频采样数据)
linesize 对视频来说是图像中一行像素的大小;对音频来说是整个音频帧的大小
width, height 图像的宽高(只针对视频)
key_frame 是否为关键帧(只针对视频) 
pict_type 帧类型(只针对视频) 。例如I, P, B
sample_rate 音频采样率(只针对音频)
nb_samples 音频每通道采样数(只针对音频)
pts 显示时间戳

Guess you like

Origin blog.csdn.net/baidu_41388533/article/details/112728029