FFMPeg代码分析:avcodec_decode_video2函数

2023-11-23 04:58

本文主要是介绍FFMPeg代码分析:avcodec_decode_video2函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

该函数的作用是实现压缩视频的解码。在avcodec.h中的声明方式如下:

int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt);
待解码的数据保存在avpkt->data中,大小为avpkt->size;解码完成后,picture用于保存输出图像数据。

该方法的各个参数:

AVCodecContext *avctx:编解码上下文环境,定义了编解码操作的一些细节;

AVFrame *picture:输出参数;传递到该方法的对象本身必须在外部由av_frame_alloc()分配空间,而实际解码过后的数据储存区将由AVCodecContext.get_buffer2()分配;AVCodecContext.refcounted_frames表示该frame的引用计数,当这个值为1时,表示有另外一帧将该帧用作参考帧,而且参考帧返回给调用者;当参考完成时,调用者需要调用av_frame_unref()方法解除对该帧的参考;av_frame_is_writable()可以通过返回值是否为1来验证该帧是否可写。

int *got_picture_ptr:该值为0表明没有图像可以解码,否则表明有图像可以解码;

const AVPacket *avpkt:输入参数,包含待解码数据。

int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr,const AVPacket *avpkt)
{AVCodecInternal *avci = avctx->internal;int ret;// copy to ensure we do not change avpktAVPacket tmp = *avpkt;if (!avctx->codec)return AVERROR(EINVAL);if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");return AVERROR(EINVAL);}*got_picture_ptr = 0;if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))return AVERROR(EINVAL);avcodec_get_frame_defaults(picture);if (!avctx->refcounted_frames)av_frame_unref(&avci->to_free);if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {int did_split = av_packet_split_side_data(&tmp);apply_param_change(avctx, &tmp);avctx->pkt = &tmp;if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,&tmp);else {ret = avctx->codec->decode(avctx, picture, got_picture_ptr,&tmp);picture->pkt_dts = avpkt->dts;if(!avctx->has_b_frames){av_frame_set_pkt_pos(picture, avpkt->pos);}//FIXME these should be under if(!avctx->has_b_frames)/* get_buffer is supposed to set frame parameters */if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {if (!picture->sample_aspect_ratio.num)    picture->sample_aspect_ratio = avctx->sample_aspect_ratio;if (!picture->width)                      picture->width               = avctx->width;if (!picture->height)                     picture->height              = avctx->height;if (picture->format == AV_PIX_FMT_NONE)   picture->format              = avctx->pix_fmt;}}add_metadata_from_side_data(avctx, picture);emms_c(); //needed to avoid an emms_c() call before every return;avctx->pkt = NULL;if (did_split) {av_packet_free_side_data(&tmp);if(ret == tmp.size)ret = avpkt->size;}if (ret < 0 && picture->data[0])av_frame_unref(picture);if (*got_picture_ptr) {if (!avctx->refcounted_frames) {avci->to_free = *picture;avci->to_free.extended_data = avci->to_free.data;memset(picture->buf, 0, sizeof(picture->buf));}avctx->frame_number++;av_frame_set_best_effort_timestamp(picture, guess_correct_pts(avctx, picture->pkt_pts, picture->pkt_dts));}} elseret = 0;/* many decoders assign whole AVFrames, thus overwriting extended_data;* make sure it's set correctly */picture->extended_data = picture->data;return ret;
}
在该函数中,调用了ret = avctx->codec->decode(avctx, picture, got_picture_ptr, &tmp);实现解码功能。在当前demo中,codec类型为ff_hevc_decoder,decode指针指向的函数为hevc_decode_frame。ff_hevc_decoder的定义如下:

AVCodec ff_hevc_decoder = {.name                  = "hevc",.long_name             = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),.type                  = AVMEDIA_TYPE_VIDEO,.id                    = AV_CODEC_ID_HEVC,.priv_data_size        = sizeof(HEVCContext),.priv_class            = &hevc_decoder_class,.init                  = hevc_decode_init,.close                 = hevc_decode_free,.decode                = hevc_decode_frame,.flush                 = hevc_decode_flush,.update_thread_context = hevc_update_thread_context,.init_thread_copy      = hevc_init_thread_copy,.capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
};
解码函数:

static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,AVPacket *avpkt)
{int ret;HEVCContext *s = avctx->priv_data;if (!avpkt->size) {ret = ff_hevc_output_frame(s, data, 1);if (ret < 0)return ret;*got_output = ret;return 0;}s->ref = NULL;ret = decode_nal_units(s, avpkt->data, avpkt->size);if (ret < 0)return ret;/* verify the SEI checksum */if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&avctx->err_recognition & AV_EF_EXPLODE &&s->is_md5) {ret = verify_md5(s, s->ref->frame);if (ret < 0) {ff_hevc_unref_frame(s, s->ref, ~0);return ret;}}s->is_md5 = 0;if (s->is_decoded) {av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);s->is_decoded = 0;}if (s->output_frame->buf[0]) {av_frame_move_ref(data, s->output_frame);*got_output = 1;}return avpkt->size;
}
熟悉编解码标准的同学都知道,H.264和HEVC都定义了网络抽象层NAL来执行传输层的任务,每一个NAL单元都按照规定保存了某些语法元素。函数decode_nal_units执行了对这些NAL单元进行解析并对NAL的下一层视频编码层VCL进行解码的任务。

这篇关于FFMPeg代码分析:avcodec_decode_video2函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/415501

相关文章

Python实现一键PDF转Word(附完整代码及详细步骤)

《Python实现一键PDF转Word(附完整代码及详细步骤)》pdf2docx是一个基于Python的第三方库,专门用于将PDF文件转换为可编辑的Word文档,下面我们就来看看如何通过pdf2doc... 目录引言:为什么需要PDF转Word一、pdf2docx介绍1. pdf2docx 是什么2. by

Python函数返回多个值的多种方法小结

《Python函数返回多个值的多种方法小结》在Python中,函数通常用于封装一段代码,使其可以重复调用,有时,我们希望一个函数能够返回多个值,Python提供了几种不同的方法来实现这一点,需要的朋友... 目录一、使用元组(Tuple):二、使用列表(list)三、使用字典(Dictionary)四、 使

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Spring Security介绍及配置实现代码

《SpringSecurity介绍及配置实现代码》SpringSecurity是一个功能强大的Java安全框架,它提供了全面的安全认证(Authentication)和授权(Authorizatio... 目录简介Spring Security配置配置实现代码简介Spring Security是一个功能强

通过cmd获取网卡速率的代码

《通过cmd获取网卡速率的代码》今天从群里看到通过bat获取网卡速率两段代码,感觉还不错,学习bat的朋友可以参考一下... 1、本机有线网卡支持的最高速度:%v%@echo off & setlocal enabledelayedexpansionecho 代码开始echo 65001编码获取: >

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o

SpringBoot实现Kafka动态反序列化的完整代码

《SpringBoot实现Kafka动态反序列化的完整代码》在分布式系统中,Kafka作为高吞吐量的消息队列,常常需要处理来自不同主题(Topic)的异构数据,不同的业务场景可能要求对同一消费者组内的... 目录引言一、问题背景1.1 动态反序列化的需求1.2 常见问题二、动态反序列化的核心方案2.1 ht

IDEA实现回退提交的git代码(四种常见场景)

《IDEA实现回退提交的git代码(四种常见场景)》:本文主要介绍IDEA实现回退提交的git代码(四种常见场景),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.已提交commit,还未push到远端(Undo Commit)2.已提交commit并push到

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.