用x264和ffmpeg将YUV编码为.h264(1)

 

一、x264 将YUV编码为.h264

1.下载安装x264

./configure --prefix=/usr --enable-shared
make
make install 

2.用安装好的x264用命令将YUV编码为.h264

./x264 cuc_ieschool_640x360_yuv420p.yuv --input-res 640x360 --dump-yuv testv/fdec-cucies420p.yuv -o ieschool420p.h264 -v

yuv文件:

https://github.com/leixiaohua1020/simplest_encoder/blob/master/cuc_ieschool_640x360_yuv420p.yuv

https://github.com/leixiaohua1020/simplest_encoder/blob/master/cuc_ieschool_640x360_yuv444p.yuv

3.用C语言代码调用x264接口将YUV编码为.h264

编译命令:

gcc simplest_x264_encoder.cpp -g -o sx264encoder -lx264

代码如下

stdint.h

https://github.com/leixiaohua1020/simplest_encoder/blob/master/simplest_x264_encoder/stdint.h

simplest_x264_encoder.cpp

 
  1. /*

  2. * Simplest X264 Encoder

  3. *

  4. * Lei Xiaohua

  5. * [email protected]

  6. * Communication University of China / Digital TV Technology

  7. * http://blog.csdn.net/leixiaohua1020

  8. *

  9. * This software encode YUV data to H.264 bitstream.

  10. * It's the simplest encoder example based on libx264.

  11. *

  12. * modify:openswc

  13. *

  14. * build:

  15. * gcc simplest_x264_encoder.cpp -g -o sx264encoder -lx264

  16. *

  17. */

  18. #include <stdio.h>

  19. #include <stdlib.h>

  20.  
  21. #include "stdint.h"

  22.  
  23. //Linux...

  24. #ifdef __cplusplus

  25. extern "C"

  26. {

  27. #endif

  28. #include <x264.h>

  29. #ifdef __cplusplus

  30. };

  31. #endif

  32.  
  33.  
  34. int main(int argc, char** argv)

  35. {

  36.  
  37. int ret;

  38. int y_size;

  39. int i,j;

  40.  
  41. //FILE* fp_src = fopen("cuc_ieschool_640x360_yuv444p.yuv", "rb");

  42. FILE* fp_src = fopen("cuc_ieschool_640x360_yuv420p.yuv", "rb");

  43.  
  44. FILE* fp_dst = fopen("cuc_ieschool.h264", "wb");

  45.  
  46. //Encode 50 frame

  47. //if set 0, encode all frame

  48. int frame_num=50;

  49. int csp=X264_CSP_I420;

  50. int width=640,height=360;

  51.  
  52. int iNal = 0;

  53. x264_nal_t* pNals = NULL;

  54. x264_t* pHandle = NULL;

  55. x264_picture_t* pPic_in = (x264_picture_t*)malloc(sizeof(x264_picture_t));

  56. x264_picture_t* pPic_out = (x264_picture_t*)malloc(sizeof(x264_picture_t));

  57. x264_param_t* pParam = (x264_param_t*)malloc(sizeof(x264_param_t));

  58.  
  59. //Check

  60. if(fp_src==NULL||fp_dst==NULL){

  61. printf("Error open files.\n");

  62. return -1;

  63. }

  64.  
  65. x264_param_default(pParam);

  66. pParam->i_width = width;

  67. pParam->i_height = height;

  68. /*

  69. //Param

  70. pParam->i_log_level = X264_LOG_DEBUG;

  71. pParam->i_threads = X264_SYNC_LOOKAHEAD_AUTO;

  72. pParam->i_frame_total = 0;

  73. pParam->i_keyint_max = 10;

  74. pParam->i_bframe = 5;

  75. pParam->b_open_gop = 0;

  76. pParam->i_bframe_pyramid = 0;

  77. pParam->rc.i_qp_constant=0;

  78. pParam->rc.i_qp_max=0;

  79. pParam->rc.i_qp_min=0;

  80. pParam->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;

  81. pParam->i_fps_den = 1;

  82. pParam->i_fps_num = 25;

  83. pParam->i_timebase_den = pParam->i_fps_num;

  84. pParam->i_timebase_num = pParam->i_fps_den;

  85. */

  86. pParam->i_csp=csp;

  87. x264_param_apply_profile(pParam, x264_profile_names[5]);

  88.  
  89. pHandle = x264_encoder_open(pParam);

  90.  
  91. x264_picture_init(pPic_out);

  92. x264_picture_alloc(pPic_in, csp, pParam->i_width, pParam->i_height);

  93.  
  94. //ret = x264_encoder_headers(pHandle, &pNals, &iNal);

  95.  
  96. y_size = pParam->i_width * pParam->i_height;

  97. //detect frame number

  98. if(frame_num==0){

  99. fseek(fp_src,0,SEEK_END);

  100. switch(csp){

  101. case X264_CSP_I444:frame_num=ftell(fp_src)/(y_size*3);break;

  102. case X264_CSP_I420:frame_num=ftell(fp_src)/(y_size*3/2);break;

  103. default:printf("Colorspace Not Support.\n");return -1;

  104. }

  105. fseek(fp_src,0,SEEK_SET);

  106. }

  107.  
  108. //Loop to Encode

  109. for( i=0;i<frame_num;i++){

  110. switch(csp){

  111. case X264_CSP_I444:{

  112. fread(pPic_in->img.plane[0],y_size,1,fp_src); //Y

  113. fread(pPic_in->img.plane[1],y_size,1,fp_src); //U

  114. fread(pPic_in->img.plane[2],y_size,1,fp_src); //V

  115. break;}

  116. case X264_CSP_I420:{

  117. fread(pPic_in->img.plane[0],y_size,1,fp_src); //Y

  118. fread(pPic_in->img.plane[1],y_size/4,1,fp_src); //U

  119. fread(pPic_in->img.plane[2],y_size/4,1,fp_src); //V

  120. break;}

  121. default:{

  122. printf("Colorspace Not Support.\n");

  123. return -1;}

  124. }

  125. pPic_in->i_pts = i;

  126.  
  127. ret = x264_encoder_encode(pHandle, &pNals, &iNal, pPic_in, pPic_out);

  128. if (ret< 0){

  129. printf("Error.\n");

  130. return -1;

  131. }

  132.  
  133. printf("Succeed encode frame: %5d\n",i);

  134.  
  135. for ( j = 0; j < iNal; ++j){

  136. fwrite(pNals[j].p_payload, 1, pNals[j].i_payload, fp_dst);

  137. }

  138. }

  139. i=0;

  140. //flush encoder

  141. while(1){

  142. ret = x264_encoder_encode(pHandle, &pNals, &iNal, NULL, pPic_out);

  143. if(ret==0){

  144. break;

  145. }

  146. printf("Flush 1 frame.\n");

  147. for (j = 0; j < iNal; ++j){

  148. fwrite(pNals[j].p_payload, 1, pNals[j].i_payload, fp_dst);

  149. }

  150. i++;

  151. }

  152. x264_picture_clean(pPic_in);

  153. x264_encoder_close(pHandle);

  154. pHandle = NULL;

  155.  
  156. free(pPic_in);

  157. free(pPic_out);

  158. free(pParam);

  159.  
  160. fclose(fp_src);

  161. fclose(fp_dst);

  162.  
  163. return 0;

  164. }

猜你喜欢

转载自blog.csdn.net/weixin_37897683/article/details/81267674