x264 is an open-source, h-code library. This article describes its compilation method in x86 Linux, and gives an example. First, compile
x264 compilation needs to be used to yasm, to http://www.tortall.net/projects/yasm/releases/download, the latest version is 1.3.1. The compile command is as follows:
#./configure;make;make Install
x264 official Download: http://www.videolan.org/developers/x264.html. Download the zip package named: last_x264.tar.bz2. The extracted files are named X264-snapshot-xxxx. The compile command is as follows:
$./configure--prefix=/home/latelee/bin/x264--enable-static
$ make
$ make install two, instance
The example that is used is given below.
/** x264 Library Encoding to use the following library-LPTHREAD-LDL require header files: stdint.h ffprobe info: Input #0, H264, from ' test.h264 ': duration:n/a, bitrate:n/a Stream #0:0: video:h264 (High), yuv420p, 176x144, fps, TBR, 1200k TBN, TBC */#include <stdio.h> # Include <stdlib.h> #include <stdint.h>//For uint8_t in x264.h #include "x264.h" int x264_encode (const CHA
r* infile, int width, int height, int type, const char* outfile) {FILE *fp_src = NULL;
FILE *fp_dst = NULL;
unsigned int luma_size = 0;
unsigned int chroma_size = 0;
int i_nal = 0;
int i_frame = 0;
unsigned int i_frame_size = 0;
int CSP = type;//x264_csp_i420;
x264_nal_t* nal = NULL;
x264_t* handle = NULL;
x264_picture_t* pic_in = NULL;
x264_picture_t* pic_out = NULL;
x264_param_t param;
FP_SRC = fopen (infile, "RB");
FP_DST = fopen (outfile, "WB");
if (Fp_src==null | | fp_dst==null) {perror ("Error Open YUV Files:");
return-1; } X264_PARAM_Default (¶M);
Param.i_width = width;
Param.i_height = height;
/*//param param.i_log_level = x264_log_debug;
Param.i_threads = X264_sync_lookahead_auto;
param.i_frame_total = 0;
Param.i_keyint_max = 10;
Param.i_bframe = 5;
PARAM.B_OPEN_GOP = 0;
Param.i_bframe_pyramid = 0;
param.rc.i_qp_constant=0;
param.rc.i_qp_max=0;
param.rc.i_qp_min=0;
Param.i_bframe_adaptive = X264_b_adapt_trellis;
Param.i_fps_den = 1;
Param.i_fps_num = 25;
Param.i_timebase_den = Param.i_fps_num;
Param.i_timebase_num = Param.i_fps_den;
*/PARAM.I_CSP = CSP;
Param.b_repeat_headers = 1;
PARAM.B_ANNEXB = 1;
Profile:high x264_profile_names definition See x264.h x264_param_apply_profile (¶m, x264_profile_names[2]);
pic_in = (x264_picture_t*) malloc (sizeof (x264_picture_t));
if (pic_in = = NULL) {goto out;
} pic_out = (x264_picture_t*) malloc (sizeof (x264_picture_t));
if (pic_out = = NULL) { Goto out;
} x264_picture_init (Pic_out);
X264_picture_alloc (pic_in, CSP, Param.i_width, param.i_height);
Handle = X264_encoder_open (¶m);
Calculate how many frames Luma_size = param.i_width * param.i_height;
Fseek (fp_src, 0, seek_end);
Switch (CSP) {Case x264_csp_i444:i_frame = Ftell (FP_SRC)/(LUMA_SIZE*3);
Chroma_size = luma_size;
Break
Case x264_csp_i420:i_frame = Ftell (FP_SRC)/(LUMA_SIZE*3/2);
Chroma_size = LUMA_SIZE/4;
Break
default:printf ("ColorSpace not support.\n");
Goto out;
} fseek (fp_src, 0, Seek_set);
printf ("framecnt:%d, y:%d u:%d\n", I_frame, Luma_size, chroma_size); Encode for (int i = 0; i < i_frame; i++) {switch (CSP) {case X264_csp_i444:cas
E x264_csp_i420:i_frame_size = fread (Pic_in->img.plane[0], 1, luma_size, FP_SRC); If (i_frame_size! = luma_size)
{printf ("Read Luma failed%d.\n", i_frame_size);
Break
} i_frame_size = Fread (pic_in->img.plane[1], 1, chroma_size, FP_SRC);
if (i_frame_size! = chroma_size) {printf ("Read chroma1 failed%d.\n", i_frame_size);
Break
} i_frame_size = Fread (pic_in->img.plane[2], 1, chroma_size, FP_SRC);
if (i_frame_size! = chroma_size) {printf ("Read chrome2 failed%d.\n", i_frame_size);
Break
} break;
default:printf ("ColorSpace not support.\n");
return-1;
} pic_in->i_pts = i;
I_frame_size = X264_encoder_encode (handle, &nal, &i_nal, pic_in, pic_out);
printf ("Encode frame:%5d framesize:%d nal:%d\n", I+1, I_frame_size, i_nal);
if (I_frame_size < 0) {printf ("Error encode frame:%d.\n", i+1);
Goto out; } #if, else if (i_frame_size) {if (!fwrite (nal->p_payload, 1, i_frame_size, Fp_ds
T)) goto out;
} #endif//Another practice #if 0//I_nal may be 0, possibly more than 1 for (int j = 0; J < I_nal; J + +)
{fwrite (nal[j].p_payload, 1, nal[j].i_payload, FP_DST); } #endif}//flush encoder while (X264_encoder_delayed_frames (handle))//while (1) {St
atic int cnt = 1;
I_frame_size = X264_encoder_encode (handle, &nal, &i_nal, NULL, pic_out);
printf ("Flush frame:%d framesize:%d nalsize:%d\n", cnt++, I_frame_size, i_nal);
if (i_frame_size = = 0) {break;
Goto out; } #if, else if (i_frame_size) {if (!fwrite (nal->p_payload, 1, i_frame_size, FP_DST
)) Goto out;
} #endif//Another practice #if 0//I_nal may be 0, possibly more than 1 for (int j = 0; J < I_nal; J + +)
{fwrite (nal[j].p_payload, 1, nal[j].i_payload, FP_DST);
} #endif} out:x264_picture_clean (pic_in);
if (handle) {x264_encoder_close (handle);
handle = NULL;
} if (pic_in) free (pic_in);
if (pic_out) free (pic_out);
Fclose (FP_SRC);
Fclose (FP_DST);
return 0; }
How to use:
X264_encode (".. /src/suzie_qcif_176x144_yuv420p.yuv ", 176, 144, 1," test.h264 ");
Makefile as follows:
# # (c) Copyleft 2011~2015 # late Lee from http://www.latelee.org # # A Simple Makefile for *one* project (c Or/and cpp fi
Le) in *one* directory # # Note: # can put head file (s) in ' include ' directory, so it looks # a little neat. # Usage: $ make # $ make debug=y # # log # 2013-05-14 sth about debug ... ################################## #############################################
# !!!
= = = Cross compile ... Cross_compile = CC = $ (cross_compile) gcc CXX = $ (cross_compile) g++ ar = $ (cross_compile) ar arflags = cr RM =-RM-RF MA KE = make CFLAGS: =-wall #**************************************************************************** # debug can is S Et to Y to include debugging info, or n otherwise debug: = N #************************************************** Ifeq ($ (debug), y) CFLAGS + =-ggdb-rdynamic else CFLAGS + =-o2-s endif #!!! = = = DEFS = CFLAGS + $ (DEFS) Ldflags: = $ (LIBS) #!!! = = = Incdirs: =-i./include #!!!
===CFLAGS + = $ (incdirs) #!!! = = = Ldflags + =/lib/libx264.a./LIB/LIBX265.A-LPTHREAD-LDL #!!!
= = = # source file (s), including C file (s) CPP file (s) # You can also use $ (wildcard *.c), etc. Src_c: = $ (wildcard *.c) Src_cpp: = $ (wildcard *.cpp) # object file (s) Obj_c: = $ (Patsubst%.c,%.o,$ (src_c)) obj_cpp : = $ (Patsubst%.cpp,%.o,$ (src_cpp)) #!!!
= = = # executable file target = a.out ############################################################################### All: $ (target) $ (target): $ (Obj_c) $ (obj_cpp) @echo "Generating executable file ..." $ (Notdir $ (target)) @$ (CXX) $ (cflag S) $^-o $ (target) $ (ldflags) # make all. C or. cpp%.O:%.c @echo "compiling:" $ (Addsuffix. C, $ (basename $ (notdir $@) ) @$ (CC) $ (CFLAGS)-C $<-o $@%.o:%.cpp @echo "compiling:" $ (addsuffix. CPP, $ (basename $ (notdir $@)) @$ (CXX) $ (CFLAGS)-C $<-o $@ clean: @echo "Cleaning ..." @$ (RM) $ (target) @$ (RM) *.o *.back *~.
Phony:all Clean
Li Yu 2015.9.14 Noon