To encode video, you must first define an h264_parameter_t structure to configure the encoder (profile, level, resolution, format, etc.), then create an encoder instance using x264enc_create or openh264enc_create.
Once the encoder is initialized, you feed it raw video frames using the picture_t structure via the x264enc_input (or openh264enc_input) function. Finally, retrieve the encoded packets using x264enc_getpacket (or openh264enc_getpacket) and write the pkt.data to a file.
h264_parameter_t param;
param.profile = H264_PROFILE_BASELINE;
param.level = H264_LEVEL_2_0;
param.width = 352;
param.height = 288;
param.format = PICTURE_YUV420;
param.frame_rate = 25000;
param.gop_size = 50;
param.bitrate = 500000;
// Create the encoder instance
void* h264 = x264enc_create(& param);
// Alternatively for OpenH264:
// void* h264 = openh264enc_create(& param);
// Prepare a raw video frame
picture_t pic;
memset(&pic, 0, sizeof(pic));
pic.format = PICTURE_YUV420;
pic.width = 352;
pic.height = 288;
pic.pts = i * 40;
pic.dts = i * 40;
pic.flags = 0; // Use AVPACKET_FLAG_KEY to force IDR frame
pic.data[0] = s_yuv;
pic.linesize[0] = pic.width;
pic.data[1] = pic.data[0] + pic.linesize[0] * pic.height;
pic.linesize[1] = pic.width / 2;
pic.data[2] = pic.data[1] + pic.linesize[1] * pic.height / 2;
pic.linesize[2] = pic.width / 2;
// Input the frame to the encoder
assert(x264enc_input(h264, &pic) > 0);
// Alternatively for OpenH264:
// assert(openh264enc_input(h264, &pic) > 0);
// Retrieve the encoded packet
avpacket_t pkt;
memset(&pkt, 0, sizeof(pkt));
assert(x264enc_getpacket(h264, &pkt) > 0);
// Alternatively for OpenH264:
// assert(1 == openh264enc_getpacket(h264, &pkt));
// Save the encoded data to a file
assert(pkt.bytes == fwrite(pkt.data, 1, pkt.bytes, wfp));