[ffmpeg] 解码

c 复制代码
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
}

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

int main(int argc, char* argv[])
{
	// 分割 h264 存入 AVPacket
	string filename = "test.h264";
	ifstream ifs(filename, ios::binary);
	if (!ifs) return -1;
	unsigned char inbuf[4096] = { 0 };

	AVCodecID codec_id = AV_CODEC_ID_H264;

	// 1. 找解码器
	auto codec = avcodec_find_decoder(codec_id);
	// 2. 创建上下文
	auto c = avcodec_alloc_context3(codec);
	// 3. 打开上下文
	avcodec_open2(c, NULL, NULL);

	// 分割上下文
	auto parser = av_parser_init(codec_id);

	auto pkt = av_packet_alloc();
	auto frame = av_frame_alloc();
	while (!ifs.eof())
	{
		ifs.read((char*)inbuf, sizeof(inbuf));
		int data_size = ifs.gcount(); // 读取的字节数
		if (data_size <= 0) break;
		// 通过 0001截断
		auto data = inbuf;
		while (data_size > 0)
		{
			// 通过 0001 截取输出到 AVPacket 返回帧大小
			int ret = av_parser_parse2(parser, c,
				&pkt->data, &pkt->size, // 输出
				data, data_size, // 输入
				AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
			data += ret;
			data_size -= ret; // 已处理
			if (pkt->size)
			{
				//cout << pkt->size << " " << flush;
				// 发送 packet 到解码线程
				ret = avcodec_send_packet(c, pkt);
				if (ret < 0)
				{
					break;
				}
				// 获取多帧解码数据
				while (ret >= 0)
				{
					// 每次会调用 av_frame_unref
					ret = avcodec_receive_frame(c, frame);
					if (ret < 0)
						break;
					cout << frame->format << " " << flush;
				}
			}
		}
	}
	av_packet_free(&pkt);

	// 取出缓存数据
	int ret = avcodec_send_packet(c, NULL);
	while (ret >= 0)
	{
		ret = avcodec_receive_frame(c, frame);
		if (ret < 0)
			break;
		cout << frame->format << "-" << flush;
	}

	av_parser_close(parser);
	avcodec_free_context(&c);
	av_frame_free(&frame);
	av_packet_free(&pkt);
	return 0;
}
相关推荐
小徐敲java19 小时前
视频推流服务器与FFmpeg 安装配置
服务器·ffmpeg·音视频
假装我不帅1 天前
ffmpeg操作mp3去除封面信息
ffmpeg
封奚泽优3 天前
下载网页中的.m3u8视频文件
ffmpeg
Vertira3 天前
win10/10 下载并安装ffmpeg.exe 的官方详细方法 (已解决)
ffmpeg
xmRao3 天前
Qt+FFmpeg 实现音频重采样
qt·ffmpeg·音视频
Evonso4 天前
静态编译的ffmpeg用法
ffmpeg
_chirs6 天前
编译不依赖动态库的FFMPEG(麒麟国防 V10)
arm开发·ffmpeg
熊猫钓鱼>_>6 天前
从零到一:打造“抗造” Electron 录屏神器的故事
前端·javascript·ffmpeg·electron·node·录屏·record
UpYoung!6 天前
【格式转换工具】专业级多媒体格式转换解决方案——Freemake Video Converter 完全指南:轻量化视频剪辑媒体格式转换
ffmpeg·短视频·实用工具·开源工具·多媒体格式转换·运维必备·视频转换格式
试剂小课堂 Pro7 天前
Ald-PEG-Ald:丙醛与聚乙二醇两端连接的对称分子
java·c语言·c++·python·ffmpeg