Recently helped a friend to see the next X264 compressed video, the main reference to Rai (leixiaohua1020) column of Open source code:
http://blog.csdn.net/leixiaohua1020/article/details/42078645
But the online can find examples are the YUV (such as I420, etc.) encoded as H264, in the encoding bitmap format of RGB data before actually need to convert the data format, as long as the source data format specified can be, X264 library can be automatically processed.
As follows:
int csp=x264_csp_bgr| X264_csp_vflip; // This format is bitmap's kind of upside-down BGR format . int CSP = x264_csp_i420; // This is the I420 format .
At the same time also tested X264VFW library, there are many problems (compressed to memory do not know why not, directly compressed to file path and filename does not support Chinese, etc.), do not use, we recommend that the Library test, X264 official website does not recommend this library.
The code of Lei Hua is also reprint as follows, which adds a few comments: (the blog post is forbidden to reprint without permission, the code in it does not know whether it can be posted here?) )
/** * The simplest X264 based video encoder * Simplest X264 Encoder * * Lei hua Lei xiaohua * [email protected] * Communication University/Digital TV technology * Communication U Niversity of China/digital TV technology *http://blog.csdn.net/leixiaohua1020* * This program can be in YUV format pixel data encoded as H. s stream, is the simplest * based on libx264 Video Encoder * * This software encode YUV data to H. * t. * It's th E Simplest encoder example based on libx264. */#include<stdio.h>#include<stdlib.h>#include"stdint.h" #ifDefined (__cplusplus)extern "C"{#include"x264.h"};#else#include"x264.h"#endif intMainintargcChar**argv) { intret; inty_size; inti,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 Frame//If set 0, encode all frame intframe_num= -; intcsp=x264_csp_i420; //Tom NOTE: If you compress the RGB data in bitmap format directly, you need to modify this sentence to int csp=x264_csp_bgr| X264_csp_vflip; intWidth=640, height= the; intINal =0; x264_nal_t* Pnals =NULL; x264_t* Phandle =NULL; x264_picture_t* ppic_in = (x264_picture_t*)malloc(sizeof(x264_picture_t)); x264_picture_t* Ppic_out = (x264_picture_t*)malloc(sizeof(x264_picture_t)); 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; } x264_param_default (Pparam);
//Tom Note: The above default parameter is the two Pass mode option, if you want to compress in real time, you need to use the one pass mode , this sentence is modified to:// X264_param_default_preset (Pparam, "Veryfast", "zerolatency");
Pparam->i_width =width; Pparam->i_height =height; /*//param pparam->i_log_level = x264_log_debug; Pparam->i_threads = X264_sync_lookahead_auto; pparam->i_frame_total = 0; Pparam->i_keyint_max = 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->i_bframe_adaptive = X264_b_adapt_trellis; 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; X264_param_apply_profile (Pparam, x264_profile_names[5]); Phandle=X264_encoder_open (Pparam); X264_picture_init (ppic_out); X264_picture_alloc (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) { CaseX264_csp_i444:frame_num=ftell (FP_SRC)/(y_size*3); Break; CaseX264_csp_i420:frame_num=ftell (FP_SRC)/(y_size*3/2); Break; default:p rintf ("ColorSpace not support.\n");return-1; } fseek (FP_SRC,0, Seek_set); } //Loop to Encode for(i=0; i<frame_num;i++){ Switch(CSP) { Casex264_csp_i444:{fread (ppic_in->img.plane[0],y_size,1, FP_SRC);//YFread (ppic_in->img.plane[1],y_size,1, FP_SRC);//UFread (ppic_in->img.plane[2],y_size,1, FP_SRC);//V Break;} Casex264_csp_i420:{fread (ppic_in->img.plane[0],y_size,1, FP_SRC);//YFread (ppic_in->img.plane[1],y_size/4,1, FP_SRC);//UFread (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) { //Tom Note: If it is one pass mode, save here, the inal is 0, and it will not be called .
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){ //Tom Note: If it is a one pass mode, the front is already saved, break directly here Break; } printf ("Flush 1 frame.\n"); for(j =0; J < INal; ++j) { //Tom Note: If it is in the mode of the "pass", save it hereFwrite (Pnals[j].p_payload,1, Pnals[j].i_payload, FP_DST); } I++; } x264_picture_clean (ppic_in); X264_encoder_close (Phandle); Phandle=NULL; Free(ppic_in); Free(ppic_out); Free(Pparam); Fclose (FP_SRC); Fclose (FP_DST); return 0;}
X264 Library directly compresses bitmap format data