我想從OpenGl上下文中製作視頻。RGB到x264:奇怪的顏色呈現
我使用glReadPixel,以確保RGB緩衝區數據是好的我將它保存到一個bmp文件,我可以正確讀取。
我的.h264視頻編碼,但有一些artefact,我不明白爲什麼。 我嘗試了很多不同的參數爲x264_param_t,但更好!
位圖保存(OpenGL的真實數據):Bitmap from OpenGl(1MO)
原H264錯誤:Raw h264 video(1MO)
的OpenGl提供ReadPixel:
int nSize = ClientHeight * ClientWidth * 3;
GLubyte *inBuff = new GLubyte[nSize];
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, ldwidth, ldheight, GL_BGR, GL_UNSIGNED_BYTE, inBuff);
】這個params定義:
x264_param_default(&mX264_param_t);
x264_param_default_preset(&mX264_param_t, "placebo", "film");
mX264_param_t.i_csp = X264_CSP_BGR;
mX264_param_t.i_threads = 6;
mX264_param_t.i_width = mWidth;
mX264_param_t.i_height = mHeight;
mX264_param_t.i_fps_num = mFps;
mX264_param_t.i_fps_den = 1;
// Intra refres:
mX264_param_t.i_keyint_max = mFps;
mX264_param_t.b_intra_refresh = 1;
//Rate control:
mX264_param_t.rc.i_rc_method = X264_RC_CRF;
mX264_param_t.rc.f_rf_constant = 25;
mX264_param_t.rc.f_rf_constant_max = 35;
int bps = 5000;
mX264_param_t.rc.i_bitrate = bps;
mX264_param_t.rc.i_vbv_max_bitrate = bps;
mX264_param_t.i_bframe = 2;
mX264_param_t.i_keyint_min = mFps/4;
//For streaming:
mX264_param_t.b_repeat_headers = 1;
mX264_param_t.b_annexb = 1;
mX264_param_t.i_log_level = X264_LOG_DEBUG;
x264_param_apply_profile(&mX264_param_t, "baseline");
mpEncoder = x264_encoder_open(&mX264_param_t);
x264_encoder_parameters(mpEncoder, &mX264_param_t);
mpPictureOut = new x264_picture_t();
mpPictureIn = new x264_picture_t();
x264_picture_alloc(mpPictureIn, X264_CSP_BGR | X264_CSP_VFLIP, mWidth, mHeight);
然後編碼循環:
mpPictureIn->img.i_csp = X264_CSP_BGR;
mpPictureIn->img.i_plane = 1;
mpPictureIn->img.i_stride[0] = 3 * mWidth;
mpPictureIn->img.plane[0] = rgbframe;
mpPictureIn->i_pts = mFrameCount;
mpPictureIn->i_type = X264_TYPE_AUTO;
mpPictureOut->i_pts = mFrameCount;
int i_nals;
x264_nal_t* nals;
int frame_size = x264_encoder_encode(mpEncoder, &nals, &i_nals, mpPictureIn, mpPictureOut);
if(frame_size > 0)
{
mpFileOut->write_frame(nals[0].p_payload, frame_size, mpPictureOut);
mFrameCount++;
}
寫入幀:
int TVideoFileWriter::write_frame(uint8_t *p_nalu, int i_size, x264_picture_t *p_picture)
{
if(fwrite(p_nalu, i_size, 1, mFileHandle))
return i_size;
return -1;
}
難道是顏色通道需要在不同的飛機? – bofjas
我仍然不確定爲什麼你的輸出被破壞(ffmpeg解碼錯誤從第一幀)。發佈你在write_frame中也做了什麼。順便說一句,這裏是你的代碼錯誤/提示的列表:1)不需要同時調用x264_param_default和x264_param_default_preset(x264_param_default_preset就足夠了)2)i_bitrate不影響任何與CRF 3)i_vbv_max_bitrate不會生效while你不會指定i_vbv_buffer_size也4)x264_param_apply_profile將失敗與基線和BGR輸出5)你可能需要使用x264_picture_init而不是x264_picture_alloc。 – nobody555
@bofjas:可能與否,我沒有找到關於libx264的正確文檔,因爲我沒有先天知識,我正在嘗試我能做的! nobody555:感謝所有的提示,但它沒有改變任何東西,我編輯我的第一篇文章與write_frame的主體。 – Maypeur