FFmpeg部分数据结构简介

本文介绍了FFmpeg中的关键数据结构,如AVFormatContext(封装格式上下文)、AVInputFormat(封装格式)、AVStream(流)、AVCodecContext(编码上下文)和AVCodec(编解码器),以及AVPacket和AVFrame的作用,特别提到了解码后的数据如何通过sws_scale()函数处理像素格式转换。
摘要由CSDN通过智能技术生成

FFmpeg部分数据结构简介

AVFormatContext

封装格式上下文结构体,也是统称全局的结构体,保存了视频文件封装格式相关信息

AVInputFormat

每种封装格式(例如:FLV、MKV、MP4、AVI等)对应一个该结构体

AVStream

视频文件中每个视频(音频)流对应一个该结构体

AVCodecContext

编码器上下文结构体,保存了视频(音频)编解码相关的信息

AVCodec

每种视频(音频)编解码器(例如:H.264解码器)对应一个该结构体。

AVPacket

存储一帧压缩编码数据

AVFrame

存储一帧解码后像素(采样)数据。

FFmpeg数据结构分析

AVFormatContext

iformat:输入视频的AVInputFormat
nb_streams:输入视频的AVStream个数
streams:输入视频的AVStream[]数组
druation:输入视频的时长(以微秒为单位)
bit_rate:输入视频码率

AVInputFormat

name:封装格式名称
long_name:封装格式的长名称
extensions:封装格式的扩展名
id:封装格式的ID
一些封装格式处理的接口函数

AVStream

id:序号
codec:该流对应的AVCodecContext
time_base:该流的时基
r_frame_rate:该流的帧率
AVCodecContext
codec:编解码器的AVCodec
width,height:图像的宽高(只针对视频)
pix_fmt:像素格式(只针对视频)
sample_rae:采样率(只针对音频)
channels:声道数(只针对音频)
sample_fmt:采样格式(只针对音频)

AVCodec

name:编解码器名称
long_name:编解码器的长名称
type:编解码器类型
id:编解码器ID
一些编解码的接口函数

AVPacket

pts:显示时间戳
dts:解码时间戳
data:压缩编码数据
size:压缩编码数据大小
stream_index:所属的AVStream

AVFrame

data:解码后的图像像素数据(音频采样数据)
linesize:对视频来说是图像中一行像素的大小;对应音频来说是整个音频帧的大小
width,height:图像的宽高(只针对视频)
key_frame:是否为关键帧(只针对视频)
pict_type:帧类型(值针对视频)。例如:I,P,B
补充知识
解码后的数据为什么要经过sws_scale()函数处理?
解码后YUV格式的视频像素数据保存在AVFrame的data[0]、data[1]、data[2]中。但是这些像素值并不是连续存储的,每行有效像素之后存储了一些无效像素。以亮度Y数据为例,data[0]中一共包含了linesize[0]*height个数据。但是出于优化等方面的考虑,linesize[0]实际上并不等于宽度width,而是一个比宽度大一些的值。因此需要使用sws_scale()进行转换。转换后去除了无效数据,width和linesize[0]取值相等。

实际去操作代码,可以更加深入了解结构数据位置

#include <stdio.h>
#include <string.h>

extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/dict.h"
};

#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avcodec.lib")

int main()
{
	AVFormatContext *pFormatCtx = NULL;
	AVCodecContext *pCodecCtx = NULL;
	AVCodec *pCodec;
	AVDictionaryEntry *dict = NULL;
	
	int iHour, iMinute, iSecond, iTotalSeconds;//HH:MM:SS
	int videoIndex, audioIndex;

	char *fileName = "bad.mp4";
	//char *fileName = "Titanic.ts";

	av_register_all();//注册所有组件

	if (avformat_open_input(&pFormatCtx, fileName, NULL, NULL) != 0)//打开输入视频文件
	{
		printf("Couldn't open input stream.\n");
		return -1;
	}

	if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
	{
		printf("Couldn't find stream information.\n");
		return -1;
	}

	videoIndex = -1;
	for (int i = 0; i < pFormatCtx->nb_streams/*视音频流的个数*/; i++)
	{
		if (pFormatCtx->streams[i]/*视音频流*/->codec->codec_type == AVMEDIA_TYPE_VIDEO)//查找音频
		{
			videoIndex = i;
			break;
		}
	}
	if (videoIndex == -1)
	{
		printf("Couldn't find a video stream.\n");
		return -1;
	}

	pCodecCtx = pFormatCtx->streams[videoIndex]->codec;	//指向AVCodecContext的指针
	pCodec = avcodec_find_decoder(pCodecCtx->codec_id);	//指向AVCodec的指针.查找解码器
	if (pCodec == NULL)
	{
		printf("Codec not found.\n");
		return -1;
	}
	//打开解码器
	if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
	{
		printf("Could not open codec.\n");
		return -1;
	}

	audioIndex = -1;
	for (int i = 0; i < pFormatCtx->nb_streams; i++)
	{
		if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
		{
			audioIndex = i;
			break;
		}
	}
	if (audioIndex == -1)
	{
		printf("Couldn't find a audio stream.\n");
		return -1;
	}

	

	//打印结构体信息

	puts("AVFormatContext信息:");
	puts("---------------------------------------------");
	printf("文件名:%s\n", pFormatCtx->filename);
	iTotalSeconds = (int)pFormatCtx->duration/*微秒*/ / 1000000;
	iHour = iTotalSeconds / 3600;//小时
	iMinute = iTotalSeconds % 3600 / 60;//分钟
	iSecond = iTotalSeconds % 60;//秒
	printf("持续时间:%02d:%02d:%02d\n", iHour, iMinute, iSecond);
	printf("平均混合码率:%d kb/s\n", pFormatCtx->bit_rate / 1000);
	printf("视音频个数:%d\n", pFormatCtx->nb_streams);
	puts("---------------------------------------------");

	puts("AVInputFormat信息:");
	puts("---------------------------------------------");
	printf("封装格式名称:%s\n", pFormatCtx->iformat->name);
	printf("封装格式长名称:%s\n", pFormatCtx->iformat->long_name);
	printf("封装格式扩展名:%s\n", pFormatCtx->iformat->extensions);
	printf("封装格式ID:%d\n", pFormatCtx->iformat->raw_codec_id);
	puts("---------------------------------------------");

	puts("AVStream信息:");
	puts("---------------------------------------------");
	printf("视频流标识符:%d\n", pFormatCtx->streams[videoIndex]->index);
	printf("音频流标识符:%d\n", pFormatCtx->streams[audioIndex]->index);
	printf("视频流长度:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
	printf("音频流长度:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
	puts("---------------------------------------------");

	puts("AVCodecContext信息:");
	puts("---------------------------------------------");
	printf("视频码率:%d kb/s\n", pCodecCtx->bit_rate / 1000);
	printf("视频大小:%d * %d\n", pCodecCtx->width, pCodecCtx->height);
	puts("---------------------------------------------");

	puts("AVCodec信息:");
	puts("---------------------------------------------");
	printf("视频编码格式:%s\n", pCodec->name);
	printf("视频编码详细格式:%s\n", pCodec->long_name);
	puts("---------------------------------------------");

	printf("视频时长:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
	printf("音频时长:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
	printf("音频采样率:%d\n", pFormatCtx->streams[audioIndex]->codec->sample_rate);
	printf("音频信道数目:%d\n", pFormatCtx->streams[audioIndex]->codec->channels);

	puts("AVFormatContext元数据:");
	puts("---------------------------------------------");
	while (dict = av_dict_get(pFormatCtx->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
	{
		printf("[%s] = %s\n", dict->key, dict->value);
	}
	puts("---------------------------------------------");

	puts("AVStream视频元数据:");
	puts("---------------------------------------------");
	dict = NULL;
	while (dict = av_dict_get(pFormatCtx->streams[videoIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
	{
		printf("[%s] = %s\n", dict->key, dict->value);
	}
	puts("---------------------------------------------");

	puts("AVStream音频元数据:");
	puts("---------------------------------------------");
	dict = NULL;
	while (dict = av_dict_get(pFormatCtx->streams[audioIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
	{
		printf("[%s] = %s\n", dict->key, dict->value);
	}
	puts("---------------------------------------------");


	av_dump_format(pFormatCtx, -1, fileName, 0);
	printf("\n\n编译信息:\n%s\n\n", avcodec_configuration());


	avcodec_close(pCodecCtx);
	avformat_close_input(&pFormatCtx);
	return 0;
}

  • 29
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baoyu45585

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值