最簡單的視頻編碼器:基於libx264(編碼YUV爲H.264)

http://blog.csdn.net/leixiaohua1020/article/details/42078645git

 

目錄(?)[+]app

 

 

=====================================================函數

最簡單的視頻編碼器系列文章列表:oop

最簡單的視頻編碼器:編譯

 

最簡單的視頻編碼器:基於libx264(編碼YUV爲H.264)post

最簡單的視頻編碼器:基於libx265(編碼YUV爲H.265)測試

最簡單的視頻編碼器:libvpx(編碼YUV爲VP8)編碼

=====================================================spa

本文記錄一個最簡單的基於libx264的H.264視頻編碼器。此前記錄的H.264編碼器都是基於FFmpeg調用libx264完成編碼的,例如:.net

 《最簡單的基於FFMPEG的視頻編碼器(YUV編碼爲H.264)》
相比與上文中的編碼器,本文記錄的編碼器屬於「輕量級」的編碼器。由於它再也不包含FFmpeg的代碼,直接調用libx264完成編碼。所以項目的體積很是小巧。該編碼器能夠將輸入的YUV數據編碼爲H.264碼流文件。
 

流程圖

調用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」模塊,該模塊使用的函數和編碼模塊是同樣的。惟一的不一樣在於再也不輸入視頻像素數據。它的做用在於輸出編碼器中剩餘的碼流數據。
 

源代碼

[cpp]  view plain  copy
 
  1. /** 
  2.  * 最簡單的基於X264的視頻編碼器 
  3.  * Simplest X264 Encoder 
  4.  * 
  5.  * 雷霄驊 Lei Xiaohua 
  6.  * leixiaohua1020@126.com 
  7.  * 中國傳媒大學/數字電視技術 
  8.  * Communication University of China / Digital TV Technology 
  9.  * http://blog.csdn.net/leixiaohua1020 
  10.  * 
  11.  * 本程序能夠YUV格式的像素數據編碼爲H.264碼流,是最簡單的 
  12.  * 基於libx264的視頻編碼器 
  13.  * 
  14.  * This software encode YUV data to H.264 bitstream. 
  15.  * It's the simplest encoder example based on libx264. 
  16.  */  
  17. #include <stdio.h>  
  18. #include <stdlib.h>  
  19.    
  20. #include "stdint.h"  
  21.    
  22. #if defined ( __cplusplus)  
  23. extern "C"  
  24. {  
  25. #include "x264.h"  
  26. };  
  27. #else  
  28. #include "x264.h"  
  29. #endif  
  30.    
  31.    
  32. int main(int argc, char** argv)  
  33. {  
  34.    
  35.          int ret;  
  36.          int y_size;  
  37.          int i,j;  
  38.    
  39.          //FILE* fp_src  = fopen("../cuc_ieschool_640x360_yuv444p.yuv", "rb");  
  40.          FILE* fp_src  = fopen("../cuc_ieschool_640x360_yuv420p.yuv", "rb");  
  41.    
  42.          FILE* fp_dst = fopen("cuc_ieschool.h264", "wb");  
  43.           
  44.          //Encode 50 frame  
  45.          //if set 0, encode all frame  
  46.          int frame_num=50;  
  47.          int csp=X264_CSP_I420;  
  48.          int width=640,height=360;  
  49.    
  50.          int iNal   = 0;  
  51.          x264_nal_t* pNals = NULL;  
  52.          x264_t* pHandle   = NULL;  
  53.          x264_picture_t* pPic_in = (x264_picture_t*)malloc(sizeof(x264_picture_t));  
  54.          x264_picture_t* pPic_out = (x264_picture_t*)malloc(sizeof(x264_picture_t));  
  55.          x264_param_t* pParam = (x264_param_t*)malloc(sizeof(x264_param_t));  
  56.           
  57.          //Check  
  58.          if(fp_src==NULL||fp_dst==NULL){  
  59.                    printf("Error open files.\n");  
  60.                    return -1;  
  61.          }  
  62.    
  63.          x264_param_default(pParam);  
  64.          pParam->i_width   = width;  
  65.          pParam->i_height  = height;  
  66.          /* 
  67.          //Param 
  68.          pParam->i_log_level  = X264_LOG_DEBUG; 
  69.          pParam->i_threads  = X264_SYNC_LOOKAHEAD_AUTO; 
  70.          pParam->i_frame_total = 0; 
  71.          pParam->i_keyint_max = 10; 
  72.          pParam->i_bframe  = 5; 
  73.          pParam->b_open_gop  = 0; 
  74.          pParam->i_bframe_pyramid = 0; 
  75.          pParam->rc.i_qp_constant=0; 
  76.          pParam->rc.i_qp_max=0; 
  77.          pParam->rc.i_qp_min=0; 
  78.          pParam->i_bframe_adaptive = X264_B_ADAPT_TRELLIS; 
  79.          pParam->i_fps_den  = 1; 
  80.          pParam->i_fps_num  = 25; 
  81.          pParam->i_timebase_den = pParam->i_fps_num; 
  82.          pParam->i_timebase_num = pParam->i_fps_den; 
  83.          */  
  84.          pParam->i_csp=csp;  
  85.          x264_param_apply_profile(pParam, x264_profile_names[5]);  
  86.           
  87.          pHandle = x264_encoder_open(pParam);  
  88.      
  89.          x264_picture_init(pPic_out);  
  90.          x264_picture_alloc(pPic_in, csp, pParam->i_width, pParam->i_height);  
  91.    
  92.          //ret = x264_encoder_headers(pHandle, &pNals, &iNal);  
  93.    
  94.          y_size = pParam->i_width * pParam->i_height;  
  95.          //detect frame number  
  96.          if(frame_num==0){  
  97.                    fseek(fp_src,0,SEEK_END);  
  98.                    switch(csp){  
  99.                    case X264_CSP_I444:frame_num=ftell(fp_src)/(y_size*3);break;  
  100.                    case X264_CSP_I420:frame_num=ftell(fp_src)/(y_size*3/2);break;  
  101.                    default:printf("Colorspace Not Support.\n");return -1;  
  102.                    }  
  103.                    fseek(fp_src,0,SEEK_SET);  
  104.          }  
  105.           
  106.          //Loop to Encode  
  107.          for( i=0;i<frame_num;i++){  
  108.                    switch(csp){  
  109.                    case X264_CSP_I444:{  
  110.                             fread(pPic_in->img.plane[0],y_size,1,fp_src);         //Y  
  111.                             fread(pPic_in->img.plane[1],y_size,1,fp_src);         //U  
  112.                             fread(pPic_in->img.plane[2],y_size,1,fp_src);         //V  
  113.                             break;}  
  114.                    case X264_CSP_I420:{  
  115.                             fread(pPic_in->img.plane[0],y_size,1,fp_src);         //Y  
  116.                             fread(pPic_in->img.plane[1],y_size/4,1,fp_src);     //U  
  117.                             fread(pPic_in->img.plane[2],y_size/4,1,fp_src);     //V  
  118.                             break;}  
  119.                    default:{  
  120.                             printf("Colorspace Not Support.\n");  
  121.                             return -1;}  
  122.                    }  
  123.                    pPic_in->i_pts = i;  
  124.    
  125.                    ret = x264_encoder_encode(pHandle, &pNals, &iNal, pPic_in, pPic_out);  
  126.                    if (ret< 0){  
  127.                             printf("Error.\n");  
  128.                             return -1;  
  129.                    }  
  130.    
  131.                    printf("Succeed encode frame: %5d\n",i);  
  132.    
  133.                    for ( j = 0; j < iNal; ++j){  
  134.                              fwrite(pNals[j].p_payload, 1, pNals[j].i_payload, fp_dst);  
  135.                    }  
  136.          }  
  137.          i=0;  
  138.          //flush encoder  
  139.          while(1){  
  140.                    ret = x264_encoder_encode(pHandle, &pNals, &iNal, NULL, pPic_out);  
  141.                    if(ret==0){  
  142.                             break;  
  143.                    }  
  144.                    printf("Flush 1 frame.\n");  
  145.                    for (j = 0; j < iNal; ++j){  
  146.                             fwrite(pNals[j].p_payload, 1, pNals[j].i_payload, fp_dst);  
  147.                    }  
  148.                    i++;  
  149.          }  
  150.          x264_picture_clean(pPic_in);  
  151.          x264_encoder_close(pHandle);  
  152.          pHandle = NULL;  
  153.    
  154.          free(pPic_in);  
  155.          free(pPic_out);  
  156.          free(pParam);  
  157.    
  158.          fclose(fp_src);  
  159.          fclose(fp_dst);  
  160.    
  161.          return 0;  
  162. }  

運行結果

程序的輸入爲一個YUV文件(已經測試過YUV444P和YUV420P兩種格式)。

輸出爲H.264碼流文件。

H.264碼流文件的信息以下所示。

下載

 

Simplest Encoder

 

項目主頁

SourceForge:https://sourceforge.net/projects/simplestencoder/

Github:https://github.com/leixiaohua1020/simplest_encoder

開源中國:http://git.oschina.net/leixiaohua1020/simplest_encoder

 

CDSN下載地址:http://download.csdn.net/detail/leixiaohua1020/8284105

  該解決方案包含了幾個常見的編碼器的使用示例: simplest_vpx_encoder:最簡單的基於libvpx的視頻編碼器 simplest_x264_encoder:最簡單的基於libx264的視頻編碼器 simplest_x265_encoder:最簡單的基於libx265的視頻編碼器
相關文章
相關標籤/搜索