The simplest Video Encoder: Based on libx265 (H.265 encoded YUV), libx265h. 265

Source: Internet
Author: User

The simplest Video Encoder: Based on libx265 (H.265 encoded YUV), libx265h. 265

This document records a simple H.265 (HEVC) Video Encoder Based on libx265. The previously recorded encoder uses FFmpeg to call libx265 for encoding. For example:

The simplest FFmpeg-based video encoder-new version (YUV encoding is HEVC (H.265)

Compared with the encoder above, the encoder recorded in this article is a "lightweight" encoder. Because it no longer contains FFmpeg code, libx265 is called directly to complete encoding. Therefore, the project size is very small. The encoder can encode the input YUV data into an H.265 code stream file.

Flowchart

The flowchart of calling libx265 for video encoding is as follows.

 
The flowchart shows that the x265 API is very similar to the x264 API. They are only slightly different in usage. The main functions in the flowchart are as follows.
X265_param_alloc (): allocates memory for the parameter set struct x265_param.
X265_param_default (): sets the default value of the parameter set struct x265_param.
X265_picture_alloc (): allocates memory for the image structure x265_picture.
X265_picture_init (): sets the default value of the image structure x265_picture.
X265_encoder_open (): Enable the encoder.
X265_encoder_encode (): encode an image.
X265_encoder_close (): Disable the encoder.
X265_picture_free (): release the resources applied for by x265_picture_alloc.
X265_param_free (): release the resources applied for by x265_param_alloc.

The structure of the stored data is as follows.
X265_picture: stores pre-compressed pixel data.
X265_nal: stores compressed encoded bitstream data.

The flowchart also contains a "flush_encoder" module, which uses the same functions as the encoding module. The only difference is that no video pixel data is input. It is used to output the remaining bitstream data in the encoder.

Source code
/*** The Simplest Video Encoder Based on X265 * Simplest X265 Encoder ** leixiao Li Lei Xiaohua * leixiaohua1020@126.com * China Media University/Digital TV technology * Communication University of China/Digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this program can be YUV format pixel data encoding H.265 code 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; optional * pHandle = NULL; x265_picture * pPic_in = NULL; // Encode 50 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 (".. /configure "," 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); pParam-> 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 = extract (); 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 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); printf (" 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) ;}}response (pHandle); Response (pPic_in); x265_param_free (pParam); free (buff); fclose (fp_src); fclose (fp_dst ); return 0 ;}

Running result

Program input is a YUV file (two formats, YUV444P and YUV420P, have been tested ).


The output is an H.265 code stream file.


The information of 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


This solution contains several common encoder usage examples:
Simplest_vpx_encoder: the simplest Video Encoder Based on libvpx
Simplest_x1__encoder: the simplest Video Encoder Based on libx264
Simplest_x265_encoder: the simplest Video Encoder Based on libx265

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.