avcodec_decode_video2函数
转自 https://www.xuebuyuan.com/2156374.html
该函数的作用是实现压缩视频的解码。在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 avpkt
AVPacket 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));
}
} else
ret = 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进行解码的任务。
avcodec_decode_video2函数的更多相关文章
- FFmpeg命令行工具和批处理脚本进行简单的音视频文件编辑
FFmpeg_Tutorial FFmpeg工具和sdk库的使用demo 一.使用FFmpeg命令行工具和批处理脚本进行简单的音视频文件编辑 1.基本介绍 对于每一个从事音视频技术开发的工程师,想必没 ...
- (转) 从ffmpeg中提取出YUV数据
有时需要从ffmpeg中提取出YUV数据用作预览,另存什么的. ffmpeg是先解码成YUV, 再以这个YUV作为输入进行编码,所以YUV数据有两种: 解码后的YUV数据, 以及 编码重建的YUV ...
- FFmpeg 入门(5):视频同步
本文转自:FFmpeg 入门(5):视频同步 | www.samirchen.com 视频如何同步 在之前的教程中,我们已经可以开始播放视频了,也已经可以开始播放音频了,但是视频和音频的播放还未同步, ...
- FFmpeg 入门(4):线程分治
本文转自:FFmpeg 入门(4):线程分治 | www.samirchen.com 概览 上一节教程中,我们使用 SDL 的音频相关的函数来支持音频播放.SDL 起了一个线程来在需要音频数据的时候去 ...
- FFmpeg 入门(1):截取视频帧
本文转自:FFmpeg 入门(1):截取视频帧 | www.samirchen.com 背景 在 Mac OS 上如果要运行教程中的相关代码需要先安装 FFmpeg,建议使用 brew 来安装: // ...
- 关于ffmpeg(libav)解码视频最后丢帧的问题
其实最初不是为了解决这个问题而来的,是Peter兄给我的提示解决另一个问题却让我误打误撞解决了另外一个问题之后也把这个隐藏了很久的bug找到(之前总是有一些特别短的视频产生不知所措还以为是视频素材本身 ...
- FFmpeg学习2:解码数据结构及函数总结
在上一篇文章中,对FFmpeg的视频解码过程做了一个总结.由于才接触FFmpeg,还是挺陌生的,这里就解码过程再做一个总结. 本文的总结分为以下两个部分: 数据读取,主要关注在解码过程中所用到的FFm ...
- 零基础学习视频解码之FFMpeg中比较重要的函数以及数据结构
http://www.cnblogs.com/tanlon/p/3879081.html 在正式开始解码练习前先了解下关于FFmpeg中比较重要的函数以及数据结构. 1. 数据结构: (1) AVF ...
- ffmpeg结构体以及函数介绍(三)
1 AVPacket typedef struct AVPacket { /** * Presentation timestamp in AVStream->time_base units; t ...
随机推荐
- hdoj4003 (树形dp+分组背包)
题目链接:https://vjudge.net/problem/HDU-4003 题意:给一棵边权树,在树根s有m个人,要通过m个人遍历到所有点,一个人经过一条边花费为边的权值,求最小花费(可以走已经 ...
- [转帖]redis监控工具汇总
redis监控工具汇总 https://www.cnblogs.com/felixzh/p/11170143.html redis-stat redis-stat是一个比较有名的redis指标可视化的 ...
- Oracle快速运行一指禅
对于oracle数据库下的企业级应用开发,经常会使用到新建用户,新建表空间以及数据的迁移工作.虽然目前互联网存在很多单个问题的解决方案,但是比较零散,本博文结合研发兄弟们的实际现状,提供一套完整初 ...
- centos7 虚拟机 A start job is running for /etc/rc.d/rc.local Comp。。。
一直卡这F5查看日志,最后一行出现A start job is running for /etc/rc.d/rc.local Comp... 原因是rc.local权限设错了解决方法:1.进入单用户模 ...
- 微信公众号通过用户授权获取用户基本信息java版
公司需要开发一个微信公众号,要求用户通过公众号登录公司网站时候自动获取用户的基本信息,在网上查资料发现大部分都是直接copy微信公众平台的开发文档,感觉还是介绍的不是太小白,所以为了方便大家也为了自己 ...
- Centos7下永久修改mysql5.6最大连接数
由于解除系统限制,设置最大连接数时,量力而行.https://blog.csdn.net/five3/article/details/79671317
- 并不对劲的CF1236D&E:Alice&Doll&UnfairGame
CF1236D Alice&Doll 题目描述 有一个机器人在一个\(n\times m\)的有\(k\)个障碍网格上移动,上北下南左西右东. 它一开始在第一行第一列,面朝东边.它在每个格子上 ...
- Foo Fighters CodeForces - 1148F
大意: 给定$n$个二元组$(val_i,mask_i)$. 保证$\sum val_i$不为$0$. 要求选择一个数$s$, 对于每个二元组$(val_i,mask_i)$, 若$s\& m ...
- IntelliJ IDEA 统一设置编码为utf-8编码
问题一: File->Settings->Editor->File Encodings 问题二: File->Other Settings->Default Settin ...
- python计算出现错误
用python计算39.8-0.1得出的结果是39.699999999999996 其他数字计算正确,唯独这个计算错误. 原因: 中文解释: https://docs.python.org/zh-cn ...