libx264編碼---YUV影象資料編碼為h.264碼流
編譯環境:ubuntu12.04
目標平臺:ARM Cortex A9
交叉編譯器:arm-none-linux-gnueabi-gcc 4.4.1
基本步驟:
一、移植x264庫到ARM板,請看上一篇博文移植x264編碼庫
二、測試:
呼叫x264庫將YUV碼流檔案編碼壓縮成H.264視訊檔案,程式碼如下(該程式碼來自雷神博文最簡單的視訊編碼器:基於libx264(編碼YUV為H.264),並參考博友文章通過V4L2採集yuv資料,並用x264壓縮資料成H264格式的檔案修改新增支援YUV422格式編碼):
流程圖
呼叫libx264進行視訊編碼的流程圖如下所示。
流程圖中主要的函式如下所示。
x264_param_default():設定引數集結構體x264_param_t的預設值。
x264_picture_alloc():為影象結構體x264_picture_t分配記憶體。
x264_encoder_open():開啟編碼器。
x264_encoder_encode():編碼一幀影象。
x264_encoder_close():關閉編碼器。
x264_picture_clean():釋放x264_picture_alloc()申請的資源。
儲存資料的結構體如下所示。
x264_picture_t:儲存壓縮編碼前的畫素資料。
x264_nal_t:儲存壓縮編碼後的碼流資料。
此外流程圖中還包括一個“flush_encoder”模組,該模組使用的函式和編碼模組是一樣的。唯一的不同在於不再輸入視訊畫素資料。它的作用在於輸出編碼器中剩餘的碼流資料。
x264編碼引數設定
H264Encoder *pEn = (H264Encoder *) AllocBuffer(sizeof(H264Encoder)); pEn->param = (x264_param_t *) AllocBuffer(sizeof(x264_param_t)); x264_param_default(pEn->param); //set default param /*設定profile屬性*/ //x264_profile_names陣列定義如下:static const char * const x264_profile_names[] = //{ "baseline", "main", "high", "high10", "high422", "high444", 0 }; x264_param_apply_profile(pEn->param,x264_profile_names[0]); pEn->param->i_csp = X264_CSP_I420;// 設定幀資料格式為420 pEn->param->i_width = m_nPicW; // 設定幀寬度 pEn->param->i_height = m_nPicH; // 設定幀高度 pEn->param->rc.i_lookahead = 0; pEn->param->i_fps_num = m_nFps; // 設定幀率(分子) pEn->param->i_fps_den = 1; // 設定幀率時間1s(分母) pEn->param->rc.i_bitrate = m_nBitrate; // 設定位元速率 pEn->param->rc.i_vbv_max_bitrate = pCodecParam->encoder.iMaxBitrate; // 設定平均位元速率模式下,最大瞬時位元速率 pEn->param->i_keyint_max = m_nFps * 2; // 設定GOP最大長度 pEn->param->i_keyint_min = m_nFps * 2; // 設定GOP最小長度 pEn->param->pf_log = x264_log; // 設定列印日誌回撥 pEn->param->p_log_private = NULL; pEn->param->i_log_level = X264_LOG_WARNING; pEn->param->b_vfr_input = 0; // 1:使用timebase和時間戳做位元速率控制 0:只使用fps做位元速率控制 pEn->param->i_timebase_num = 1; // timebase(分子) pEn->param->i_timebase_den = 1000; // timebase(分母) pEn->param->b_repeat_headers = 0; // 1:在每個關鍵幀前面放sps和pps pEn->param->i_threads = 1; // 並行編碼多個幀執行緒數 pEn->param->rc.i_rc_method = X264_RC_ABR; // 位元速率控制方法,CQP(恆定質量),CRF(恆定位元速率),ABR(平均位元速率) pEn->param->rc.i_vbv_buffer_size = pCodecParam->encoder.iMaxBitrate; //位元速率控制緩衝區的大小,單位kbit,預設0 pEn->handle = x264_encoder_open(pEn->param); // 建立一個新的編碼器控制代碼
編碼實現:
/**
* 最簡單的基於X264的視訊編碼器
* Simplest X264 Encoder
* jiangyu
*
* 本程式可以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"
#if defined ( __cplusplus)
extern "C"
{
#include "x264.h"
};
#else
#include "x264.h"
#endif
int width=640;
int height=480;
int csp=X264_CSP_I420;
int main(int argc, char** argv)
{
int ret;
int y_size;
int i,j;
FILE* fp_src = fopen("./yuv420p_640x480.yuv", "rb");
FILE* fp_dst = fopen("output.h264", "wb");
//Encode frame number
//if set 0, encode all frame
int frame_num=0;
int iNal = 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); //給引數結構體賦預設值
x264_param_default_preset(pParam, "fast" , "zerolatency" ); //設定preset和tune
//修改部分引數
pParam->i_csp=csp;
pParam->i_width = width; // 寬度
pParam->i_height = height; // 高度
pParam->i_fps_num = 25; // 設定幀率(分子)
pParam->i_fps_den = 1; // 設定幀率時間1s(分母)
pParam->i_threads = X264_SYNC_LOOKAHEAD_AUTO;
pParam->i_keyint_max = 10; //在此間隔設定IDR關鍵幀
pParam->rc.i_bitrate = 1200; // 設定位元速率,在ABR(平均位元速率)模式下才生效,且必須在設定ABR前先設定bitrate
pParam->rc.i_rc_method = X264_RC_ABR; // 位元速率控制方法,CQP(恆定質量),CRF(恆定位元速率,預設值23),ABR(平均位元速率)
/*
//Param
pParam->i_log_level = X264_LOG_DEBUG;
pParam->i_frame_total = 0;
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_timebase_den = pParam->i_fps_num;
pParam->i_timebase_num = pParam->i_fps_den;
*/
//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);
//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){
case X264_CSP_I444:frame_num=ftell(fp_src)/(y_size*3);break;
case X264_CSP_I422:frame_num=ftell(fp_src)/(y_size*2);break;
case X264_CSP_I420:frame_num=ftell(fp_src)/(y_size*3/2);break;
default:printf("Colorspace Not Support.\n");return -1;
}
fseek(fp_src,0,SEEK_SET);
}
//Loop to Encode
for( i=0;i<frame_num;i++){
switch(csp){
case X264_CSP_I444:{
fread(pPic_in->img.plane[0],y_size,1,fp_src); //Y
fread(pPic_in->img.plane[1],y_size,1,fp_src); //U
fread(pPic_in->img.plane[2],y_size,1,fp_src); //V
break;}
case X264_CSP_I422:{
int index = 0;
int y_i=0,u_i=0,v_i=0;
for(index = 0 ; index < y_size*2 ;){
fread(&pPic_in->img.plane[0][y_i++],1,1,fp_src); //Y
index++;
fread(&pPic_in->img.plane[1][u_i++],1,1,fp_src); //U
index++;
fread(&pPic_in->img.plane[0][y_i++],1,1,fp_src); //Y
index++;
fread(&pPic_in->img.plane[2][v_i++],1,1,fp_src); //V
index++;
}break;}
case X264_CSP_I420:{
fread(pPic_in->img.plane[0],y_size,1,fp_src); //Y
fread(pPic_in->img.plane[1],y_size/4,1,fp_src); //U
fread(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){
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){
break;
}
printf("Flush 1 frame.\n");
for (j = 0; j < iNal; ++j){
fwrite(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;
}
PC端ubuntu下交叉編譯:
arm-none-linux-gnueabi-gcc x264_coder.c -o x264_coder -L/home/jiangyu/software/x264/x264-snapshot-20181014-2245/_install/lib/ -I/home/jiangyu/software/x264/x264-snapshot-20181014-2245/_install/include/ -lx264
生成可執行檔案x264_coder,拷貝至目標板進行編碼測試:
目標板上執行:./x264_coder
生成output_image.h264視訊檔案,利用VLC播放器可開啟觀看
編碼前檔案大小out_image.yuv 58.5M
編碼壓縮後文件大小out_image.h264 360KB
至此本節完成。