Guest User

Untitled

a guest
Dec 27th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. #include "x264Encoder.h"
  2.  
  3. x264Encoder::x264Encoder(void)
  4. {
  5.  
  6. }
  7.  
  8.  
  9. x264Encoder::~x264Encoder(void)
  10. {
  11.  
  12. }
  13.  
  14. void x264Encoder::initilize()
  15. {
  16. x264_param_default_preset(&parameters, "ultrafast", "zerolatency");
  17.  
  18.  
  19. parameters.i_csp = X264_CSP_I420;
  20. parameters.i_log_level = X264_LOG_INFO;
  21. parameters.i_threads = 16;
  22. parameters.i_width = 1920;
  23. parameters.i_height = 1080;
  24. parameters.i_fps_num = 60;
  25. parameters.i_fps_den = 1;
  26. parameters.i_keyint_max = 60;
  27. parameters.b_intra_refresh = 1;
  28. parameters.rc.i_rc_method = X264_RC_CRF;
  29. parameters.rc.i_vbv_buffer_size = 2000;
  30. parameters.rc.i_vbv_max_bitrate = 4000;
  31. parameters.rc.f_rf_constant = 20;
  32. parameters.rc.f_rf_constant_max = 30;
  33. parameters.i_sps_id = 7;
  34. parameters.i_slice_max_size = 1500;
  35. parameters.b_repeat_headers = 1;
  36. parameters.b_annexb = 1;
  37. parameters.b_opencl = 1;
  38. parameters.i_nal_hrd = 0;
  39.  
  40. parameters.rc.b_stat_read = 0;
  41. parameters.rc.b_stat_write = 1;
  42.  
  43. x264_param_apply_profile(&parameters, "baseline");
  44.  
  45. encoder = x264_encoder_open(&parameters);
  46. x264_picture_alloc(&picture_in, X264_CSP_I420, parameters.i_width, parameters.i_height);
  47. picture_in.i_type = X264_TYPE_AUTO;
  48. picture_in.img.i_csp = X264_CSP_I420;
  49. picture_in.img.i_plane = 3;
  50.  
  51. picture_in.img.i_stride[0] = 1920;
  52. picture_in.img.i_stride[1] = 1920/2;
  53. picture_in.img.i_stride[2] = 1920/2;
  54. // i have initilized my color space converter for BGR24 to YUV420 because my opencv video capture gives BGR24 image. You can initilize according to your input pixelFormat
  55. //convertContext = sws_getContext(parameters.i_width,parameters.i_height, PIX_FMT_BGR24, parameters.i_width,parameters.i_height,PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
  56.  
  57. int width = 1920;
  58. int height = 1080;
  59. int ystride = width;
  60. int uvstride = width/2;
  61. int ysize = ystride * height;
  62. int vusize = uvstride * (height/2);
  63. int size = ysize + (2*vusize);
  64.  
  65. yuvbasef = new char[size];
  66. }
  67.  
  68. void x264Encoder::unInitilize()
  69. {
  70. x264_encoder_close(encoder);
  71. //sws_freeContext(convertContext);
  72. }
  73.  
  74. void rgb_to_y420p(uint8_t* destination, uint8_t* rgb, size_t width, size_t height) {
  75. size_t image_size = width * height;
  76. size_t upos = image_size;
  77. size_t vpos = upos + upos / 4;
  78. size_t i = 0;
  79.  
  80. for (size_t line = 0; line < height; line++) {
  81. if (!(line & 1)) {
  82. for (size_t x = 0; x < width; x += 2) {
  83. uint8_t r = rgb[4 * i];
  84. uint8_t g = rgb[4 * i + 1];
  85. uint8_t b = rgb[4 * i + 2];
  86.  
  87. destination[i++] = (RY*r + GY*g + BY*b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
  88.  
  89. //U and V are switched, deal with it
  90. destination[vpos++] = (RU*r + GU*g + BU*b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
  91. destination[upos++] = (RV*r + GV*g + BV*b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
  92.  
  93. r = rgb[4 * i];
  94. g = rgb[4 * i + 1];
  95. b = rgb[4 * i + 2];
  96.  
  97. destination[i++] = (RY*r + GY*g + BY*b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
  98. }
  99. } else {
  100. for (size_t x = 0; x < width; x++) {
  101. uint8_t r = rgb[4 * i];
  102. uint8_t g = rgb[4 * i + 1];
  103. uint8_t b = rgb[4 * i + 2];
  104.  
  105. destination[i++] = (RY*r + GY*g + BY*b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
  106. }
  107. }
  108. }
  109. }
  110.  
  111. void x264Encoder::encodeFrame(char* image)
  112. {
  113. //int srcStride = parameters.i_width * 3;
  114. //sws_scale(convertContext, &(image.data), &srcStride, 0, parameters.i_height, picture_in.img.plane, picture_in.img.i_stride);
  115. x264_nal_t* nals ;
  116. int i_nals = 0;
  117. int frameSize = -1;
  118.  
  119.  
  120. rgb_to_y420p((uint8_t*)yuvbasef, (uint8_t*)image, 1920, 1080);
  121.  
  122. picture_in.img.plane[0] = (uint8_t*)yuvbasef;
  123. picture_in.img.plane[1] = (uint8_t*)yuvbasef + 1920 * 1080;
  124. picture_in.img.plane[2] = (uint8_t*)yuvbasef + 1920 * 1080 + 1920 * 1080 / 4;
  125.  
  126.  
  127.  
  128. frameSize = x264_encoder_encode(encoder, &nals, &i_nals, &picture_in, &picture_out);
  129. if(frameSize > 0)
  130. {
  131. for(int i = 0; i< i_nals; i++)
  132. {
  133. outputQueue.push(nals[i]);
  134. }
  135. }
  136. }
  137.  
  138. void x264Encoder::encodeFrame_no_convert(char* image)
  139. {
  140. //int srcStride = parameters.i_width * 3;
  141. //sws_scale(convertContext, &(image.data), &srcStride, 0, parameters.i_height, picture_in.img.plane, picture_in.img.i_stride);
  142. x264_nal_t* nals ;
  143. int i_nals = 0;
  144. int frameSize = -1;
  145.  
  146. picture_in.img.plane[0] = (uint8_t*)image;
  147. picture_in.img.plane[1] = (uint8_t*)image + 1920 * 1080;
  148. picture_in.img.plane[2] = (uint8_t*)image + 1920 * 1080 + 1920 * 1080 / 4;
  149.  
  150.  
  151.  
  152. frameSize = x264_encoder_encode(encoder, &nals, &i_nals, &picture_in, &picture_out);
  153. if(frameSize > 0)
  154. {
  155. for(int i = 0; i< i_nals; i++)
  156. {
  157. outputQueue.push(nals[i]);
  158. }
  159. }
  160. }
  161.  
  162. bool x264Encoder::isNalsAvailableInOutputQueue()
  163. {
  164. if(outputQueue.empty() == true)
  165. {
  166. return false;
  167. }
  168. else
  169. {
  170. return true;
  171. }
  172. }
  173.  
  174. x264_nal_t x264Encoder::getNalUnit()
  175. {
  176. x264_nal_t nal;
  177. nal = outputQueue.front();
  178. outputQueue.pop();
  179. return nal;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment