jdb
jdb

Reputation: 51

x265 encoder, convert yuv to x265_picture

I'm writing an application in C++ and I want to convert a yuv picture that I get from the webcam to an x265_picture, so that it can be encode to hevc. I followed the tutorial on http://x265.readthedocs.org/en/default/api.html#introduction. But there is nothing on how you can convert yuv to x265_picture. How can this be accomplished?

x265_param *param = x265_param_alloc();

x265_param_default_preset(param, "ultrafast", "zerolatency");

x265_param_parse(param, "fps", "30");
x265_param_parse(param, "input-res", "352x288"); //wxh
x265_param_parse(param, "bframes", "0");
x265_param_parse(param, "rc-lookahead", "20");
x265_param_parse(param, "repeat-headers", "1");

x265_picture *pic_in = x265_picture_alloc();
x265_picture *pic_out = x265_picture_alloc();

x265_picture_init(param, pic_in);

x265_nal *pp_nal;
uint32_t pi_nal;
x265_encoder *encoder = x265_encoder_open(param);
x265_encoder_encode(encoder, &pp_nal, &pi_nal, pic_in, pic_out);

Upvotes: 1

Views: 1374

Answers (1)

mpromonet
mpromonet

Reputation: 11942

By defaut x265 set colorspace type to i420 (alias yuv 4:2:0 planar), you can check this printing it :

x265_picture_init(param, pic_in);       
fprintf(stderr,"colorSpace:%s\n", x265_source_csp_names[pic_in->colorSpace]);

In order to link the input x265_picture structure to your i420 buffer, you should use something like :

pic_in->stride[0] = param->sourceWidth;
pic_in->stride[1] = pic_in->stride[0] >> x265_cli_csps[pic_in->colorSpace].width[1];
pic_in->stride[2] = pic_in->stride[0] >> x265_cli_csps[pic_in->colorSpace].width[2];
pic_in->planes[0] = i420_buffer;
pic_in->planes[1] = (char*)pic_in->planes[0] + pic_in->stride[0] * param->sourceHeight;
pic_in->planes[2] = (char*)pic_in->planes[1] + pic_in->stride[1] * (param->sourceHeight >> x265_cli_csps[pic_in->colorSpace].height[1]);    

If the yuv format of your webcam is an other one, you could either set colorspace type (if libx265 support it), or use libyuv to convert to i420 format.

Upvotes: 1

Related Questions