FFmpeg之AVPacket
花满楼原创
AVPacket,是压缩数据的结构体(解码前或编码后的结构体)。
本文介绍FFmepg中常见结构AVPacekt,尽量用具体值来理解。
整个用于调试的代码可以这样写:
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
void show_frame(const char* filepath) {
av_register_all();
av_log_set_level(AV_LOG_DEBUG);
AVFormatContext* formatContext = avformat_alloc_context();
int status = 0;
int success = 0;
int videostreamidx = -1;
AVCodecContext* codecContext = NULL;
status = avformat_open_input(&formatContext, filepath, NULL, NULL);
if (status == 0) {
status = avformat_find_stream_info(formatContext, NULL);
if (status >= 0) {
for (int i = 0; i < formatContext->nb_streams; i ++) {
if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videostreamidx = i;
break;
}
}
if (videostreamidx > -1) {
AVStream* avstream = formatContext->streams[videostreamidx];
codecContext = avstream->codec;
AVCodec* codec = avcodec_find_decoder(codecContext->codec_id);
if (codec) {
status = avcodec_open2(codecContext, codec, NULL);
if (status == 0) {
success = 1;
}
}
}
}
else {
av_log(NULL, AV_LOG_DEBUG, "avformat_find_stream_info error\n");
}
if (success) {
av_dump_format(formatContext, 0, filepath, 0);
int gotframe = 0;
AVFrame* frame = av_frame_alloc();
int decodelen = 0;
int limitcount = 10;
int pcindex = 0;
while (pcindex < limitcount) {
AVPacket packet;
av_init_packet( &packet );
status = av_read_frame(formatContext, &packet);
if (status < 0) {
if (status == AVERROR_EOF) {
av_log(NULL, AV_LOG_DEBUG, "read end for file\n");
}
else {
av_log(NULL, AV_LOG_DEBUG, "av_read_frame error\n");
}
av_packet_unref(&packet);
break;
}
else {
if (packet.stream_index == videostreamidx) {
decodelen = avcodec_decode_video2(codecContext, frame, &gotframe, &packet);
if (decodelen > 0 && gotframe) {
av_log(NULL, AV_LOG_DEBUG, "got one avframe, pcindex=%d\n", pcindex);
}
}
}
av_packet_unref(&packet);
pcindex ++;
}
av_frame_free(&frame);
}
avformat_close_input(&formatContext);
}
avformat_free_context(formatContext);
}
int main(int argc, char *argv[])
{
show_frame("moments.mp4");
return 0;
}
av_read_frame函数可以取得一个AVPacket,所以在调用这个函数的地方下个断点,看一下AVPacet长什么样子。
编译脚本还是沿用之前介绍的makefile或直接用gcc来编译。
在没有调用av_read_frame前,AVPacket中的变量值:
调用av_read_frame后,AVPacket中的变量:
再一次av_read_frame后:
av_read_frame后,AVPacket也可能没有数据:
AVPacket是压缩数据,一个AVPacket,对于视频最多一帧(NALU),但对于音频就可能多帧。
AVPacket中的变量含义:
pts/dts,显示/解码时间戵,以packet所在的流的time_base为单位。
stream_index,所在流的索引。
data,avpacket拥有的数据。
size,avpacket的数据长度。
duration,avpacket的时长,同样以time_base为单位。
AVPacket结构,在libavcodec/avcodec.h中定义。
FFmpeg之AVPacket的更多相关文章
- FFmpeg 中AVPacket的使用
AVPacket保存的是解码前的数据,也就是压缩后的数据.该结构本身不直接包含数据,其有一个指向数据域的指针,FFmpeg中很多的数据结构都使用这种方法来管理数据. AVPacket的使用通常离不开下 ...
- FFmpeg数据结构AVPacket
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10410320.html 本文基于FFmpeg 4.1版本. 1. 数据结构定义 stru ...
- C/C++音视频库ffmpeg的数据包AVPacket分析
ffmpeg下载地址 http://www.ffmpeg.club/ AVPacket是ffmpeg用来存放编码后的视频帧数据,我们来分析一下这个结构体,先贴出ffmpeg3.2中AVPacket声明 ...
- FFmpeg详解
认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几乎实现了所有当下常见的数据封装格式.多媒体传输协议以及音视频编解码器.因此,对于从事多媒体技术开发的工程师来说,深入研究FFM ...
- 最新FFMPEG解码流程
FFMPEG解码流程: 1. 注册所有容器格式和CODEC: av_register_all() 2. 打开文件: av_open_input_file() 3 ...
- (转)FFMPEG解码H264拼帧简解
http://blog.csdn.net/ikevin/article/details/7649095 H264的I帧通常 0x00 0x00 0x00 0x01 0x67 开始,到下一个帧头开始之前 ...
- FFmpeg基础一
来源:http://blog.csdn.net/chance_yin/article/details/10323441 一.研究数字多媒体,首先要了解几个基本术语(ffmpeg的相关文档几乎都是英文的 ...
- ffmpeg 详解
来源:http://blog.itpub.net/9399028/viewspace-1242300/ FFMPEG详解 认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几 ...
- 【FFMPEG】I,P,B帧和PTS,DTS时间戳的关系
FFmpeg里有两种时间戳:DTS(Decoding Time Stamp)和PTS(Presentation Time Stamp). 顾名思义,前者是解码的时间,后者是显示的时间.要仔细理解这两个 ...
随机推荐
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- gops - Go语言程序查看和诊断工具
想必 Java 的开发者没有不知道或者没用过 jps 这个命令的,这个命令是用来在主机上查看有哪些 Java 程序在运行的. 我刚用 Go 语言程序的时候也很苦恼,我部署在公司服务器上的 Go 程序, ...
- Strategic Game
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- git使用教程之git基础
1 获取Git仓库 在现有目录中初始化仓库 git init 克隆现有的仓库 git clone https://github.com/yangwang12345/node_test.git Git ...
- Tomcat初应用
Tomcat初应用 这里我们自己建立一个html的web资源,然后在tomcat里进行配置,使我们可以通过服务器在浏览器里打开. 第一步:新建html文件,在里面随便输入几个字符串如:新建txt文件- ...
- SQL表连接查询(inner join(join)、full join、left join、right join、cross join)
下面列出了您可以使用的 JOIN 类型,以及它们之间的差异. JOIN: 如果表中有至少一个匹配,则返回行(join=inner join) LEFT JOIN: 即使右表中没有匹配,也从左表返回所有 ...
- vue 响应式原理
Vue 采用声明式编程替代过去的类 Jquery 的命令式编程,并且能够侦测数据的变化,更新视图.这使得我们可以只关注数据本身,而不用手动处理数据到视图的渲染,避免了繁琐的 DOM 操作,提高了开发效 ...
- Python学习--字典
在Python中通过名字来引用值的数据结构称为映射(mapping).字典是Python中唯一内建(Python解释器本身支持,不需要import)的映射类型.字典中的值没有特殊的顺序,都存储在一个特 ...
- C#获取存储过程的 Return返回值和Output输出参数值
1.获取Return返回值 程序代码 //存储过程//Create PROCEDURE MYSQL// @a int,// @b int//AS// return @a + ...
- Python的classmethod和staticmethod区别
静态方法(staticmethod) 类方法(classmethod) 静态方法和类方法都可以通过类名.方法名或者实例.方法访问. #-*- coding:utf8 -*- class A(objec ...