利用ffmpeg来进行视频解码的完整示例代码(H.264)

int Decode() 
{ 
	FILE * inpf; 

	int nWrite; 
	int i,p; 
	int nalLen; 
	unsigned char* Buf; 
	int got_picture, consumed_bytes; 
	unsigned char *DisplayBuf; 
	DisplayBuf=(unsigned char *)malloc(60000); 

	char outfile[] = "test.pgm"; 

	//1.打开输入文件 
	inpf = fopen("test.264", "rb"); 

	//outf = fopen("out.yuv", "wb"); 

	if(!inpf) 
	{ 
		goto Decodereturn; 
	} 

	nalLen = 0; 
	Buf = (unsigned char*)calloc ( 1000000, sizeof(char)); //准备解码文件缓冲 

	//2.注册解码器,并且找到H264解码器 
	avcodec_init(); 
	avcodec_register_all(); 
	codec = avcodec_find_decoder(CODEC_ID_H264); 

	if (!codec) { 
		return 0; 
	} 
	//allocate codec context 
	//分配解码器内存 
	c = avcodec_alloc_context(); 

	if(!c){ 
		return 0; 
	} 
	//open codec 
	//3.打开解码器 
	if (avcodec_open(c, codec) < 0) { 
		return 0; 
	} 

	//allocate frame buffer 
	//分配解码器用的帧缓冲 
	picture = avcodec_alloc_frame(); 
	if(!picture){ 
		return 0; 
	} 

	rgbdatanew = (unsigned char *)malloc(sizeof(unsigned char)*(3 * width * height));

	while(!feof(inpf)) 
	{ 

		//4.获取下一个NAL的长度,并且将NAL放入Buf 
		nalLen = getNextNal(inpf, Buf); 

		//5.对改NAL解码,解码的YUV数据存在picture中 
		consumed_bytes= avcodec_decode_video(c, picture, &got_picture, Buf, nalLen); 

		if(consumed_bytes > 0) 
		{ 

			//6.将picture中的YUV数据显示或者保存到文件 
			p=0; 
			for(i=0; i<c->height; i++) 
			{ 
				memcpy(DisplayBuf+p,picture->data[0] + i * picture->linesize[0], c->width); 
				p+=c->width; 
			} 
			for(i=0; i<c->height/2; i++) 
			{ 
				memcpy(DisplayBuf+p,picture->data[1] + i * picture->linesize[1], c->width/2);
				p+=c->width/2; 
			} 
			for(i=0; i<c->height/2; i++) 
			{ 
				memcpy(DisplayBuf+p,picture->data[2] + i * picture->linesize[2], c->width/2);
				p+=c->width/2; 
			} 
			//显示画面 
			DisplayVideo(DisplayBuf); 
		} 
	} 

	//7.关闭输入文件 
	if(inpf) 
		fclose(inpf); 

Decodereturn: 

	//8.关闭解码器,释放解码器内存 
	if(c) { 
		avcodec_close(c); 
		av_free(c); 
		c = NULL; 
	} 
	//9.释放解码画面内存 
	if(picture) { 
		av_free(picture); 
		picture = NULL; 
	} 

	//10.释放解码文件缓冲 
	if(Buf) 
	{ 
		free(Buf); 
		Buf = NULL; 
	} 

	free(DisplayBuf); 
} 


 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
FFmpeg是一个强大的多媒体处理框架,它可以用于视频和音频的编码、解码、流处理等任务。H264是常见的视频编码标准,而GDR(Graphics Device Render)通常指的是GPU加速解码。在FFmpeg中,如果你想要使用GPU进行H264解码,你需要利用FFmpeg中的硬件加速功能,如libavcodec中的H264 decoder和libavfilter中的GPU filters。 以下是一个简单的示例,展示如何在FFmpeg中使用GDR解码H264视频: ```cpp #include <ffplay.h> // 包含FFmpeg的播放器API int main(int argc, char** argv) { AVFormatContext* ctx = NULL; AVPacket pkt; AVStream* stream; AVCodecContext* codec_ctx; AVFrame frame; // 初始化FFmpeg av_register_all(); avformat_network_init(); // 打开输入文件 if (argc != 2) { fprintf(stderr, "Usage: %s <input_file>\n", argv); return 1; } if (avformat_open_input(&ctx, argv, NULL, NULL) != 0) { perror("Opening file"); return 1; } // 查找H264视频流 for (int i = 0; i < ctx->nb_streams; i++) { stream = ctx->streams[i]; if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO && stream->codec->codec_id == AV_CODEC_ID_H264) { break; } } // 获取视频流的解码上下文 codec_ctx = stream->codec; // 启动解码器 if (avcodec_open2(codec_ctx, NULL, NULL) < 0) { perror("Opening codec"); return 1; } // 初始化帧 av_frame_alloc(&frame); // 创建一个FFmpeg播放器实例 FFPacketContext packet_ctx; FFParseContext parse_ctx; av_register_all(); ff_parse_init(&parse_ctx); avformat_network_init(); FFParseAVFormatContext parse_ctx_with_format = { .avformat_ctx = ctx }; // 进入主循环 while (1) { // 从输入读取数据 if (av_read_frame(ctx, &pkt) >= 0) { // 使用GPU解码 if (avcodec_decode_video2(codec_ctx, &frame, &pkt.isAccepted, &pkt) == 0) { // 处理解码后的帧 // frame data can be accessed here } else { printf("Error decoding frame\n"); } av_packet_unref(&pkt); } else { if (pkt.data) av_free(pkt.data); break; } } // 关闭资源 avcodec_close(codec_ctx); avformat_close_input(&ctx); av_frame_free(&frame); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值