The simplest Video Encoder: Based on libx264 (encoding YUV is H.264), libxpolicyuv

Source: Internet
Author: User

The simplest Video Encoder: Based on libx264 (encoding YUV is H.264), libxpolicyuv
This document records a simple H.264-based H.264 video encoder. Previously recorded H.264 encoders were encoded Based on FFmpeg calling libx264, for example:
The simplest Video Encoder Based on FFMPEG (YUV encoding is H.264)
Compared with the encoder above, the encoder recorded in this article is a "lightweight" encoder. Because it no longer contains FFmpeg code, libx264 is called directly to complete encoding. Therefore, the project size is very small. The encoder can encode the input YUV data into a H.264 bitstream file.
 
Flowchart

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


The main functions in the flowchart are as follows.
X1__param_default (): sets the default value of the parameter set struct x1__param_t.
X__picture_alloc (): allocates memory for the image structure x264_picture_t.
X1__encoder_open (): Enable the encoder.
X1__encoder_encode (): encode an image.
X1__encoder_close (): Disable the encoder.
X264_picture_clean (): release the resources applied for by x264_picture_alloc.
 
The structure of the stored data is as follows.
X__picture_t: stores pre-compression pixel data.
X1__nal_t: 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
/*** Simplest X264-Based Video Encoder * Simplest X264 Encoder ** leixiao 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 for H. 264 bitrate, which is the simplest * libx264-Based Video Encoder ** This software encode YUV data to H. 264 bitstream. * It's the simplest encoder example based on libx.pdf. */# include <stdio. h> # include <stdlib. h> # include "stdint. h "# if defined (_ cplusplus) extern" C "{# include" x264.h "}; # else # include" x264.h "# endif int main (int argc, char ** argv) {int ret; int y_size; int I, j; // FILE * fp_src = fopen (".. /cuc_ieschool_640x360_yuv444p.yuv "," rb "); FILE * fp_src = fopen (".. /cuc_ieschool_640x360_yuv420p.yuv "," rb "); FILE * fp_dst = fopen (" cuc_ieschool.h264 "," wb "); // Encode 50 frame // if set 0, encode all frame int frame_num = 50; int csp = X264_CSP_I420; int width = 640, height = 360; int iNal = 0; x1__nal_t * pNals = NULL; x1__t * pHandle = NULL; required * pPic_in = (Bytes *) malloc (sizeof (bytes); bytes * pPic_out = (Bytes *) malloc (sizeof (bytes); x264_param_t * pParam = (x264_param_t *) malloc (sizeof (x264_param_t); // Check if (fp_src = NULL | fp_dst = NULL) {printf ("Error open files. \ n "); return-1;} x1__param_default (pParam); pParam-> I _width = width; pParam-> I _height = height; /* // Param pParam-> I _log_level = xpai_log_debug; pParam-> I _threads = enabled; pParam-> I _frame_total = 0; pParam-> enabled = 10; pParam-> I _bframe = 5; pParam-> B _open_gop = 0; pParam-> I _bframe_pyramid = 0; pParam-> rc. I _qp_constant = 0; pParam-> rc. I _qp_max = 0; pParam-> rc. I _qp_min = 0; pParam-> Limit = inflow; pParam-> I _fps_den = 1; pParam-> I _fps_num = 25; pParam-> I _timebase_den = pParam-> I _fps_num; pParam-> I _timebase_num = pParam-> I _fps_den; */pParam-> I _csp = csp; Forward (pParam, x1__profile_names [5]); pHandle = forward (pParam ); x264_picture_init (pPic_out); values (pPic_in, csp, pParam-> I _width, pParam-> I _height); // ret = x264_encoder_headers (pHandle, & pNals, & iNal ); y_size = pParam-> I _width * pParam-> I _height; // detect frame number if (frame_num = 0) {fseek (fp_src, 0, SEEK_END); switch (csp) {case x1__csp_i444: frame_num = ftell (fp_src)/(y_size * 3); break; case x1__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 x1__csp_i444: {fread (pPic_in-> img. plane [0], y_size, 1, fp_src); // Y fread (pPic_in-> img. plane [1], y_size, 1, fp_src); // U fread (pPic_in-> img. plane [2], y_size, 1, fp_src); // V break;} case x1__csp_i420: {fread (pPic_in-> img. plane [0], y_size, 1, fp_src); // Y fread (pPic_in-> img. plane [1], y_size/4, 1, fp_src); // U fread (pPic_in-> img. plane [2], y_size/4, 1, fp_src); // V break;} default: {printf ("Colorspace Not Support. \ n "); return-1 ;}} pPic_in-> I _pts = I; ret = x264_encoder_encode (pHandle, & pNals, & iNal, pPic_in, pPic_out ); if (ret <0) {printf ("Error. \ n "); return-1;} printf (" Succeed encode frame: % 5d \ n ", I); for (j = 0; j <iNal; ++ j) {fwrite (pNals [j]. p_payload, 1, pNals [j]. I _payload, fp_dst) ;}} I = 0; // flush encoder while (1) {ret = x264_encoder_encode (pHandle, & pNals, & iNal, NULL, pPic_out ); if (ret = 0) {break;} printf ("Flush 1 frame. \ n "); for (j = 0; j <iNal; ++ j) {fwrite (pNals [j]. p_payload, 1, pNals [j]. I _payload, fp_dst);} I ++;} x264_picture_clean (pPic_in); encrypt (pHandle); pHandle = NULL; free (pPic_in); free (pPic_out); free (pParam ); 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 a H.264 code stream file.


The information of the H.264 bitstream 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.