最简单的视频编码器:基于libx265(编码YUV为H.265)

介绍了一个基于libx265的简单H.265视频编码器实现,可将YUV格式数据编码为H.265码流。提供源代码及运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

=====================================================

最简单的视频编码器系列文章列表:

最简单的视频编码器:编译

最简单的视频编码器:基于libx264(编码YUV为H.264)

最简单的视频编码器:基于libx265(编码YUV为H.265)

最简单的视频编码器:libvpx(编码YUV为VP8)

=====================================================

本文记录一个最简单的基于libx265的H.265(HEVC)视频编码器。此前记录的编码器是通过FFmpeg调用libx265完成编码的,例如:

《最简单的基于FFmpeg的视频编码器-更新版(YUV编码为HEVC(H.265))》

相比与上文中的编码器,本文记录的编码器属于“轻量级”的编码器。因为它不再包含FFmpeg的代码,直接调用libx265完成编码。因此项目的体积非常小巧。该编码器可以将输入的YUV数据编码为H.265码流文件。

流程图

调用libx265进行视频编码的流程图如下所示。

 

从流程图中可以看出x265的API和x264的API十分相似。它们在用法上只有微小的不同。
流程图中主要的函数如下所示。
x265_param_alloc():为参数集结构体x265_param分配内存。
x265_param_default():设置参数集结构体x265_param的缺省值。
x265_picture_alloc():为图像结构体x265_picture分配内存。
x265_picture_init():设置图像结构体x265_picture的缺省值。
x265_encoder_open():打开编码器。
x265_encoder_encode():编码一帧图像。
x265_encoder_close():关闭编码器。
x265_picture_free():释放x265_picture_alloc()申请的资源。
x265_param_free():释放x265_param_alloc()申请的资源。

存储数据的结构体如下所示。
x265_picture:存储压缩编码前的像素数据。
x265_nal:存储压缩编码后的码流数据。

此外流程图中还包括一个“flush_encoder”模块,该模块使用的函数和编码模块是一样的。唯一的不同在于不再输入视频像素数据。它的作用是输出编码器中剩余的码流数据。

源代码

/**
 * 最简单的基于X265的视频编码器
 * Simplest X265 Encoder
 *
 * 雷霄骅 Lei Xiaohua
 * leixiaohua1020@126.com
 * 中国传媒大学/数字电视技术
 * Communication University of China / Digital TV Technology
 * http://blog.csdn.net/leixiaohua1020
 *
 * 本程序可以YUV格式的像素数据编码为H.265码流,是最简单的
 * 基于libx265的视频编码器
 *
 * This software encode YUV data to H.265 bitstream.
 * It's the simplest encoder example based on libx265.
 */
#include <stdio.h>
#include <stdlib.h>

#if defined ( __cplusplus)
extern "C"
{
#include "x265.h"
};
#else
#include "x265.h"
#endif

int main(int argc, char** argv){
	int i,j;
	FILE *fp_src=NULL;
	FILE *fp_dst=NULL;
	int y_size;
	int buff_size;
	char *buff=NULL;
	int ret;
	x265_nal *pNals=NULL;
	uint32_t iNal=0;

	x265_param* pParam=NULL;
	x265_encoder* pHandle=NULL;
	x265_picture *pPic_in=NULL;

	//Encode 50 frame
	//if set 0, encode all frame
	int frame_num=50;
	int csp=X265_CSP_I420;
	int width=640,height=360;

	fp_src=fopen("../cuc_ieschool_640x360_yuv420p.yuv","rb");
	//fp_src=fopen("../cuc_ieschool_640x360_yuv444p.yuv","rb");

	fp_dst=fopen("cuc_ieschool.h265","wb");
	//Check
	if(fp_src==NULL||fp_dst==NULL){
		return -1;
	}

	pParam=x265_param_alloc();
	x265_param_default(pParam);
	pParam->bRepeatHeaders=1;//write sps,pps before keyframe
	pParam->internalCsp=csp;
	pParam->sourceWidth=width;
	pParam->sourceHeight=height;
	pParam->fpsNum=25;
	pParam->fpsDenom=1;
	//Init
	pHandle=x265_encoder_open(pParam);
	if(pHandle==NULL){
		printf("x265_encoder_open err\n");
		return 0;
	}
	y_size = pParam->sourceWidth * pParam->sourceHeight;

	pPic_in = x265_picture_alloc();
	x265_picture_init(pParam,pPic_in);
	switch(csp){
	case X265_CSP_I444:{
		buff=(char *)malloc(y_size*3);
		pPic_in->planes[0]=buff;
		pPic_in->planes[1]=buff+y_size;
		pPic_in->planes[2]=buff+y_size*2;
		pPic_in->stride[0]=width;
		pPic_in->stride[1]=width;
		pPic_in->stride[2]=width;
		break;
					   }
	case X265_CSP_I420:{
		buff=(char *)malloc(y_size*3/2);
		pPic_in->planes[0]=buff;
		pPic_in->planes[1]=buff+y_size;
		pPic_in->planes[2]=buff+y_size*5/4;
		pPic_in->stride[0]=width;
		pPic_in->stride[1]=width/2;
		pPic_in->stride[2]=width/2;
		break;
					   }
	default:{
		printf("Colorspace Not Support.\n");
		return -1;
			}
	}
	
	//detect frame number
	if(frame_num==0){
		fseek(fp_src,0,SEEK_END);
		switch(csp){
		case X265_CSP_I444:frame_num=ftell(fp_src)/(y_size*3);break;
		case X265_CSP_I420:frame_num=ftell(fp_src)/(y_size*3/2);break;
		default:printf("Colorspace Not Support.\n");return -1;
		}
		fseek(fp_src,0,SEEK_SET);
	}

	//Loop to Encode
	for( i=0;i<frame_num;i++){
		switch(csp){
		case X265_CSP_I444:{
			fread(pPic_in->planes[0],1,y_size,fp_src);		//Y
			fread(pPic_in->planes[1],1,y_size,fp_src);		//U
			fread(pPic_in->planes[2],1,y_size,fp_src);		//V
			break;}
		case X265_CSP_I420:{
			fread(pPic_in->planes[0],1,y_size,fp_src);		//Y
			fread(pPic_in->planes[1],1,y_size/4,fp_src);	//U
			fread(pPic_in->planes[2],1,y_size/4,fp_src);	//V
			break;}
		default:{
			printf("Colorspace Not Support.\n");
			return -1;}
		}

		ret=x265_encoder_encode(pHandle,&pNals,&iNal,pPic_in,NULL);	
		printf("Succeed encode %5d frames\n",i);

		for(j=0;j<iNal;j++){
			fwrite(pNals[j].payload,1,pNals[j].sizeBytes,fp_dst);
		}	
	}
	//Flush Decoder
	while(1){
		ret=x265_encoder_encode(pHandle,&pNals,&iNal,NULL,NULL);
		if(ret==0){
			break;
		}
		printf("Flush 1 frame.\n");

		for(j=0;j<iNal;j++){
			fwrite(pNals[j].payload,1,pNals[j].sizeBytes,fp_dst);
		}
	}
	
	x265_encoder_close(pHandle);
	x265_picture_free(pPic_in);
	x265_param_free(pParam);
	free(buff);
	fclose(fp_src);
	fclose(fp_dst);
	
	return 0;
}

运行结果

程序的输入为一个YUV文件(已经测试过YUV444P和YUV420P两种格式)。


输出为H.265码流文件。


H.265码流文件的信息如下所示。


下载


Simplest Encoder

项目主页

SourceForge:https://sourceforge.net/projects/simplestencoder/

Github:https://github.com/leixiaohua1020/simplest_encoder

开源中国:http://git.oschina.net/leixiaohua1020/simplest_encoder


CDSN下载地址:http://download.csdn.net/detail/leixiaohua1020/8284105


该解决方案包含了几个常见的编码器的使用示例:
simplest_vpx_encoder:最简单的基于libvpx的视频编码器
simplest_x264_encoder:最简单的基于libx264的视频编码器
simplest_x265_encoder:最简单的基于libx265的视频编码器

你可以使用 FFmpeg 库来进行 H.264 编码。 首先,你需要用 OpenCV 来读取视频并把每一帧转换为 `cv::Mat` 格式。然后,你可以使用多线程来并行处理视频帧。 下一步是使用 FFmpeg 库来编码每个 `cv::Mat` 帧。你可以使用 `AVCodecContext` 来设置编码器参数,然后使用 `avcodec_encode_video2()` 方法将帧编码为 H.264 格式。最后,你需要使用 `AVPacket` 将编码数据写入输出文件。 以下是一个简单的代码示例,可以将 `cv::Mat` 帧编码为 H.264 格式: ```c++ #include <iostream> #include <thread> #include <mutex> #include <condition_variable> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> } // Video parameters const int WIDTH = 640; const int HEIGHT = 480; const int FPS = 30; // FFmpeg variables AVFormatContext *pFormatCtx = nullptr; AVOutputFormat *pOutputFmt = nullptr; AVCodec *pCodec = nullptr; AVCodecContext *pCodecCtx = nullptr; AVStream *pStream = nullptr; uint8_t *pFrameBuffer = nullptr; AVFrame *pFrame = nullptr; AVPacket pkt; // Thread synchronization variables std::mutex mtx; std::condition_variable cv; bool ready = false; bool quit = false; // Thread function to encode a frame void encodeFrame(cv::Mat frame) { // Convert OpenCV Mat to FFmpeg AVFrame cv::cvtColor(frame, frame, cv::COLOR_BGR2YUV_I420); memcpy(pFrame->data[0], frame.data, WIDTH*HEIGHT); memcpy(pFrame->data[1], frame.data + WIDTH*HEIGHT, WIDTH*HEIGHT/4); memcpy(pFrame->data[2], frame.data + WIDTH*HEIGHT*5/4, WIDTH*HEIGHT/4); // Encode the frame int ret = avcodec_send_frame(pCodecCtx, pFrame); if (ret < 0) { std::cerr << "Error sending frame to encoder: " << av_err2str(ret) << std::endl; return; } while (ret >= 0) { ret = avcodec_receive_packet(pCodecCtx, &pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { std::cerr << "Error receiving packet from encoder: " << av_err2str(ret) << std::endl; return; } // Write the packet to the output file av_interleaved_write_frame(pFormatCtx, &pkt); } } int main() { // Initialize FFmpeg av_register_all(); avformat_network_init(); // Open output file int ret = avformat_alloc_output_context2(&pFormatCtx, nullptr, nullptr, "output.mp4"); if (ret < 0) { std::cerr << "Error allocating output format context: " << av_err2str(ret) << std::endl; return 1; } pOutputFmt = pFormatCtx->oformat; // Find H.264 encoder pCodec = avcodec_find_encoder_by_name("libx264"); if (!pCodec) { std::cerr << "Error finding H.264 encoder" << std::endl; return 1; } // Create codec context pCodecCtx = avcodec_alloc_context3(pCodec); if (!pCodecCtx) { std::cerr << "Error allocating codec context" << std::endl; return 1; } pCodecCtx->width = WIDTH; pCodecCtx->height = HEIGHT; pCodecCtx->time_base = {1, FPS}; pCodecCtx->framerate = {FPS, 1}; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; pCodecCtx->gop_size = 10; pCodecCtx->bit_rate = 1000000; // Open codec ret = avcodec_open2(pCodecCtx, pCodec, nullptr); if (ret < 0) { std::cerr << "Error opening codec: " << av_err2str(ret) << std::endl; return 1; } // Add video stream to output file pStream = avformat_new_stream(pFormatCtx, pCodec); if (!pStream) { std::cerr << "Error creating new stream" << std::endl; return 1; } ret = avcodec_parameters_from_context(pStream->codecpar, pCodecCtx); if (ret < 0) { std::cerr << "Error setting codec parameters: " << av_err2str(ret) << std::endl; return 1; } av_dump_format(pFormatCtx, 0, "output.mp4", 1); // Open output file ret = avio_open(&pFormatCtx->pb, "output.mp4", AVIO_FLAG_WRITE); if (ret < 0) { std::cerr << "Error opening output file: " << av_err2str(ret) << std::endl; return 1; } // Write header to output file ret = avformat_write_header(pFormatCtx, nullptr); if (ret < 0) { std::cerr << "Error writing header to output file: " << av_err2str(ret) << std::endl; return 1; } // Allocate frame buffer and AVFrame pFrameBuffer = (uint8_t*) av_malloc(av_image_get_buffer_size(pCodecCtx->pix_fmt, WIDTH, HEIGHT, 32)); av_image_fill_arrays(pFrame->data, pFrame->linesize, pFrameBuffer, pCodecCtx->pix_fmt, WIDTH, HEIGHT, 32); pFrame->width = WIDTH; pFrame->height = HEIGHT; pFrame->format = pCodecCtx->pix_fmt; // Start encoding thread std::thread encoder([&]() { while (!quit) { std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, []{ return ready || quit; }); if (ready) { // Encode the frame encodeFrame(frame); // Notify main thread that encoding is done ready = false; cv.notify_one(); } } }); // Start reading video frames cv::VideoCapture cap("input.mp4"); if (!cap.isOpened()) { std::cerr << "Error opening input file" << std::endl; return 1; } cv::Mat frame; while (cap.read(frame)) { // Wait for encoding thread to finish previous frame std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, []{ return !ready || quit; }); if (!quit) { // Start encoding new frame ready = true; cv.notify_one(); } } // Wait for encoding thread to finish last frame std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, []{ return !ready; }); // Clean up FFmpeg av_write_trailer(pFormatCtx); avcodec_free_context(&pCodecCtx); av_frame_free(&pFrame); av_free(pFrameBuffer); avio_close(pFormatCtx->pb); avformat_free_context(pFormatCtx); quit = true; cv.notify_one(); encoder.join(); return 0; } ``` 请注意,此代码示例省略了许多错误检查和清理代码,并且可能需要进行适当修改以适应你的需求。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值