1、源码下载
http://download.videolan.org/x264/snapshots/
2、安装
./configure --prefix=./_install --enable-shared --enable-static
make
sudo make install
3、demo
在x264库里的x264_config.h中确定版本号,版本太混乱了,还不兼容
#define X264_POINTVER "0.158.x"
/** * 最简单的基于X264的视频编码器 * Simplest X264 Encoder * leixiaohua * * 本程序可以YUV格式的像素数据编码为H.264码流,是最简单的 * 基于libx264的视频编码器 * * This software encode YUV data to H.264 bitstream. * It's the simplest encoder example based on libx264. */ #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <unistd.h> //#include <pthread.h> #if defined ( __cplusplus) extern "C" { #include "x264.h" }; #else #include "x264.h" #endif int csp=X264_CSP_I420; int frame_num=0; int width=640; int height=360; FILE* fp_src; FILE* fp_dst; x264_nal_t* pNals; x264_t* pHandle; x264_picture_t* pPic_in; x264_picture_t* pPic_out; x264_param_t* pParam; int y_size; int en_h264_init() { fp_src = fopen("./cuc_ieschool_640x360_yuv420p.yuv", "rb"); fp_dst = fopen("output.h264", "wb"); pNals = NULL; pHandle = NULL; pPic_in = (x264_picture_t*)malloc(sizeof(x264_picture_t)); pPic_out = (x264_picture_t*)malloc(sizeof(x264_picture_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); x264_param_default_preset(pParam, "fast" , "zerolatency" ); pParam->i_csp=csp; pParam->i_width = width; pParam->i_height = height; pParam->i_fps_num = 25; pParam->i_fps_den = 1; pParam->i_threads = X264_SYNC_LOOKAHEAD_AUTO; pParam->i_keyint_max = 10; pParam->rc.i_bitrate = 1200; pParam->rc.i_rc_method = X264_RC_ABR; //set profile x264_param_apply_profile(pParam, "baseline"); //open encoder pHandle = x264_encoder_open(pParam); x264_picture_init(pPic_out); x264_picture_alloc(pPic_in, csp, pParam->i_width, pParam->i_height); y_size = pParam->i_width * pParam->i_height; //detect frame number if(frame_num==0){ fseek(fp_src,0,SEEK_END); frame_num=ftell(fp_src)/(y_size*3/2); fseek(fp_src,0,SEEK_SET); } } int en_h264_run(int frame_num,char *y,char *u,char *v) { int i,j,ret; int iNal = 0; for( i=0;i<frame_num;i++){ char y_buf[640*360]; char u_buf[640*360/4]; char v_buf[640*360/4]; #if 1 fread(y_buf, y_size, 1, fp_src); //Y fread(u_buf, y_size / 4, 1, fp_src); //U fread(v_buf, y_size / 4, 1, fp_src); //V #else memcpy(y_buf,y,sizeof(y)); memcpy(u_buf,u,sizeof(u)); memcpy(v_buf,v,sizeof(v)); #endif memcpy(pPic_in->img.plane[0],y_buf,y_size); memcpy(pPic_in->img.plane[1],u_buf,y_size / 4); memcpy(pPic_in->img.plane[2],v_buf,y_size / 4); 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); } } } int en_h264_release() { 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; } int main(int argc, char** argv) { en_h264_init(); char y[640*360]; char u[640*360/4]; char v[640*360/4]; en_h264_run(50,y,u,v); en_h264_release(); return 0; }
4、编译
cc main.c -o main -lx264 -std=c99