先贴上雷神的一张FFmpeg关键结构体之间的关系图:

再看雷神的分析:

  • 每个AVStream存储一个视频/音频流的相关数据;
  • 每个AVStream对应一个AVCodecContext,存储该视频/音频流使用解码方式的相关数据;
  • 每个AVCodecContext中对应一个AVCodec,包含该视频/音频对应的解码器。
  • 每种解码器都对应一个AVCodec结构。

看过雷神的分析,再加上自己的使用经验,总结如下:

AVCodec是存储编解码器信息的结构体,一般来说使用如下两个函数获得:

/**
* Find a registered decoder with a matching codec ID.
*
* @param id AVCodecID of the requested decoder
* @return A decoder if one was found, NULL otherwise.
*/
AVCodec *avcodec_find_decoder(enum AVCodecID id); /**
* Find a registered encoder with a matching codec ID.
*
* @param id AVCodecID of the requested encoder
* @return An encoder if one was found, NULL otherwise.
*/
AVCodec *avcodec_find_encoder(enum AVCodecID id);

AVCodecContex结构体除了包含解码器之外还包含视音频流相关的信息,如宽高,比特率,声道数等信息。一般通过如下函数获得:

/**
* Allocate an AVCodecContext and set its fields to default values. The
* resulting struct should be freed with avcodec_free_context().
*
* @param codec if non-NULL, allocate private data and initialize defaults
* for the given codec. It is illegal to then call avcodec_open2()
* with a different codec.
* If NULL, then the codec-specific defaults won't be initialized,
* which may result in suboptimal default settings (this is
* important mainly for encoders, e.g. libx264).
*
* @return An AVCodecContext filled with default values or NULL on failure.
*/
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);

通过以上函数获得之后通常要将视音频流的信息赋值过去,使用如下函数:

/**
* Fill the codec context based on the values from the supplied codec
* parameters. Any allocated fields in codec that have a corresponding field in
* par are freed and replaced with duplicates of the corresponding field in par.
* Fields in codec that do not have a counterpart in par are not touched.
*
* @return >= 0 on success, a negative AVERROR code on failure.
*/
int avcodec_parameters_to_context(AVCodecContext *codec,
const AVCodecParameters *par);

其中的参数 AVCodecParameters为编解码器的相关参数,是从AVCodecContext分离出来,其结构体中没有函数。

/**
* This struct describes the properties of an encoded stream.
*
* sizeof(AVCodecParameters) is not a part of the public ABI, this struct must
* be allocated with avcodec_parameters_alloc() and freed with
* avcodec_parameters_free().
*/
typedef struct AVCodecParameters {
/**
* General type of the encoded data.
*/
enum AVMediaType codec_type;
/**
* Specific type of the encoded data (the codec used).
*/
enum AVCodecID codec_id;
/**
* Additional information about the codec (corresponds to the AVI FOURCC).
*/
uint32_t codec_tag; /**
* Extra binary data needed for initializing the decoder, codec-dependent.
*
* Must be allocated with av_malloc() and will be freed by
* avcodec_parameters_free(). The allocated size of extradata must be at
* least extradata_size + AV_INPUT_BUFFER_PADDING_SIZE, with the padding
* bytes zeroed.
*/
uint8_t *extradata;
/**
* Size of the extradata content in bytes.
*/
int extradata_size; /**
* - video: the pixel format, the value corresponds to enum AVPixelFormat.
* - audio: the sample format, the value corresponds to enum AVSampleFormat.
*/
int format; /**
* The average bitrate of the encoded data (in bits per second).
*/
int64_t bit_rate; /**
* The number of bits per sample in the codedwords.
*
* This is basically the bitrate per sample. It is mandatory for a bunch of
* formats to actually decode them. It's the number of bits for one sample in
* the actual coded bitstream.
*
* This could be for example 4 for ADPCM
* For PCM formats this matches bits_per_raw_sample
* Can be 0
*/
int bits_per_coded_sample; /**
* This is the number of valid bits in each output sample. If the
* sample format has more bits, the least significant bits are additional
* padding bits, which are always 0. Use right shifts to reduce the sample
* to its actual size. For example, audio formats with 24 bit samples will
* have bits_per_raw_sample set to 24, and format set to AV_SAMPLE_FMT_S32.
* To get the original sample use "(int32_t)sample >> 8"."
*
* For ADPCM this might be 12 or 16 or similar
* Can be 0
*/
int bits_per_raw_sample; /**
* Codec-specific bitstream restrictions that the stream conforms to.
*/
int profile;
int level; /**
* Video only. The dimensions of the video frame in pixels.
*/
int width;
int height; /**
* Video only. The aspect ratio (width / height) which a single pixel
* should have when displayed.
*
* When the aspect ratio is unknown / undefined, the numerator should be
* set to 0 (the denominator may have any value).
*/
AVRational sample_aspect_ratio; /**
* Video only. The order of the fields in interlaced video.
*/
enum AVFieldOrder field_order; /**
* Video only. Additional colorspace characteristics.
*/
enum AVColorRange color_range;
enum AVColorPrimaries color_primaries;
enum AVColorTransferCharacteristic color_trc;
enum AVColorSpace color_space;
enum AVChromaLocation chroma_location; /**
* Video only. Number of delayed frames.
*/
int video_delay; /**
* Audio only. The channel layout bitmask. May be 0 if the channel layout is
* unknown or unspecified, otherwise the number of bits set must be equal to
* the channels field.
*/
uint64_t channel_layout;
/**
* Audio only. The number of audio channels.
*/
int channels;
/**
* Audio only. The number of audio samples per second.
*/
int sample_rate;
/**
* Audio only. The number of bytes per coded audio frame, required by some
* formats.
*
* Corresponds to nBlockAlign in WAVEFORMATEX.
*/
int block_align;
/**
* Audio only. Audio frame size, if known. Required by some formats to be static.
*/
int frame_size; /**
* Audio only. The amount of padding (in samples) inserted by the encoder at
* the beginning of the audio. I.e. this number of leading decoded samples
* must be discarded by the caller to get the original audio without leading
* padding.
*/
int initial_padding;
/**
* Audio only. The amount of padding (in samples) appended by the encoder to
* the end of the audio. I.e. this number of decoded samples must be
* discarded by the caller from the end of the stream to get the original
* audio without any trailing padding.
*/
int trailing_padding;
/**
* Audio only. Number of samples to skip after a discontinuity.
*/
int seek_preroll;
} AVCodecParameters;

这么做是因为AVCodecContext这个结构体实在是太大了。

我们可以从 AVStream 的 codecpar 成员中直接获得AVCodecParameters。

最后附上参考雷神的链接:

https://blog.csdn.net/leixiaohua1020/article/details/11693997

缅怀

FFmpeg——AVCodec,AVCodecContext,AVCodecParameters 辨析的更多相关文章

  1. FFmpeg AVCodec

    FFmpeg编解码 FFmpeg支持绝大多数视频编解码格式,如何遍历FFmpeg编解码器? 编解码器以链表形式存储,使用av_codec_next() 函数可以获取编解码器指针,当参数为NULL时,获 ...

  2. FFmpeg 学习(一):FFmpeg 简介

    一.FFmpeg 介绍 FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常先 ...

  3. 新手学习FFmpeg - 调用API完成视频的读取和输出

    在写了几个avfilter之后,原本以为对ffmpeg应该算是入门了. 结果今天想对一个视频文件进行转码操作,才发现基本的视频读取,输出都搞不定. 痛定思痛,仔细研究了一下ffmpeg提供的examp ...

  4. ffmpeg 和 SDL 的结合使用

    FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视 频的完整解决方案.它包含了非常先进的音频/视频编解码库 ...

  5. 详细介绍Qt,ffmpeg 和SDL开发

        Qt 与 ffmpeg 与 SDl 教程是本文要介绍的内容,从多个角度介绍本文,运用了qmake,先来看内容. 1.  注释 从“ #” 开始,到这一行结束. 2.  指定源文件 1.     ...

  6. ffmpeg架构和解码流程分析

    转 一,ffmpeg架构 1. 简介 FFmpeg是一个集录制.转换.音/视频编码解码功能为一体的完整的开源解决方案.FFmpeg的 开发是基于Linux操作系统,但是可以在大多数操作系统中编译和使用 ...

  7. (转)如何基于FFMPEG和SDL写一个少于1000行代码的视频播放器

    原文地址:http://www.dranger.com/ffmpeg/ FFMPEG是一个很好的库,可以用来创建视频应用或者生成特定的工具.FFMPEG几乎为你把所有的繁重工作都做了,比如解码.编码. ...

  8. 利用FFmpeg玩转Android视频录制与压缩(二)<转>

    转载出处:http://blog.csdn.net/mabeijianxi/article/details/72983362 预热 时光荏苒,光阴如梭,离上一次吹牛逼已经过去了两三个月,身边很多人的女 ...

  9. ffmpeg教程

    转:http://blog.sina.com.cn/s/blog_51396f890100nd91.html 概要  电影文件有很多基本的组成部分.首先,文件本身被称为容器Container,容器的类 ...

随机推荐

  1. 不会PPT配色没关系,有这些配色网站,也能让你的PPT配色美到极致

    很多小伙伴在做PPT的时候,都会被PPT的配色难倒.看到各种非常好看的颜色总是想要将其用在自己的PPT中,可是却发现,颜色和颜色之间完全不搭,自己的PPT也变得丑到不像样. 别担心,今天将分享几个非常 ...

  2. HGAME 2020 misc

    week1 每日推荐 拿到Wireshark capture file后,按常规思路,用foremost命令拿到一个加密的压缩包,之后文件->导出对象->http,看到最大的一个文件里面最 ...

  3. linux下的apue.3e安装[Unix环境高级编程]

    近期正在看<Unix环境高级编程>一书,目前看了20多页,书中 有几个c的例程,于是想着,也让它运行一下,看看是否跟描述相符,做开发人员本应该这样,于是敲了一部分代码,然后编译,发现并不能 ...

  4. [Bug合集] java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent

    出现场景: 在一个Button中定义了onclick属性,值为startChat. 在Activity中定义一个方法. public void startChat(View view){} 运行时,点 ...

  5. 计算机二级-C语言-字符数字转化为整型数字。形参与实参类型相一致。double类型的使用。

    //函数fun功能:将a和b所指的两个字符串分别转化成面值相同的整数,并进行相加作为函数值返回,规定只含有9个以下数字字符. //重难点:字符数字转化为整型数字. #include <stdio ...

  6. 图解 Kafka 水印备份机制

    高可用是很多分布式系统中必备的特征之一,Kafka 日志的高可用是通过基于 leader-follower 的多副本同步实现的,每个分区下有多个副本,其中只有一个是 leader 副本,提供发送和消费 ...

  7. CNCF 宣布 TUF 毕业 | 云原生生态周报 Vol. 33

    作者 | 孙健波.汪萌海.陈有坤.李鹏 业界要闻 CNCF 宣布 TUF 毕业 CNCF 宣布 TUF(The update Framework)项目正式毕业,成为继 Kubernetes.Preme ...

  8. oracle学习笔记(十四) 数据库对象 索引 视图 序列 同义词

    数据库对象 用户模式:指数据库用户所创建和存储数据对象的统称.在访问其它用户模式的数据库对象时需加上用户模式. 如:scott.emp, scott.dept等. 数据库对象包括:表.视图.索引.序列 ...

  9. php之新的开始---php的相关及其环境搭建

    写在前面:首先,感谢“奇矩”给的这次机会,不论结果如何,我都会尽我最大的努力! 在今晚之前,如果说,我与php的接触是停留在知道的阶段,那今晚过后,我想我算是认识了php.php作为一门编程语言,已经 ...

  10. PTA----7-3树的遍历

    7-3 树的遍历 (25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数 ...