1 首先升級用 vs2015 來編譯開發 windows 下的程序,因為更好的兼容 c99 語法,不用改很多東西。
libx264 的編譯:
在 ubuntu 下的確非常容易,windows 平台有點麻煩,需要用到 MYSY2 ,現在這些開源項目,有的都不提供 vs 的工程,哪怕提供個 cmake 也行啊。
找到一個老版本的 libx264 http://download.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20091006-2245.tar.bz2 帶有 vs 的工程。
http://yasm.tortall.net/ 需要安裝這個,匯編器,速度比較快吧。
使用 vs 2015 打開解決方案,libx264 編譯成功 x264 測試程序不行。
有幾個函數找不到 _x264_lookahead_init _x264_lookahead_is_empt _x264_lookahead_put_frame ....
添加 encoder\lookahead.c 到lib ,重新編譯成功2個。
========== 全部重新生成: 成功 2 個,失敗 0 個,跳過 0 個 ==========
2 使用MSYS2 編譯
安裝包下載
https://github.com/msys2/msys2-installer
pacman -S base-devel
pacman -S gcc
32位 64位安裝不同
pacman -S mingw-w64-i686-toolchain
pacman -S mingw-w64-x86_64-toolchain
編譯最新版x264
/x264-master
./configure --enable-shared --disable-asm --enable-debug --disable-thread --disable-avs --extra-ldflags=-Wl,--output-def=libx264.def
編譯成功,生成了libx264.a libx264.def libx264.dll.a libx264-159.dll x264.exe 使用 Visual Studio 2015 工具命令提示,轉為 libx264.lib
LIB /DEF:libx264.def
Microsoft (R) Library Manager Version 14.00.24215.1
Copyright (C) Microsoft Corporation. All rights reserved.
LINK : warning LNK4068: 未指定 /MACHINE;默認設置為 X86
正在創建庫 libx264.lib 和對象 libx264.exp
復制 x264_config.h x264.h libx264-159.dll libx264.lib 到 vs2015 工程
實際運行的時候,還需要 msys32\usr\bin 拷一些 dll 提示缺什么就復制過來,msys-2.0.dll libwinpthread-1.dll libgcc_s_dw2-1.dll 等
1,RGB 轉 YUV
ffmpeg 方法
1 read_jpeg(file_image, &video_width, &video_height, &image_buff); 2 3 uint8_t *indata[AV_NUM_DATA_POINTERS] = { 0 }; 4 indata[0] = (uint8_t *)image_buff; 5 int inlinesize[AV_NUM_DATA_POINTERS] = { 0 }; 6 inlinesize[0] = frame->width * 3; 7 8 ret = sws_scale(sws_ctx, indata, inlinesize, 0, frame->height, frame->data, frame->linesize); 9 10 for (int j = 0; j < 3; j++) 11 { 12 printf("j:%d linesize in:%d out:%d\n", j, inlinesize[j], frame->linesize[j]); 13 }
get_video_size: 480*272
file_image:img/image-00001.jpg
j:0 linesize in:1440 out:480
j:1 linesize in:0 out:256
j:2 linesize in:0 out:256
RGB 數據被轉為 YUV 數組,那么怎么取出來呢?
1 //方法1 2 if (0 > x264_picture_alloc(&m_pic, m_param.i_csp, m_param.i_width, m_param.i_height*2)) 3 { 4 printf("x264 [error]: malloc failed\n"); 5 return -1; 6 } 7 8 memcpy(m_pic.img.plane[0], frame->data[0], frame->linesize[0]*frame->height); 9 m_pic.img.i_stride[0] = frame->linesize[0]; 10 11 memcpy(m_pic.img.plane[1], frame->data[1], frame->linesize[1]*frame->height/2); 12 m_pic.img.i_stride[1] = frame->linesize[1]; 13 14 memcpy(m_pic.img.plane[2], frame->data[2], frame->linesize[2]*frame->height/2); 15 m_pic.img.i_stride[2] = frame->linesize[2]; 16 17 //方法2 18 if (0 > x264_picture_alloc(&m_pic, m_param.i_csp, m_param.i_width, m_param.i_height)) 19 { 20 printf("x264 [error]: malloc failed\n"); 21 return -1; 22 } 23 sws_scale(sws_ctx, indata, inlinesize, 0, frame->height, m_pic.img.plane, m_pic.img.i_stride);
有2種方法,如果手動 memcpy 則需要將圖片內存申請的大一些,因為有對齊的問題,如果用 ffmpeg 傳入 m_pic.img.plane, m_pic.img.i_stride 就不需要,ffmpeg 內部應該是做了 realloc。
參考 example.c 編寫測試程序,x264_encoder_encode() i_nal 一直為 0。所以重新編譯了靜態庫,方便調試。
#x264-master# ./configure --prefix=/usr --disable-asm --enable-static --enable-shared --enable-debug
編譯測試程序 :g++ encode_video.cpp -lavcodec -lavutil -lswscale -lswresample -lavformat -ljpeg libx264.a -lpthread -ldl
x264_encoder_encode() 返回一直為0的問題,調了好幾個小時,想了各種方法,改了各種參數,都沒有效果,最后終於找到問題了,原來是測試的 YUV 數據太少。
jpg 圖片轉的 YUV 用了40幀后才出現編碼數據。
1 x264 [debug]: frame= 0 QP=19.71 NAL=3 Slice:I Poc:0 I:396 P:0 SKIP:0 size=9052 bytes 2 frame_size:9052 3 file_image:img//image-00042.jpg 4 Send frame 41 5 x264 [debug]: frame= 1 QP=20.29 NAL=2 Slice:P Poc:2 I:6 P:140 SKIP:250 size=836 bytes 6 frame_size:836 7 file_image:img//image-00043.jpg 8 Send frame 42 9 x264 [debug]: frame= 2 QP=19.75 NAL=2 Slice:P Poc:4 I:0 P:43 SKIP:353 size=132 bytes 10 frame_size:132
參數設置,每10幀產生一個 關鍵 I 幀 ,同時禁用了 B 幀,因為我們要做 live 直播,去掉了。
m_param.i_frame_reference = 10;
m_param.i_bframe = 0;
