static void *obs_x264_create(obs_data_t *settings, obs_encoder_t *encoder) { struct obs_x264 *obsx264 = bzalloc(sizeof(struct obs_x264)); obsx264->encoder = encoder; if (update_settings(obsx264, settings)) { obsx264->context = x264_encoder_open(&obsx264->params); if (obsx264->context == NULL) warn("x264 failed to load"); else load_headers(obsx264); } else { warn("bad settings specified"); } if (!obsx264->context) { bfree(obsx264); return NULL; } obsx264->performance_token = os_request_high_performance("x264 encoding"); return obsx264; }
HRESULT x264::init(int width, int height, int bitrate) { this->width = width; this->height = height; x264_param_t param; x264_param_default_preset(¶m, "medium", "zerolatency"); // x264_param_apply_profile(¶m, "baseline"); param.i_frame_reference = 1; param.i_width = width; param.i_height = height; param.i_fps_num = 24000; param.i_fps_den = 1001; param.i_csp = X264_CSP_I420; param.i_keyint_max = 25; //param.b_intra_refresh = 1; param.b_cabac = 1; param.b_annexb = 1; param.rc.i_rc_method = X264_RC_ABR; param.rc.i_bitrate = bitrate; encoder = x264_encoder_open(¶m); // init picture x264_picture_alloc(&pic_in, X264_CSP_I420, width, height); last_encode_time = timeGetTime(); return S_OK; }
HRESULT CX264Encoder::Init( X264ENCPARAM param ) { memset(m_level, (unsigned char)tp_lvl_2, sizeof(m_level)); m_stEncParam = param; x264_param_t st264Param; ConfigParam( &st264Param ); x264_t *h; if ( ( h = x264_encoder_open( &st264Param ) ) == NULL ) { LOG(stderr, "x264 [error]: x264_encoder_open failed\n"); return false; } m_px264Handle = (void*)h; x264_picture_t *pic = (x264_picture_t*)x264_malloc(sizeof(x264_picture_t)); memset( pic, 0, sizeof( x264_picture_t ) ); pic->i_type = X264_TYPE_AUTO; pic->i_qpplus1 = X264_QP_AUTO; pic->i_pic_struct = X264_CSP_I420; pic->img.i_csp = X264_CSP_I420; pic->img.i_plane = 3; pic->img.i_stride[0] = m_stEncParam.iWidth; pic->img.i_stride[1] = pic->img.i_stride[2] = m_stEncParam.iWidth>>1; m_pPic = (void*)pic; return true; }
int H264EncWrapper::Initialize(int iWidth, int iHeight, int iRateBit, int iFps) { m_param.i_width = iWidth; m_param.i_height = iHeight; m_param.i_fps_num = iFps; m_param.i_fps_den = 1; m_param.rc.i_bitrate = iRateBit; m_param.rc.i_rc_method = X264_RC_ABR; m_param.i_frame_reference = 4; /* 参考帧的最大帧数 */ //m_param.i_keyint_max = 8; //m_param.i_keyint_min = 4; /* 根据输入参数param初始化总结构 x264_t *h */ if( ( m_h = x264_encoder_open( &m_param ) ) == NULL ) { fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" ); return -1; } x264_picture_alloc( &m_pic, X264_CSP_I420, m_param.i_width, m_param.i_height ); m_pic.i_type = X264_TYPE_AUTO; m_pic.i_qpplus1 = 0; return 0; }
EncodeContext encode_context_create(int width, int height) { EncodeContext context; context.width = width; context.height = height; x264_param_t param; x264_param_default_preset(¶m, "veryfast", "zerolatency"); param.i_threads = 1; param.i_width = width; param.i_height = height; param.i_fps_num = 20; // the FPS doesn't matter param.i_fps_den = 1; // Intra refres: param.i_keyint_max = 20; // once every twenty frames param.b_intra_refresh = 1; //Rate control: param.rc.i_rc_method = X264_RC_CRF; param.rc.f_rf_constant = 25; param.rc.f_rf_constant_max = 35; //For streaming: param.b_repeat_headers = 1; param.b_annexb = 1; x264_param_apply_profile(¶m, "baseline"); context.encoder = x264_encoder_open(¶m); context.converter = sws_getContext(width, height, PIX_FMT_RGB24, width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL); return context; }
static void enc_preprocess(MSFilter *f){ EncData *d=(EncData*)f->data; #if 0 x264_param_t *params=&d->params; #endif d->packer=rfc3984_new(); rfc3984_set_mode(d->packer,d->mode); rfc3984_enable_stap_a(d->packer,FALSE); d->framenum=0; /* open the encoder */ if(x264_encoder_open(d->enc,d)!=0) { ms_error("MSH264Enc: encoder open error\n"); return ; } /* configure the encoder */ if(x264_encoder_configure(d->enc)!=0) { ms_error("MSH264Enc: encoder configure error\n"); return ; } /* allocate memory for the frame buffers */ if(x264_encoder_allocate_framebuffer(d->enc)!=0) { ms_error("MSH264Enc: encoder allocate framebuffer error\n"); return ; } }
void *vc_open (int width, int height, double fps) { Ctx *ctx = new Ctx; ctx->force_keyframe = 0; // 设置编码属性 //x264_param_default(&ctx->param); x264_param_default_preset(&ctx->param, "fast", "zerolatency"); //x264_param_apply_profile(&ctx->param, "baseline"); ctx->param.i_width = width; ctx->param.i_height = height; ctx->param.b_repeat_headers = 1; // 重复SPS/PPS 放到关键帧前面 ctx->param.b_cabac = 1; ctx->param.i_threads = 1; // ctx->param.b_intra_refresh = 1; ctx->param.i_fps_num = (int)fps; ctx->param.i_fps_den = 1; //ctx->param.b_vfr_input = 1; ctx->param.i_keyint_max = ctx->param.i_fps_num * 2; //ctx->param.i_keyint_min = 1; // rc // ctx->param.rc.i_rc_method = X264_RC_CRF; ctx->param.rc.i_bitrate = 50; //ctx->param.rc.f_rate_tolerance = 0.1; //ctx->param.rc.i_vbv_max_bitrate = ctx->param.rc.i_bitrate * 1.3; //ctx->param.rc.f_rf_constant = 600; //ctx->param.rc.f_rf_constant_max = ctx->param.rc.f_rf_constant * 1.3; #ifdef DEBUG ctx->param.i_log_level = X264_LOG_WARNING; #else ctx->param.i_log_level = X264_LOG_NONE; #endif // release ctx->x264 = x264_encoder_open(&ctx->param); if (!ctx->x264) { fprintf(stderr, "%s: x264_encoder_open err\n", __func__); delete ctx; return 0; } x264_picture_init(&ctx->picture); ctx->picture.img.i_csp = X264_CSP_I420; ctx->picture.img.i_plane = 3; ctx->output = malloc(128*1024); ctx->output_bufsize = 128*1024; ctx->output_datasize = 0; ctx->get_pts = first_pts; ctx->info_valid = 0; return ctx; }
void H264Encoder::init_(const int wid, const int hei) { // 0. building encoder parameters. x264_param_default_preset(&x264_opt_, "ultrafast", "zerolatency"); x264_opt_.i_width = wid; x264_opt_.i_height = hei; x264_opt_.i_threads = 1; x264_opt_.b_repeat_headers = 1; x264_opt_.b_intra_refresh = 1; x264_opt_.rc.i_rc_method = X264_RC_CQP; x264_opt_.rc.i_qp_constant = 24; x264_opt_.rc.i_qp_min = 24; x264_opt_.rc.i_qp_max = 24; //x264_param_default(&opt); x264_param_apply_profile(&x264_opt_, "baseline"); // 1. Prepare the output buffer and target file x264_picture_alloc(&x264_picin_, X264_CSP_NV12, x264_opt_.i_width, x264_opt_.i_height); x264_picture_alloc(&x264_picout_, X264_CSP_NV12, x264_opt_.i_width, x264_opt_.i_height); // 2. Building the encoder handler x264_hdl_ = x264_encoder_open(&x264_opt_); x264_encoder_parameters(x264_hdl_, &x264_opt_); }
/* Begin encode * file located at: * * src/com/livecamera/encoder/h264encoder.java */ jlong Java_com_livecamera_encoder_h264encoder_CompressBegin( JNIEnv* env, jobject thiz, jint width, jint height) { Encoder *en = (Encoder*) malloc(sizeof(Encoder)); en->param = (x264_param_t*) malloc(sizeof(x264_param_t)); en->picture = (x264_picture_t*) malloc(sizeof(x264_picture_t)); x264_param_default(en->param); //set default param en->param->i_log_level = X264_LOG_NONE; en->param->i_width = width; en->param->i_height = height; en->param->rc.i_lookahead = 0; en->param->i_bframe = 0; en->param->i_fps_num = 5; en->param->i_fps_den = 1; if ((en->handle = x264_encoder_open(en->param)) == 0) { return 0; } //create a new pic x264_picture_alloc(en->picture, X264_CSP_I420, en->param->i_width, en->param->i_height); return (jlong)en; }
void do_init_encoder(struct x264lib_ctx *ctx, int width, int height, int initial_quality, int supports_csc_option) { ctx->quality = initial_quality; ctx->supports_csc_option = supports_csc_option; ctx->colour_sampling = get_x264_colour_sampling(ctx, initial_quality); ctx->x264_quality = get_x264_quality(initial_quality); ctx->csc_format = get_csc_format_for_x264_format(ctx->colour_sampling); ctx->encoding_preset = 2; ctx->preset = x264_preset_names[ctx->encoding_preset]; ctx->profile = get_profile_for_quality(initial_quality); ctx->csc_algo = get_csc_algo_for_quality(initial_quality); //printf("do_init_encoder(%p, %i, %i, %i, %i) colour_sampling=%i, x264_quality=%f, profile=%s\n", ctx, width, height, initial_quality, supports_csc_option, ctx->colour_sampling, ctx->x264_quality, ctx->profile); x264_param_t param; x264_param_default_preset(¶m, ctx->preset, "zerolatency"); param.i_threads = 1; param.i_width = width; param.i_height = height; param.i_csp = ctx->colour_sampling; param.rc.f_rf_constant = ctx->x264_quality; param.i_log_level = 0; x264_param_apply_profile(¶m, ctx->profile); ctx->encoder = x264_encoder_open(¶m); ctx->width = width; ctx->height = height; ctx->rgb2yuv = init_encoder_csc(ctx); }
int H264Encoder::Prepare(const MediaDescription& desc) { if ( desc.isVideo == false) return -1; int wid = desc.width; int hei = desc.height; // 0. building a default encoder parameters. x264_param_default_preset(&x264_opt_, "ultrafast", "zerolatency"); x264_opt_.rc.i_rc_method = X264_RC_CRF; x264_opt_.rc.i_bitrate = 512; x264_opt_.i_nal_hrd = X264_NAL_HRD_CBR; //x264_param_default(&opt); // 1. Setting the fields of parameter struct x264_opt_.i_width = wid; x264_opt_.i_height = hei; //opt.i_slice_count = 5; //opt.b_intra_refresh = 1; // 3. Prepare the output buffer and target file x264_picture_alloc(&x264_picin_[0], X264_CSP_NV12, x264_opt_.i_width, x264_opt_.i_height); x264_picture_alloc(&x264_picin_[1], X264_CSP_NV12, x264_opt_.i_width, x264_opt_.i_height); x264_picture_alloc(&x264_picout_, X264_CSP_NV12, x264_opt_.i_width, x264_opt_.i_height); ppIndex = -1; // 4. Building the encoder handler x264_hdl_ = x264_encoder_open(&x264_opt_); x264_encoder_parameters(x264_hdl_, &x264_opt_); return 0; }
EncoderVideoSource::EncoderVideoSource(UsageEnvironment& env, unsigned int width, unsigned int height, unsigned int framerate, unsigned int bitrate, unsigned int keyinterval, VideoCodec codec, char *type): FramedSource(env), fp(NULL), fWidth(width), fHeight(height), fFramerate(framerate), fBitrate(bitrate), fKeyInterval(keyinterval), fCodec(codec), fConfigBytes(NULL), fStartTime(0), fEncoderHandle(NULL) { memset(mediaType, 0, sizeof(mediaType)); memcpy(mediaType, type, strlen(type)); #ifdef STARV_TEST if(strcmp(type, "live") == 0 || strcmp(type, "livehd") == 0) fp = fopen(VIDEO_FILE, "rb"); if(strcmp(type, "mobile") == 0) fp = fopen(VIDEO_FILE, "rb"); #endif Debug(ckite_log_message, "type = %s\n", type); if(strcmp(type, "store") == 0) { Debug(ckite_log_message, "EncoderVideoSource store \n"); } fBuffer = new unsigned char[fWidth*fHeight*3/2]; #ifdef SDKH264 #ifdef ENC_SOURCE x264_param_default(&m_param); x264_param_apply_profile(&m_param, "baseline"); m_param.i_width = fWidth; m_param.i_height = fHeight; m_param.i_fps_num = 10; // m_param.i_fps_den = 1000; m_param.i_frame_reference = 1; //m_param.i_maxframes = 0; // no find this parameter m_param.i_keyint_max = 250; m_param.i_bframe = 0; m_param.rc.i_bitrate = 1000; // the unit is kbps //m_param.rc.b_cbr = 0; m_param.rc.f_qcompress = 0; //m_param.rc.b_stat_write = 0; //m_param.analyse.inter = 0; m_param.analyse.b_psnr = 0; m_param.b_cabac = 0; m_param.rc.b_mb_tree = 0; //m_param.pf_log = NULL //if set NULL , it will segment fault x264_handle = x264_encoder_open(&m_param); //memset(p_nal, 0x0, sizeof (struct nal)); fprintf(stderr, "x264_encoder_open x264_handle:%x\n", x264_handle); #endif for(int i = 0; i < 4; i++) { more_nal[i] = NULL; more_nal_len[i] = 0; } #endif #ifdef SDKMPEG4 #ifdef ENC_SOURCE Debug(ckite_log_message, "xvid fWidth = %d, fHeight = %d\n", fWidth, fHeight); fEncoderHandle = xvid_enc_init(fWidth, fHeight, fBitrate, fFramerate, fKeyInterval, 1); #endif #endif }
static int config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt) { h264_module_t *mod=(h264_module_t*)vf->priv; if(parse_error) return 0; mod->mux->bih->biWidth = width; mod->mux->bih->biHeight = height; mod->mux->bih->biSizeImage = width * height * 3; mod->mux->aspect = (float)d_width/d_height; // make sure param is initialized x264enc_set_param(NULL, ""); param.i_width = width; param.i_height = height; param.i_fps_num = mod->mux->h.dwRate; param.i_fps_den = mod->mux->h.dwScale; param.b_vfr_input = 0; param.vui.i_sar_width = d_width*height; param.vui.i_sar_height = d_height*width; x264_param_parse(¶m, "stats", passtmpfile); switch(outfmt) { case IMGFMT_I420: param.i_csp = X264_CSP_I420; break; case IMGFMT_YV12: param.i_csp = X264_CSP_YV12; break; default: mp_msg(MSGT_MENCODER, MSGL_ERR, "Wrong colorspace.\n"); return 0; } mod->x264 = x264_encoder_open(¶m); if(!mod->x264) { mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_open failed.\n"); return 0; } if(!param.b_repeat_headers){ x264_nal_t *nal; int extradata_size, nnal; extradata_size = x264_encoder_headers(mod->x264, &nal, &nnal); mod->mux->bih= realloc(mod->mux->bih, sizeof(*mod->mux->bih) + extradata_size); memcpy(mod->mux->bih + 1, nal->p_payload, extradata_size); mod->mux->bih->biSize= sizeof(*mod->mux->bih) + extradata_size; } if (param.i_bframe > 1 && param.i_bframe_pyramid) mod->mux->decoder_delay = 2; else mod->mux->decoder_delay = param.i_bframe ? 1 : 0; return 1; }
void H264Encoder::Init(x264_param_t * param) { m_encoder = x264_encoder_open(param); m_width = param->i_width; m_height = param->i_height; m_stride = m_width*4; x264_picture_alloc(&m_pic_in, X264_CSP_I420, m_width, m_height); m_convert_ctx = sws_getContext(m_width, m_height, PIX_FMT_RGBA, m_width, m_height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL); }
VideoEncoder* video_encoder_init(int width, int height, int fpsNum, int fpsDen, int maxWidth){ printf("[arcade encoder init] %d x %d @ (%d / %d)\n",width,height,fpsNum,fpsDen); VideoEncoder *enc = (VideoEncoder*) malloc(sizeof(VideoEncoder)); memset(enc,0,sizeof(VideoEncoder)); //TODO: this needs to become a parameter enc->in_fmt = AV_PIX_FMT_BGRA; enc->width = width; enc->height = height; enc->max_width = maxWidth; printf("[arcade encoder] capping width at %dpx\n",enc->max_width); float aspectRatio = (float) enc->width / (float) enc->height; enc->out_width = enc->width > enc->max_width ? enc->max_width : enc->width; enc->out_height = (int) ((float) enc->out_width / aspectRatio); printf("[arcade encoder init]\n\t%d x %d --> %d x %d\n\t@ (%d / %d)\n", enc->width,enc->height, enc->out_width,enc->out_height, fpsNum,fpsDen); x264_param_t param; x264_param_default_preset(¶m, "ultrafast", "zerolatency"); param.i_threads = 1; param.i_width = enc->out_width; param.i_height = enc->out_height; param.i_fps_num = fpsNum; param.i_fps_den = fpsDen; // Intra refres: param.i_keyint_max = 30000; param.b_intra_refresh = 1; //Rate control: param.rc.i_rc_method = X264_RC_CRF; param.rc.f_rf_constant = 25; param.rc.f_rf_constant_max = 35; //For streaming: param.b_repeat_headers = 1; param.b_annexb = 1; x264_param_apply_profile(¶m, "baseline"); enc->encoder = x264_encoder_open(¶m); x264_picture_alloc(&enc->pic_in, X264_CSP_I420, enc->out_width, enc->out_height); enc->output_buffer_size = (int)sizeof(uint8_t)*4096*1024; enc->output_buffer = malloc(enc->output_buffer_size); enc->sws = sws_getContext(enc->width, enc->height, enc->in_fmt, enc->out_width, enc->out_height, AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL); enc->num_frames = 0; return enc; }
static void enc_preprocess(MSFilter *f){ EncData *d=(EncData*)f->data; x264_param_t params; float bitrate; d->packer=rfc3984_new(); rfc3984_set_mode(d->packer,d->mode); rfc3984_enable_stap_a(d->packer,FALSE); x264_param_default(¶ms); params.i_threads=0; params.i_sync_lookahead=0; params.i_width=d->vsize.width; params.i_height=d->vsize.height; params.i_fps_num=(int)d->fps; params.i_fps_den=1; params.i_slice_max_size=ms_get_payload_max_size()-100; /*-100 security margin*/ params.i_level_idc=13; bitrate=(float)d->bitrate*0.92; if (bitrate>RC_MARGIN) bitrate-=RC_MARGIN; params.rc.i_rc_method = X264_RC_ABR; params.rc.i_bitrate=(int)(bitrate/1000); params.rc.f_rate_tolerance=0.1; params.rc.i_vbv_max_bitrate=(int) ((bitrate+RC_MARGIN/2)/1000); params.rc.i_vbv_buffer_size=params.rc.i_vbv_max_bitrate; params.rc.f_vbv_buffer_init=0.5; params.rc.i_lookahead=0; /*enable this by config ?*/ /* params.i_keyint_max = (int)d->fps*d->keyframe_int; params.i_keyint_min = (int)d->fps; */ params.b_repeat_headers=1; params.b_annexb=0; //these parameters must be set so that our stream is baseline params.analyse.b_transform_8x8 = 0; params.b_cabac = 0; params.i_cqm_preset = X264_CQM_FLAT; params.i_bframe = 0; params.analyse.i_weighted_pred = X264_WEIGHTP_NONE; x264_param_apply_preset(¶ms,"faster");//将编码设置成superfast模式【相比其他模式,会有一些花屏】 x264_param_apply_tune(¶ms,"zerolatency");//将延时设置成最短 d->enc=x264_encoder_open(¶ms); if (d->enc==NULL) ms_error("Fail to create x264 encoder."); d->framenum=0; video_starter_init(&d->starter); }
int CX264Encoder::open() { int ret = -1; if (!h ){ if (h = x264_encoder_open(¶m)){ return ret; } } else{ return 0; } return ret; }
void rtspStream::initH264Encoder(int width,int height,int fps,int bitRate) { frame_num = 0; pX264Handle = NULL; pX264Param = new x264_param_t; assert(pX264Param); m_nFPS = 25; //* 配置参数 //* 使用默认参数,在这里因为我的是实时网络传输,所以我使用了zerolatency的选项,使用这个选项之后就不会有delayed_frames,如果你使用的不是这样的话,还需要在编码完成之后得到缓存的编码帧 x264_param_default_preset(pX264Param, "veryfast", "zerolatency"); //* cpuFlags pX264Param->i_threads = X264_SYNC_LOOKAHEAD_AUTO;//* 取空缓冲区继续使用不死锁的保证. //* 视频选项 pX264Param->i_width = width; //* 要编码的图像宽度. pX264Param->i_height = height; //* 要编码的图像高度 pX264Param->i_frame_total = 0; //* 编码总帧数.不知道用0. pX264Param->i_keyint_max = 10; //* 流参数 pX264Param->i_bframe = 5; pX264Param->b_open_gop = 0; pX264Param->i_bframe_pyramid = 0; pX264Param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS; //* Log参数,不需要打印编码信息时直接注释掉就行 // pX264Param->i_log_level = X264_LOG_DEBUG; //* 速率控制参数 pX264Param->rc.i_bitrate = bitRate;//* 码率(比特率,单位Kbps) //* muxing parameters pX264Param->i_fps_den = 1; //* 帧率分母 pX264Param->i_fps_num = fps;//* 帧率分子 pX264Param->i_timebase_den = pX264Param->i_fps_num; pX264Param->i_timebase_num = pX264Param->i_fps_den; //* 设置Profile.使用Baseline profile x264_param_apply_profile(pX264Param, x264_profile_names[0]); pNals = NULL; pPicIn = new x264_picture_t; pPicOut = new x264_picture_t; x264_picture_init(pPicOut); x264_picture_alloc(pPicIn, X264_CSP_I420, pX264Param->i_width, pX264Param->i_height); pPicIn->img.i_csp = X264_CSP_I420; pPicIn->img.i_plane = 3; //* 打开编码器句柄,通过x264_encoder_parameters得到设置给X264 //* 的参数.通过x264_encoder_reconfig更新X264的参数 pX264Handle = x264_encoder_open(pX264Param); assert(pX264Handle); pPicIn->img.plane[0] = PYUVBuf; pPicIn->img.plane[1] = PYUVBuf + width *height; pPicIn->img.plane[2] = PYUVBuf + width * height * 5 / 4; pPicIn->img.plane[3] = 0; }
bool InitEncoder() { x264 = x264_encoder_open(¶mData); if (!x264) { return false; } Log::writeMessage(LOG_RTSPSERV, 1, "LiveSDK_Log:------------------------------------------"); Log::writeMessage(LOG_RTSPSERV, 1, "LiveSDK_Log:%s", WcharToAnsi(GetInfoString().Array()).c_str()); Log::writeMessage(LOG_RTSPSERV, 1, "LiveSDK_Log:------------------------------------------"); DataPacket packet; GetHeaders(packet); return true; }
static void enc_preprocess(MSFilter *f){ EncData *d=(EncData*)f->data; x264_param_t *params=&d->params; d->packer=rfc3984_new(); rfc3984_set_mode(d->packer,d->mode); rfc3984_enable_stap_a(d->packer,FALSE); #if defined(__arm__) || defined(ANDROID) if (x264_param_default_preset(params,"superfast"/*"ultrafast"*/,"zerolatency")) { ms_error("Cannot apply default x264 configuration"); } #else x264_param_default(params); #endif params->i_threads=ms_get_cpu_count(); params->i_sync_lookahead=0; params->i_width=d->vconf.vsize.width; params->i_height=d->vconf.vsize.height; params->i_fps_num=(int)d->vconf.fps; params->i_fps_den=1; params->i_slice_max_size=ms_get_payload_max_size()-100; /*-100 security margin*/ params->i_level_idc=13; apply_bitrate(f); params->rc.i_lookahead=0; /*enable this by config ?*/ /* params.i_keyint_max = (int)d->fps*d->keyframe_int; params.i_keyint_min = (int)d->fps; */ params->b_repeat_headers=1; params->b_annexb=0; //these parameters must be set so that our stream is baseline params->analyse.b_transform_8x8 = 0; params->b_cabac = 0; params->i_cqm_preset = X264_CQM_FLAT; params->i_bframe = 0; params->analyse.i_weighted_pred = X264_WEIGHTP_NONE; d->enc=x264_encoder_open(params); if (d->enc==NULL) ms_error("Fail to create x264 encoder."); d->framenum=0; video_starter_init(&d->starter); }
void x264Encoder::Initilize() { x264_param_default_preset(¶meters, "veryfast", "zerolatency"); parameters.i_log_level = X264_LOG_INFO; parameters.i_threads = 1; parameters.i_width = 512 / 2; parameters.i_height = 424 / 2; parameters.i_fps_num = _fps; parameters.i_fps_den = 1; parameters.i_keyint_max = 15; parameters.b_intra_refresh = 1; parameters.rc.i_rc_method = X264_RC_CRF; parameters.rc.i_vbv_buffer_size = 1000000; parameters.rc.i_vbv_max_bitrate = 90000; parameters.rc.f_rf_constant = 25; parameters.rc.f_rf_constant_max = 35; parameters.i_sps_id = 7; // the following two value you should keep 1 parameters.b_repeat_headers = 1; // to get header before every I-Frame parameters.b_annexb = 1; // put start code in front of nal. we will remove start code later x264_param_apply_profile(¶meters, "baseline"); encoder = x264_encoder_open(¶meters); x264_picture_alloc(&picture_in, X264_CSP_I420, 512 / 2, 424 / 2); picture_in.i_type = X264_TYPE_AUTO; picture_in.img.i_csp = X264_CSP_I420; x264_picture_alloc(&picture_out, X264_CSP_I420, parameters.i_width, parameters.i_height); picture_out.i_type = X264_TYPE_AUTO; picture_out.img.i_csp = X264_CSP_I420; // 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( 512 / 2, 424 / 2, PIX_FMT_BGR24, parameters.i_width, parameters.i_height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL); }
struct x264lib_ctx *init_encoder(int width, int height) { struct x264lib_ctx *ctx = malloc(sizeof(struct x264lib_ctx)); ctx->encoding_preset = 2; x264_param_t param; x264_param_default_preset(¶m, x264_preset_names[ctx->encoding_preset], "zerolatency"); param.i_threads = 1; param.i_width = width; param.i_height = height; param.i_csp = X264_CSP_I420; param.i_log_level = 0; x264_param_apply_profile(¶m, "baseline"); ctx->encoder = x264_encoder_open(¶m); ctx->width = width; ctx->height = height; ctx->rgb2yuv = sws_getContext(ctx->width, ctx->height, PIX_FMT_RGB24, ctx->width, ctx->height, PIX_FMT_YUV420P, SWS_SINC | SWS_ACCURATE_RND, NULL, NULL, NULL); return ctx; }
bool init_x264_encoder(Encoder * enc,int width,int height) { enc->param = (x264_param_t*)malloc(sizeof(x264_param_t)); enc->picture = (x264_picture_t*)malloc(sizeof(x264_picture_t)); enc->picture->i_pts = 0 ; // set default param // todo improvements later x264_param_default(enc->param); // set width and height enc->param->i_width = width; enc->param->i_height = height ; enc->param->rc.i_lookahead = 0; // set fps enc->param->i_fps_num = 10 ; enc->param->i_fps_den = 1 ; // set baseline x264_param_apply_profile(enc->param, x264_profile_names[0]); // open encoder if( (enc->handle = x264_encoder_open(enc->param)) == 0 ) { SKY_LOG(1,(TAG_H264ENCODER,"Could not Open x264_encoder")); // will free when encoder close or now //free(enc->param); //free(enc->picture); return false ; } // create a new picture malloc enc->picture here X264_CSP_I422 X264_CSP_YV16 X264_CSP_NV16 //x264_picture_alloc(enc->picture,X264_CSP_YV12,enc->param->i_width,enc->param->i_height); //enc->picture->img.i_csp = X264_CSP_YV12 ; //x264_picture_alloc(enc->picture,X264_CSP_NV12,enc->param->i_width,enc->param->i_height); //enc->picture->img.i_csp = X264_CSP_NV12 ; x264_picture_alloc(enc->picture,X264_CSP_YV12,enc->param->i_width,enc->param->i_height); enc->picture->img.i_csp = X264_CSP_YV12 ; enc->picture->img.i_plane = 3 ; return true ; }
void encode_init() { // fill x264_param_t with default values and do CPU detection x264_param_default(x264_encode.para); if(x264_param_default_preset(x264_encode.para, capg.preset, capg.tune) < 0) CAP_DBG_EXIT("x264_param_default_preset error!\n"); // ricann debug CAP_DBG("-------------------after x264_param_default_preset\n"); encode_printpara(); // Configure non-default params // real frame rate is i_fps_num/i_fps_den x264_encode.para->i_fps_num = capg.frame_rate; x264_encode.para->i_width = capg.width; x264_encode.para->i_height = capg.height; x264_encode.para->i_keyint_max = capg.gop_size; x264_encode.para->i_csp = x264_encode.colorspace; x264_encode.para->b_vfr_input = 0; x264_encode.para->b_repeat_headers = 1; // ricann debug CAP_DBG("-------------------after assign value\n"); encode_printpara(); x264_encode.pic->img.i_csp = x264_encode.colorspace; x264_encode.pic->img.i_plane = 3; x264_encode.pic->i_type = X264_TYPE_AUTO; if(x264_param_apply_profile(x264_encode.para, capg.profile) < 0) CAP_DBG_EXIT("x264_param_apply_profile error!\n"); x264_encode.handle = x264_encoder_open(x264_encode.para); if(!x264_encode.handle) CAP_DBG_EXIT("x264_encoder_open error!\n"); // ricann debug CAP_DBG("-------------------after x264_encoder_open\n"); encode_printpara(); }
void encode_init(Encoder *encoder, int img_width, int img_height) { //Set default x264 parameters encoder->param = (x264_param_t *) malloc(sizeof(x264_param_t)); encoder->picture = (x264_picture_t *) malloc(sizeof(x264_picture_t)); x264_param_default(encoder->param); encoder->param->i_width = img_width; //set frame width encoder->param->i_height = img_height; //set frame height encoder->param->rc.i_lookahead = 0; //表示i帧向前缓冲区 encoder->param->i_fps_num = 25; //帧率分子 encoder->param->i_fps_den = 1; //帧率分母 encoder->param->rc.i_lookahead = 0; encoder->param->i_sync_lookahead = 0; encoder->param->i_bframe = 0; encoder->param->b_sliced_threads = 1; encoder->param->b_vfr_input = 0; encoder->param->rc.b_mb_tree = 0; x264_param_apply_profile(encoder->param, x264_profile_names[0]); encoder->handle = x264_encoder_open(encoder->param); if (encoder->handle == 0) { return; } /* Create a new pic */ //encoder->picture->param->i_width = img_width; //encoder->picture->param->i_height = img_height; x264_picture_alloc(encoder->picture, X264_CSP_I420, encoder->param->i_width,encoder->param->i_height); encoder->picture->img.i_csp = X264_CSP_I420; encoder->picture->img.i_plane = 3; g_H264_Buf = (uint8_t *) malloc( sizeof(uint8_t) * g_ImgWidth * g_ImgHeight * 3); // 设置缓冲区 }
void init_encoder(void) { x264_param_default_preset(¶m, "veryfast", "zerolatency"); param.i_threads=1; param.i_width=x264_width; param.i_height=x264_height; param.i_fps_num=fps; param.i_fps_den=1; param.i_keyint_max=25; param.b_intra_refresh=1; //If param->b_annexb is set, Annex-B bytestream with startcode. //* This size is the size used in mp4/similar muxing; it is equal to ( i_payload-4 ) --> uint8_t *p_payload; param.b_annexb=1; x264_param_apply_profile(¶m,"baseline"); encoder=x264_encoder_open(¶m); printf("in init_encoder function\n"); }
_declspec(dllexport) int __cdecl AllocEncoder(struct TranscoderContext* ctx) { struct TranscoderOptions* options = ctx->options; if (x264_picture_alloc(&pic_in, X264_CSP_I420, options->OutputWidth, options->OutputHeight) < 0) { return 1; } fprintf(stdout, "Width: %d\n", options->InputWidth); fprintf(stdout, "Height: %d\n", options->InputHeight); NALBYTES = malloc(options->OutputWidth * options->OutputHeight * 4); x264_param_t param; x264_param_default_preset(¶m, "ultrafast", "zerolatency"); param.i_threads = 0; param.i_width = options->OutputWidth; param.i_height = options->OutputHeight; param.rc.i_rc_method = X264_RC_CRF; //param.rc.i_bitrate = 3000; //param.rc.i_vbv_max_bitrate = 4000; param.rc.f_rf_constant = 20; fprintf(stdout, "rf_constant: %f\n", param.rc.f_rf_constant); fprintf(stdout, "rf_constant_max: %f\n", param.rc.f_rf_constant_max); //fprintf(stdout, "rf_constant_max: %f\n", param.rc.rf); //param.i_keyint_max = 25; param.i_keyint_max = 500; //param.b_intra_refresh = 1; param.b_repeat_headers = 1; param.b_annexb = 1; //param.i_log_level = -1; x264_t* encoder = x264_encoder_open(¶m); ctx->encoder = encoder; convertCtx = sws_getContext(options->InputWidth, options->InputHeight, AV_PIX_FMT_RGBA, options->OutputWidth, options->OutputHeight, AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL); return 0; }
H264Exporter::H264Exporter(const exporter_settings& settings) : _success{false} , _settings(settings) , _file{settings.path, std::ios::binary} , _frame{0} , _encoder{nullptr} { x264_param_t param; // Best quality (0) -> "veryslow" (8); worst quality (4) -> "ultrafast" (0). auto quality = std::to_string(2 * (4 - settings.quality)); if (x264_param_default_preset(¶m, quality.c_str(), "film") < 0) { std::cerr << "couldn't get default preset" << std::endl; return; } param.i_threads = settings.threads > 1 ? settings.threads - 1 : 1; param.i_lookahead_threads = settings.threads > 1 ? 1 : 0; param.i_width = settings.width; param.i_height = settings.height; param.i_fps_num = settings.fps; param.i_fps_den = 1; param.i_frame_total = settings.fps * settings.length; param.i_keyint_min = 0; param.i_keyint_max = settings.fps; if (x264_param_apply_profile(¶m, "high") < 0) { std::cerr << "couldn't get apply profile" << std::endl; return; } _encoder = x264_encoder_open(¶m); if (!_encoder) { std::cerr << "couldn't create encoder" << std::endl; return; } if (x264_picture_alloc(&_pic, X264_CSP_I420, _settings.width, _settings.height) < 0) { std::cerr << "couldn't allocate picture" << std::endl; return; } _success = true; }
jlong Java_h264_com_H264Encoder_CompressBegin(JNIEnv* env, jobject thiz, jint width, jint height) { Encoder * en = (Encoder *) malloc(sizeof(Encoder)); en->param = (x264_param_t *) malloc(sizeof(x264_param_t)); en->picture = (x264_param_t *) malloc(sizeof(x264_picture_t)); x264_param_default(en->param); //set default param x264_param_apply_profile(en->param,"baseline"); //en->param->rc.i_rc_method = X264_RC_CQP; en->param->i_log_level = X264_LOG_NONE; en->param->i_width = width; //set frame width en->param->i_height = height; //set frame height en->param->rc.i_lookahead =0; en->param->i_fps_num =5; en->param->i_fps_den = 1; if ((en->handle = x264_encoder_open(en->param)) == 0) { return 0; } /* Create a new pic */ x264_picture_alloc(en->picture, X264_CSP_I420, en->param->i_width, en->param->i_height); return (jlong) en; }
x264_t* msx264::msx264_encoder_open() { int iResult; pX264Handle = x264_encoder_open(¶ms); assert(pX264Handle); iResult = x264_encoder_headers(pX264Handle, &pNals, &iNal); assert(iResult >= 0); //* 获取整个流的PPS和SPS,不需要可以不调用. iResult = x264_encoder_headers(pX264Handle, &pNals, &iNal); assert(iResult >= 0); //* PPS SPS 总共只有36B,如何解析出来呢? for (int i = 0; i < iNal; ++i) { switch (pNals[i].i_type) { case NAL_SPS: break; case NAL_PPS: break; default: break; } } }