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 ; } }
static void enc_preprocess(MSFilter* f) { EncData *d=(EncData*)f->data; d->packer=rfc3984_new(); rfc3984_set_mode(d->packer,d->mode); rfc3984_enable_stap_a(d->packer,FALSE); ms_video_starter_init(&d->starter); AMediaCodec *codec; codec = AMediaCodec_createEncoderByType("video/avc"); d->codec = codec; AMediaFormat *format = AMediaFormat_new(); AMediaFormat_setString(format, "mime", "video/avc"); AMediaFormat_setInt32(format, "width", d->vconf.vsize.width); AMediaFormat_setInt32(format, "height", d->vconf.vsize.height); AMediaFormat_setInt32(format, "i-frame-interval", 20); AMediaFormat_setInt32(format, "color-format", 19); AMediaFormat_setInt32(format, "bitrate", d->vconf.required_bitrate); AMediaFormat_setInt32(format, "frame-rate", d->vconf.fps); AMediaFormat_setInt32(format, "bitrate-mode",1); media_status_t status = AMediaCodec_configure(d->codec, format, NULL, NULL, AMEDIACODEC_CONFIGURE_FLAG_ENCODE); if(status != 0){ d->isYUV = FALSE; AMediaFormat_setInt32(format, "color-format", 21); AMediaCodec_configure(d->codec, format, NULL, NULL, AMEDIACODEC_CONFIGURE_FLAG_ENCODE); } AMediaCodec_start(d->codec); AMediaFormat_delete(format); }
void MSOpenH264Encoder::initialize() { mFrameCount = 0; mPacker = rfc3984_new(); rfc3984_set_mode(mPacker, mPacketisationMode); rfc3984_enable_stap_a(mPacker, FALSE); if (mEncoder != 0) { SEncParamExt params; int ret = mEncoder->GetDefaultParams(¶ms); if (ret != 0) { ms_error("OpenH264 encoder: Failed getting default params: %d", ret); } else { int targetBitrate, maxBitrate; calcBitrates(targetBitrate, maxBitrate); params.iUsageType = CAMERA_VIDEO_REAL_TIME; params.iInputCsp = videoFormatI420; params.iPicWidth = mVConf.vsize.width; params.iPicHeight = mVConf.vsize.height; params.iTargetBitrate = targetBitrate; params.iMaxBitrate = maxBitrate; params.iRCMode = RC_LOW_BW_MODE; params.fMaxFrameRate = mVConf.fps; params.uiIntraPeriod=mVConf.fps*10; params.bEnableSpsPpsIdAddition=0; //params.bEnableRc = true; params.bEnableFrameSkip = true; params.bPrefixNalAddingCtrl = false; params.uiMaxNalSize = ms_get_payload_max_size(); params.iMultipleThreadIdc = ms_get_cpu_count(); params.bEnableDenoise = false; params.bEnableBackgroundDetection = true; params.bEnableAdaptiveQuant = true; params.bEnableSceneChangeDetect = false; params.bEnableLongTermReference = false; params.iSpatialLayerNum=1; params.sSpatialLayers[0].iVideoWidth = mVConf.vsize.width; params.sSpatialLayers[0].iVideoHeight = mVConf.vsize.height; params.sSpatialLayers[0].fFrameRate = mVConf.fps; params.sSpatialLayers[0].iSpatialBitrate = targetBitrate; params.sSpatialLayers[0].iMaxSpatialBitrate = maxBitrate; params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_DYN_SLICE; params.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceSizeConstraint = ms_get_payload_max_size(); ret = mEncoder->InitializeExt(¶ms); if (ret != 0) { ms_error("OpenH264 encoder: Failed to initialize: %d", ret); } else { ret = mEncoder->SetOption(ENCODER_OPTION_TRACE_LEVEL, &debugLevel); if (ret != 0) { ms_error("OpenH264 encoder: Failed setting trace level: %d", ret); } mInitialized = true; } } } }
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); }
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); }
static void h264_enc_configure(VTH264EncCtx *ctx) { OSStatus err; const char *error_msg = "Could not initialize the VideoToolbox compresson session"; int max_payload_size = ms_factory_get_payload_max_size(ctx->f->factory); CFNumberRef value; CFMutableDictionaryRef pixbuf_attr = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); int pixel_type = kCVPixelFormatType_420YpCbCr8Planar; value = CFNumberCreate(NULL, kCFNumberIntType, &pixel_type); CFDictionarySetValue(pixbuf_attr, kCVPixelBufferPixelFormatTypeKey, value); err =VTCompressionSessionCreate(NULL, ctx->conf.vsize.width, ctx->conf.vsize.height, kCMVideoCodecType_H264, NULL, pixbuf_attr, NULL, (VTCompressionOutputCallback)h264_enc_output_cb, ctx, &ctx->session); CFRelease(pixbuf_attr); if(err) { ms_error("%s: error code %d", error_msg, err); goto fail; } VTSessionSetProperty(ctx->session, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_Baseline_AutoLevel); VTSessionSetProperty(ctx->session, kVTCompressionPropertyKey_AllowFrameReordering, kCFBooleanFalse); value = CFNumberCreate(NULL, kCFNumberIntType, &ctx->conf.required_bitrate); VTSessionSetProperty(ctx->session, kVTCompressionPropertyKey_AverageBitRate, value); value = CFNumberCreate(NULL, kCFNumberFloatType, &ctx->conf.fps); VTSessionSetProperty(ctx->session, kVTCompressionPropertyKey_ExpectedFrameRate, value); if((err = VTCompressionSessionPrepareToEncodeFrames(ctx->session)) != 0) { ms_error("Could not prepare the VideoToolbox compression session: error code %d", err); goto fail; } rfc3984_init(&ctx->packer_ctx); rfc3984_set_mode(&ctx->packer_ctx, 1); ctx->packer_ctx.maxsz = max_payload_size; ctx->is_configured = TRUE; return; fail: if(ctx->session) CFRelease(ctx->session); }
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")) { #else x264_param_default(params); { #endif ms_error("Cannot apply default x264 configuration"); }; 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); } static void x264_nals_to_msgb(x264_nal_t *xnals, int num_nals, MSQueue * nalus){ int i; mblk_t *m; /*int bytes;*/ for (i=0;i<num_nals;++i){ m=allocb(xnals[i].i_payload+10,0); memcpy(m->b_wptr,xnals[i].p_payload+4,xnals[i].i_payload-4); m->b_wptr+=xnals[i].i_payload-4; if (xnals[i].i_type==7) { ms_message("A SPS is being sent."); }else if (xnals[i].i_type==8) { ms_message("A PPS is being sent."); } ms_queue_put(nalus,m); } }