【FFmpeg】avcodec_receive_packet函数

news/2024/10/22 18:41:27/

目录

  • 1.avcodec_receive_packet

FFmpeg相关记录:

示例工程:
【FFmpeg】调用ffmpeg库实现264软编
【FFmpeg】调用ffmpeg库实现264软解
【FFmpeg】调用ffmpeg库进行RTMP推流和拉流
【FFmpeg】调用ffmpeg库进行SDL2解码后渲染

流程分析:
【FFmpeg】编码链路上主要函数的简单分析
【FFmpeg】解码链路上主要函数的简单分析

结构体分析:
【FFmpeg】AVCodec结构体
【FFmpeg】AVCodecContext结构体
【FFmpeg】AVStream结构体
【FFmpeg】AVFormatContext结构体
【FFmpeg】AVIOContext结构体
【FFmpeg】AVPacket结构体

函数分析:
【通用】
【FFmpeg】avcodec_find_encoder和avcodec_find_decoder
【FFmpeg】关键结构体的初始化和释放(AVFormatContext、AVIOContext等)
【FFmpeg】avcodec_open2函数
【FFmpeg】内存分配和释放(av_malloc、av_realloc等)

【推流】
【FFmpeg】avformat_open_input函数
【FFmpeg】avformat_find_stream_info函数
【FFmpeg】avformat_alloc_output_context2函数
【FFmpeg】avio_open2函数
【FFmpeg】avformat_write_header函数
【FFmpeg】av_write_frame函数

【编码】
【FFmpeg】avcodec_send_frame函数

【解码】
【FFmpeg】avcodec_send_packet函数
【FFmpeg】avcodec_receive_frame函数

1.avcodec_receive_packet

/*** Read encoded data from the encoder.** @param avctx codec context* @param avpkt This will be set to a reference-counted packet allocated by the*              encoder. Note that the function will always call*              av_packet_unref(avpkt) before doing anything else.* @retval 0               success* @retval AVERROR(EAGAIN) output is not available in the current state - user must*                         try to send input* @retval AVERROR_EOF     the encoder has been fully flushed, and there will be no*                         more output packets* @retval AVERROR(EINVAL) codec not opened, or it is a decoder* @retval "another negative error code" legitimate encoding errors*/
// 从encoder当中读取已经编码的数据
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);

函数的定义位于libavcodec\encode.c中,如下所示,其中核心的函数是av_packet_move_ref,用于获取已经编码的pkt

int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
{AVCodecInternal *avci = avctx->internal;int ret;// 减少avpkt的引用次数av_packet_unref(avpkt);if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))return AVERROR(EINVAL);// buffer_pkt是编解码内部的数据缓冲区,用来存放压缩之后的数据if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {// 将buffer_pkt的引用给到avpkt上,同时将buffer_pkt的信息清零av_packet_move_ref(avpkt, avci->buffer_pkt); } else {// 如果缓冲区没有数据,需要进行编码操作ret = encode_receive_packet_internal(avctx, avpkt);if (ret < 0)return ret;}return 0;
}

这里的av_packet_move_ref的作用是取出avci这个内部(internal)结构体中缓冲区的数据,并且赋值给到avpkt中,随后将avci->buffer_pkt中的信息设置为默认值。如果缓冲区中没有数据,则需要重新进行编码操作,使用encode_receive_packet_internal函数,在其他文中记录过,不再多记录
av_packet_move_ref的定义如下,先进行内容的拷贝,随后将src设置为初始值

void av_packet_move_ref(AVPacket *dst, AVPacket *src)
{*dst = *src;get_packet_defaults(src);
}

get_packet_defaults的定义如下,就是将AVPacket全部设置成为默认值

static void get_packet_defaults(AVPacket *pkt)
{memset(pkt, 0, sizeof(*pkt));pkt->pts             = AV_NOPTS_VALUE;pkt->dts             = AV_NOPTS_VALUE;pkt->pos             = -1;pkt->time_base       = av_make_q(0, 1);
}

CSDN : https://blog.csdn.net/weixin_42877471
Github : https://github.com/DoFulangChen


http://www.ppmy.cn/news/1501258.html

相关文章

帆软报表学习官网 中文

帆软报表学习官网&#xff1a;快速入门指南- FineReport帮助文档 - 全面的报表使用教程和学习资料

前后端分离的开发模式+YAPI接口文档

博客主页&#xff1a;音符犹如代码系列专栏&#xff1a;JavaWeb关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ 早期的开发模式&#xff1a;前后端混合开发 在这种模式下开发下&#xff0c;…

C++笔记---缺省参数和函数重载

1. 缺省参数 1.1 定义 缺省参数是声明或定义函数时为函数的参数指定一个缺省值&#xff08;默认值&#xff09;。在调用该函数时&#xff0c;如果没有指定实参 则采用该形参的缺省值&#xff0c;否则使用指定的实参&#xff0c;缺省参数分为全缺省和半缺省参数。 void Func(…

C语言100基础拔高题(3)

1.利用递归函数调用方式&#xff0c;将所输入的5个字符&#xff0c;以相反顺序打印出来。 解题思路&#xff1a;通过反复调用一个打印最后一个元素的函数&#xff0c;来实现此功能。源代码如下: #include<stdio.h> void oposize(char str[], int len); int main() {//利…

@Transactional使用的注意事项

在项目中涉及到CRUD操作时&#xff0c;一般都会在方法上添加该注解&#xff0c;以为加上Transactional&#xff0c;Spring就可以自动帮我们进行事务的开启、提交 有一个很多人都会犯的误区&#xff1a; 将Spring事务与Transactional划上了等号&#xff0c;只要有数据库相关操作…

vue3 命令运行窗口暴露网络地址,以及修改端口号

一般情况下这里的地址是隐藏的 这里加上 --host 可以暴露网络地址&#xff0c;再加上--port --8080 就可以将端口号修改为8080&#xff08;修改后边的数字就可以修改为你想要的端口号&#xff09;

快速安装torch-gpu和Tensorflow-gpu(自用,Ubuntu)

要更详细的教程可以参考Tensorflow PyTorch 安装&#xff08;CPU GPU 版本&#xff09;&#xff0c;这里是有基础之后的快速安装。 一、Pytorch 安装 conda create -n torch_env python3.10.13 conda activate torch_env conda install cudatoolkit11.8 -c nvidia pip ins…