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 
#include 
 
#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;iimg.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      

至此本节完成。

你可能感兴趣的:(嵌入式)