c++ - Convert OpenGL output to H264 with x264 -


i want convert output of opengl program h264 , stream output. got collected of code somewhere , output file have no idea it, or if valid. output saved in file.h264.

edit: "global" variables

    x264_param_t param;     x264_t* encoder;     x264_picture_t pic_in;     x264_picture_t pic_out;      x264_nal_t *headers;     int i_nal;     file* pfile; 

my init function:

initx264() {     pfile = fopen("file.h264", "wb");      x264_param_t param;     x264_param_default_preset(&param, "veryfast", "zerolatency");     param.i_threads = 1;     param.i_width = 1024;     param.i_height = 768;     param.i_fps_num = 30;     param.i_fps_den = 1;      param.i_keyint_max = 30;     param.b_intra_refresh = 1;      param.rc.i_rc_method = x264_rc_crf;     param.rc.f_rf_constant = 25;     param.rc.f_rf_constant_max = 35;      param.b_annexb = 0;     param.b_repeat_headers = 0;      param.i_log_level = x264_log_debug;      x264_param_apply_profile(&param, "baseline");      encoder = x264_encoder_open(&param);     x264_picture_alloc(&pic_in, x264_csp_i420, 1024, 768);      x264_encoder_parameters( encoder, &param );      x264_encoder_headers( encoder, &headers, &i_nal );      int size = headers[0].i_payload + headers[1].i_payload + headers[2].i_payload;     fwrite( headers[0].p_payload, 1, size, pfile); } 

this goes in render function , executed 30 times per second:

    glubyte *data = new glubyte[3 * 1024 * 768];     glubyte *pixelyuv = new glubyte[3 * 1024 * 768];      glreadpixels(0, 0, 1024, 768, gl_rgb, gl_unsigned_byte, data);     rgb2yuv(1024, 768, data, pixelyuv, pixelyuv + 1024 * 768, pixelyuv + 1024 * 768 + (1024 * 768) / 4, true);     pic_in.img.plane[0] = pixelyuv;     pic_in.img.plane[1] = pixelyuv + 1024 * 768;     pic_in.img.plane[2] = pixelyuv + 1024 * 768 + (1024 * 768) / 4;      x264_nal_t* nals;     int i_nals;     int frame_size = x264_encoder_encode(encoder, &nals, &i_nals, &pic_in, &pic_out);      if( frame_size )     {         fwrite( (char*)nals[0].p_payload, frame_size, 1, pfile );      } 

i got grb2yuv funktion http://svn.gnumonks.org/trunk/21c3-video/cutting_tagging/tools/mpeg4ip-1.2/server/util/rgb2yuv/rgb2yuv.c

the output looks like

x264 [debug]: frame=   0 qp=11.14 nal=3 slice:i poc:0   i:3072 p:0    skip:0    size=21133 bytes x264 [debug]: frame=   1 qp=20.08 nal=2 slice:p poc:2   i:0    p:14   skip:3058 size=72 bytes x264 [debug]: frame=   2 qp=18.66 nal=2 slice:p poc:4   i:0    p:48   skip:3024 size=161 bytes x264 [debug]: frame=   3 qp=18.23 nal=2 slice:p poc:6   i:0    p:84   skip:2988 size=293 bytes 

on linux file file.h264 returns data.

your file format bad. nothing able read that. .264 uses annexb headers prepended idr frames.

change these parameters,

param.b_annexb = 1; param.b_repeat_headers = 1; 

and delete

x264_encoder_headers( encoder, &headers, &i_nal ); int size = headers[0].i_payload + headers[1].i_payload + headers[2].i_payload; fwrite( headers[0].p_payload, 1, size, pfile); 

finally should able take output , convert mp4.

ffmpeg -i file.264 -vcodec copy -an file.mp4 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -