使用libx264將I420編碼為H 264

2021-07-02 07:32:21 字數 2004 閱讀 3659

libx264是乙個自由的h.264編碼庫,是x264專案的一部分,使用廣泛,ffmpeg的h.264實現就是用的libx264。

標頭檔案

1 #include 2 #include 3 #include 4 #include 5 #include 6 #include

變數宣告和引數

1

int width = 480;2

int height = 360;3

int fps = 25

;4 size_t yuv_size = width * height * 3 / 2

;5 x264_t *encoder;

6x264_picture_t pic_in, pic_out;

7int

inf, outf;

8 uint8_t *yuv_buffer;910

if (argc != 3

)

encoder就是編碼器,x264_t格式在x264.h檔案中只有

typedef struct x264_t x264_t
編碼器型別只需要也只能宣告為x264_t的指標型別

每次編碼時,yuv的資訊都儲存在pic_in中

輸入輸出的檔案描述符

從檔案讀入的yuv的緩衝區

初始化encoder

1

x264_param_t param;

2 x264_param_default_preset(¶m, "

veryfast

", "

zerolatency");

3 param.i_threads = 1

;4 param.i_width =width;

5 param.i_height =height;

6 param.i_fps_num =fps;

7 param.i_fps_den = 1;8

9 param.i_keyint_max = 25

;10 param.b_intra_refresh = 1;11

12 param.b_annexb = 1;13

baseline");

15 encoder = x264_encoder_open(¶m);

初始化pic_in

x264_picture_alloc(&pic_in, x264_csp_i420, width, height);

23 yuv_buffer =malloc(yuv_size);

45 pic_in.img.plane[0] =yuv_buffer;

6 pic_in.img.plane[1] = pic_in.img.plane[0] + width *height;

7 pic_in.img.plane[2] = pic_in.img.plane[1] + width * height / 4;

初始化檔案描述符

1 inf = open(argv[1

], o_rdonly);

2if (inf < 0

) 5 outf = open(argv[2], o_creat | o_wronly, 444);6

if (outf < 0

) 編碼

int64_t i_pts = 0;2

3 x264_nal_t *nals;

4int

nnal;

5while (read(inf, yuv_buffer, yuv_size) > 0

) 12 }

掃尾

x264_encoder_close(encoder);

2close(inf);

3close(outf);

4free(yuv_buffer);

5return

0;gcc sourcefile -lx264 -wall -o execfile

libx264編碼部分引數說明

html view plain copy 示例 x264 param default preset m x264param,ultrafast zerolatency ultrafast cpu占用最小,zerolatency 不快取幀 m x264param.i width width m x26...

libx264在VS2013中編譯

1.解除安裝系統中yasm。搜尋系統中的yasm.exe,yasm.props,yasm.xml,yasm.targetsvsyasm.exe,vsyasm.props,vsyasm.xml.vsyasm.targets。vs2013中一般yasm.exe安裝在c program files x86...

x264使用示例

note x264的編碼示例.使用x264的版本為libx264 115 1.示例是個死迴圈,會源源不斷的編碼,然後將資料寫檔案.2.示例的行為是 編碼1000幀後,取空編碼緩衝區,然後迴圈執行這兩步.author 戈 include include include include stdint.h...