=====================================================函數
最簡單的視頻編碼器系列文章列表:oop
最簡單的視頻編碼器:編譯
最簡單的視頻編碼器:基於libx264(編碼YUV爲H.264)post
最簡單的視頻編碼器:基於libx265(編碼YUV爲H.265)測試
最簡單的視頻編碼器:libvpx(編碼YUV爲VP8)ui
=====================================================編碼
本文記錄一個最簡單的基於libx265的H.265(HEVC)視頻編碼器。此前記錄的編碼器是經過FFmpeg調用libx265完成編碼的,例如:spa
《最簡單的基於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」模塊,該模塊使用的函數和編碼模塊是同樣的。惟一的不一樣在於再也不輸入視頻像素數據。它的做用是輸出編碼器中剩餘的碼流數據。
源代碼
- #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;
-
-
-
- int frame_num=50;
- int csp=X265_CSP_I420;
- int width=640,height=360;
-
- fp_src=fopen("../cuc_ieschool_640x360_yuv420p.yuv","rb");
-
-
- fp_dst=fopen("cuc_ieschool.h265","wb");
-
- if(fp_src==NULL||fp_dst==NULL){
- return -1;
- }
-
- pParam=x265_param_alloc();
- x265_param_default(pParam);
- pParam->bRepeatHeaders=1;
- pParam->internalCsp=csp;
- pParam->sourceWidth=width;
- pParam->sourceHeight=height;
- pParam->fpsNum=25;
- pParam->fpsDenom=1;
-
- 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;
- }
- }
-
-
- 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);
- }
-
-
- for( i=0;i<frame_num;i++){
- switch(csp){
- case X265_CSP_I444:{
- fread(pPic_in->planes[0],1,y_size,fp_src);
- fread(pPic_in->planes[1],1,y_size,fp_src);
- fread(pPic_in->planes[2],1,y_size,fp_src);
- break;}
- case X265_CSP_I420:{
- fread(pPic_in->planes[0],1,y_size,fp_src);
- fread(pPic_in->planes[1],1,y_size/4,fp_src);
- fread(pPic_in->planes[2],1,y_size/4,fp_src);
- 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);
- }
- }
-
- 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的視頻編碼器