Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "x264Encoder.h"
- x264Encoder::x264Encoder(void)
- {
- }
- x264Encoder::~x264Encoder(void)
- {
- }
- void x264Encoder::initilize()
- {
- x264_param_default_preset(¶meters, "ultrafast", "zerolatency");
- parameters.i_csp = X264_CSP_I420;
- parameters.i_log_level = X264_LOG_INFO;
- parameters.i_threads = 16;
- parameters.i_width = 1920;
- parameters.i_height = 1080;
- parameters.i_fps_num = 60;
- parameters.i_fps_den = 1;
- parameters.i_keyint_max = 60;
- parameters.b_intra_refresh = 1;
- parameters.rc.i_rc_method = X264_RC_CRF;
- parameters.rc.i_vbv_buffer_size = 2000;
- parameters.rc.i_vbv_max_bitrate = 4000;
- parameters.rc.f_rf_constant = 20;
- parameters.rc.f_rf_constant_max = 30;
- parameters.i_sps_id = 7;
- parameters.i_slice_max_size = 1500;
- parameters.b_repeat_headers = 1;
- parameters.b_annexb = 1;
- parameters.b_opencl = 1;
- parameters.i_nal_hrd = 0;
- parameters.rc.b_stat_read = 0;
- parameters.rc.b_stat_write = 1;
- x264_param_apply_profile(¶meters, "baseline");
- encoder = x264_encoder_open(¶meters);
- x264_picture_alloc(&picture_in, X264_CSP_I420, parameters.i_width, parameters.i_height);
- picture_in.i_type = X264_TYPE_AUTO;
- picture_in.img.i_csp = X264_CSP_I420;
- picture_in.img.i_plane = 3;
- picture_in.img.i_stride[0] = 1920;
- picture_in.img.i_stride[1] = 1920/2;
- picture_in.img.i_stride[2] = 1920/2;
- // 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
- //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);
- int width = 1920;
- int height = 1080;
- int ystride = width;
- int uvstride = width/2;
- int ysize = ystride * height;
- int vusize = uvstride * (height/2);
- int size = ysize + (2*vusize);
- yuvbasef = new char[size];
- }
- void x264Encoder::unInitilize()
- {
- x264_encoder_close(encoder);
- //sws_freeContext(convertContext);
- }
- void rgb_to_y420p(uint8_t* destination, uint8_t* rgb, size_t width, size_t height) {
- size_t image_size = width * height;
- size_t upos = image_size;
- size_t vpos = upos + upos / 4;
- size_t i = 0;
- for (size_t line = 0; line < height; line++) {
- if (!(line & 1)) {
- for (size_t x = 0; x < width; x += 2) {
- uint8_t r = rgb[4 * i];
- uint8_t g = rgb[4 * i + 1];
- uint8_t b = rgb[4 * i + 2];
- destination[i++] = (RY*r + GY*g + BY*b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
- //U and V are switched, deal with it
- destination[vpos++] = (RU*r + GU*g + BU*b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
- destination[upos++] = (RV*r + GV*g + BV*b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
- r = rgb[4 * i];
- g = rgb[4 * i + 1];
- b = rgb[4 * i + 2];
- destination[i++] = (RY*r + GY*g + BY*b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
- }
- } else {
- for (size_t x = 0; x < width; x++) {
- uint8_t r = rgb[4 * i];
- uint8_t g = rgb[4 * i + 1];
- uint8_t b = rgb[4 * i + 2];
- destination[i++] = (RY*r + GY*g + BY*b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
- }
- }
- }
- }
- void x264Encoder::encodeFrame(char* image)
- {
- //int srcStride = parameters.i_width * 3;
- //sws_scale(convertContext, &(image.data), &srcStride, 0, parameters.i_height, picture_in.img.plane, picture_in.img.i_stride);
- x264_nal_t* nals ;
- int i_nals = 0;
- int frameSize = -1;
- rgb_to_y420p((uint8_t*)yuvbasef, (uint8_t*)image, 1920, 1080);
- picture_in.img.plane[0] = (uint8_t*)yuvbasef;
- picture_in.img.plane[1] = (uint8_t*)yuvbasef + 1920 * 1080;
- picture_in.img.plane[2] = (uint8_t*)yuvbasef + 1920 * 1080 + 1920 * 1080 / 4;
- frameSize = x264_encoder_encode(encoder, &nals, &i_nals, &picture_in, &picture_out);
- if(frameSize > 0)
- {
- for(int i = 0; i< i_nals; i++)
- {
- outputQueue.push(nals[i]);
- }
- }
- }
- void x264Encoder::encodeFrame_no_convert(char* image)
- {
- //int srcStride = parameters.i_width * 3;
- //sws_scale(convertContext, &(image.data), &srcStride, 0, parameters.i_height, picture_in.img.plane, picture_in.img.i_stride);
- x264_nal_t* nals ;
- int i_nals = 0;
- int frameSize = -1;
- picture_in.img.plane[0] = (uint8_t*)image;
- picture_in.img.plane[1] = (uint8_t*)image + 1920 * 1080;
- picture_in.img.plane[2] = (uint8_t*)image + 1920 * 1080 + 1920 * 1080 / 4;
- frameSize = x264_encoder_encode(encoder, &nals, &i_nals, &picture_in, &picture_out);
- if(frameSize > 0)
- {
- for(int i = 0; i< i_nals; i++)
- {
- outputQueue.push(nals[i]);
- }
- }
- }
- bool x264Encoder::isNalsAvailableInOutputQueue()
- {
- if(outputQueue.empty() == true)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- x264_nal_t x264Encoder::getNalUnit()
- {
- x264_nal_t nal;
- nal = outputQueue.front();
- outputQueue.pop();
- return nal;
- }
Advertisement
Add Comment
Please, Sign In to add comment