FFmpeg部分数据结构简介

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;
}
相关推荐
wclass-zhengge1 小时前
数据结构与算法篇(树 - 常见术语)
数据结构·算法
夜雨翦春韭1 小时前
【代码随想录Day31】贪心算法Part05
java·数据结构·算法·leetcode·贪心算法
C++忠实粉丝7 小时前
前缀和(8)_矩阵区域和
数据结构·c++·线性代数·算法·矩阵
ZZZ_O^O7 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
代码雕刻家8 小时前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
Kalika0-010 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
代码雕刻家10 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
头快撑不住了11 小时前
ffmpeg源码分析(七)结构体之AVStream
ffmpeg
小字节,大梦想11 小时前
【C++】二叉搜索树
数据结构·c++
我是哈哈hh12 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝