This document records one of the simplest libx265-based h.265 (HEVC) video encoders. The previously recorded encoder is encoded by FFmpeg call libx265, for example:
The simplest ffmpeg-based video encoder-Updated (YUV encoded as HEVC (h.265))
Compared with the encoder above, the encoder recorded in this document belongs to the "lightweight" encoder. Because it no longer contains ffmpeg code, call libx265 to complete the encoding directly. So the size of the project is very small. The encoder can encode the input YUV data into a h.265 bitstream file.
Flow chart
The flowchart that calls libx265 for video encoding is shown below.
As you can see from the flowchart, the x265 API is very similar to the x264 API. They are only slightly different in usage. The main functions in the flowchart are shown below.
X265_param_alloc (): Allocates memory for the parameter set structure body X265_param.
X265_param_default (): Sets the default value of the parameter set structure body X265_param.
X265_picture_alloc (): Allocates memory for the image structure body x265_picture.
X265_picture_init (): Sets the default value for the image structure body x265_picture.
X265_encoder_open (): Open the encoder.
X265_encoder_encode (): Encodes a frame image.
X265_encoder_close (): Turn off the encoder.
X265_picture_free (): Releases the resources requested by X265_picture_alloc ().
X265_param_free (): Releases the resources requested by X265_param_alloc ().
The structure in which the data is stored is shown below.
X265_picture: Stores the pixel data before compressing the encoding.
X265_nal: Stores encoded stream data after compression.
Also included in the flowchart is a "Flush_encoder" module, which uses the same function as the encoding module. The only difference is that the video pixel data is no longer entered. Its function is to output the remaining stream data in the encoder.
Source
/** * Simplest X265 based video encoder * Simplest X265 Encoder * * Lei Huawei Lei Xiaohua * leixiaohua102[email protected] * Communication University/Digital TV Technology * Communication University of China/digital TV technology * http://blog.csdn.net/leixiaohua1020 * * This program can be in YUV format pixel data encoded as h.2 65 stream, is the simplest * libx265 based video encoder * * 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" #endifint 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 frame//if set 0, Encode all frameint 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 ");//checkif (fp_src==null| | Fp_dst==null) {return-1;} Pparam=x265_param_alloc (); X265_param_default (pparam);pP Aram->brepeatheaders=1;//write Sps,pps before Keyframepparam->internalcsp=csp;pparam->sourcewidth=width;pparam->sourceheight=height;pparam-> Fpsnum=25;pparam->fpsdenom=1;//initphandle=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);pP ic_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);pP Ic_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 Numberif (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 Encodefor (i=0;i<frame_num;i++) {switch (CSP) {case X265_csp_i444:{fread (ppic_in->planes[0],1,y_size, FP_SRC);//yfread (PPIC_IN->PLANES[1],1,Y_SIZE,FP_SRC);//ufread (PPIC_IN->PLANES[2],1,Y_SIZE,FP_SRC);// Vbreak;} Case X265_csp_i420:{fread (PPIC_IN->PLANES[0],1,Y_SIZE,FP_SRC);//yfread (Ppic_in->planes[1],1,y_size/4,fp_ SRC);//ufread (ppic_in->planes[2],1,y_size/4,fp_src);//vbreak;} default:{printf ("ColorSpace not support.\n"); return-1;}} Ret=x265_encoder_encode (phandle,&pnals,&inal,ppic_in,null);p rintf ("Succeed Encode%5d frames\n", I); for (j= 0;j<inal;j++) {fwrite (pnals[j].payload,1,pnals[j].siZEBYTES,FP_DST);}} Flush Decoderwhile (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_param_free (Pparam); X265_picture_free (ppic_in); free (buff); fclose (FP_SRC); Fclose (FP_DST); return 0;}
Run results
The input to the program is a YUV file (two formats have been tested for yuv444p and yuv420p).
The output is a h.265 stream file.
The information for the h.265 code stream file is as follows.
Download
simplest Encoder
SourceForge Project home: https://sourceforge.net/projects/simplestencoder/
cdsn:http://download.csdn.net/detail/leixiaohua1020/8284105
The solution contains several common examples of using encoders:
Simplest_vpx_encoder: The simplest LIBVPX-based video encoder
Simplest_x264_encoder: The simplest libx264-based video encoder
Simplest_x265_encoder: The simplest libx265-based video encoder
Simplest video encoder: Based on libx265 (encoded YUV is h.265)