Commit 82d705e2 authored by Andrey Turkin's avatar Andrey Turkin Committed by Timo Rothenpieler

avcodec/nvenc: split large functions into smaller ones

Functions names and scopes are from libav. This commit basically moves
code around without changing it; it shouldn't change any functionality
except some small leak fixes on error paths.
Signed-off-by: 's avatarTimo Rothenpieler <timo@rothenpieler.org>
parent 9591ca70
......@@ -541,44 +541,17 @@ static av_cold void nvenc_unload_nvenc(AVCodecContext *avctx)
av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
}
static av_cold int nvenc_encode_init(AVCodecContext *avctx)
static av_cold int nvenc_setup_device(AVCodecContext *avctx)
{
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encode_session_params = { 0 };
NV_ENC_PRESET_CONFIG preset_config = { 0 };
CUcontext cu_context_curr;
CUresult cu_res;
GUID encoder_preset = NV_ENC_PRESET_HQ_GUID;
GUID codec;
NVENCSTATUS nv_status = NV_ENC_SUCCESS;
AVCPBProperties *cpb_props;
int surfaceCount = 0;
int i, num_mbs;
int isLL = 0;
int lossless = 0;
int res = 0;
int dw, dh;
int qp_inter_p;
NvencContext *ctx = avctx->priv_data;
NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
if (!nvenc_dyload_nvenc(avctx))
return AVERROR_EXTERNAL;
ctx->last_dts = AV_NOPTS_VALUE;
ctx->encode_config.version = NV_ENC_CONFIG_VER;
ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
preset_config.version = NV_ENC_PRESET_CONFIG_VER;
preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
encode_session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
encode_session_params.apiVersion = NVENCAPI_VERSION;
CUresult cu_res;
CUcontext cu_context_curr;
if (ctx->gpu >= dl_fn->nvenc_device_count) {
av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->gpu, dl_fn->nvenc_device_count);
res = AVERROR(EINVAL);
goto error;
return AVERROR(EINVAL);
}
ctx->cu_context = NULL;
......@@ -586,18 +559,30 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
if (cu_res != CUDA_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res);
res = AVERROR_EXTERNAL;
goto error;
return AVERROR_EXTERNAL;
}
cu_res = dl_fn->cu_ctx_pop_current(&cu_context_curr);
if (cu_res != CUDA_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "Failed popping CUDA context: 0x%x\n", (int)cu_res);
res = AVERROR_EXTERNAL;
goto error;
return AVERROR_EXTERNAL;
}
return 0;
}
static av_cold int nvenc_open_session(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encode_session_params = { 0 };
NVENCSTATUS nv_status;
encode_session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
encode_session_params.apiVersion = NVENCAPI_VERSION;
encode_session_params.device = ctx->cu_context;
encode_session_params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
......@@ -605,167 +590,53 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
if (nv_status != NV_ENC_SUCCESS) {
ctx->nvencoder = NULL;
av_log(avctx, AV_LOG_FATAL, "OpenEncodeSessionEx failed: 0x%x\n", (int)nv_status);
res = AVERROR_EXTERNAL;
goto error;
}
if (ctx->preset) {
if (!strcmp(ctx->preset, "slow")) {
encoder_preset = NV_ENC_PRESET_HQ_GUID;
ctx->twopass = 1;
} else if (!strcmp(ctx->preset, "medium")) {
encoder_preset = NV_ENC_PRESET_HQ_GUID;
ctx->twopass = 0;
} else if (!strcmp(ctx->preset, "fast")) {
encoder_preset = NV_ENC_PRESET_HP_GUID;
ctx->twopass = 0;
} else if (!strcmp(ctx->preset, "hq")) {
encoder_preset = NV_ENC_PRESET_HQ_GUID;
} else if (!strcmp(ctx->preset, "hp")) {
encoder_preset = NV_ENC_PRESET_HP_GUID;
} else if (!strcmp(ctx->preset, "bd")) {
encoder_preset = NV_ENC_PRESET_BD_GUID;
} else if (!strcmp(ctx->preset, "ll")) {
encoder_preset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID;
isLL = 1;
} else if (!strcmp(ctx->preset, "llhp")) {
encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
isLL = 1;
} else if (!strcmp(ctx->preset, "llhq")) {
encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
isLL = 1;
} else if (!strcmp(ctx->preset, "lossless")) {
encoder_preset = NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID;
lossless = 1;
} else if (!strcmp(ctx->preset, "losslesshp")) {
encoder_preset = NV_ENC_PRESET_LOSSLESS_HP_GUID;
lossless = 1;
} else if (!strcmp(ctx->preset, "default")) {
encoder_preset = NV_ENC_PRESET_DEFAULT_GUID;
} else {
av_log(avctx, AV_LOG_FATAL, "Preset \"%s\" is unknown! Supported presets: slow, medium, fast, hp, hq, bd, ll, llhp, llhq, lossless, losslesshp, default\n", ctx->preset);
res = AVERROR(EINVAL);
goto error;
}
}
if (ctx->twopass < 0) {
ctx->twopass = isLL;
}
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
codec = NV_ENC_CODEC_H264_GUID;
break;
case AV_CODEC_ID_H265:
codec = NV_ENC_CODEC_HEVC_GUID;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
res = AVERROR(EINVAL);
goto error;
}
nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, codec, encoder_preset, &preset_config);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "GetEncodePresetConfig failed: 0x%x\n", (int)nv_status);
res = AVERROR_EXTERNAL;
goto error;
}
ctx->init_encode_params.encodeGUID = codec;
ctx->init_encode_params.encodeHeight = avctx->height;
ctx->init_encode_params.encodeWidth = avctx->width;
if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den &&
(avctx->sample_aspect_ratio.num != 1 || avctx->sample_aspect_ratio.num != 1)) {
av_reduce(&dw, &dh,
avctx->width * avctx->sample_aspect_ratio.num,
avctx->height * avctx->sample_aspect_ratio.den,
1024 * 1024);
ctx->init_encode_params.darHeight = dh;
ctx->init_encode_params.darWidth = dw;
} else {
ctx->init_encode_params.darHeight = avctx->height;
ctx->init_encode_params.darWidth = avctx->width;
return AVERROR_EXTERNAL;
}
// De-compensate for hardware, dubiously, trying to compensate for
// playback at 704 pixel width.
if (avctx->width == 720 &&
(avctx->height == 480 || avctx->height == 576)) {
av_reduce(&dw, &dh,
ctx->init_encode_params.darWidth * 44,
ctx->init_encode_params.darHeight * 45,
1024 * 1024);
ctx->init_encode_params.darHeight = dh;
ctx->init_encode_params.darWidth = dw;
}
return 0;
}
ctx->init_encode_params.frameRateNum = avctx->time_base.den;
ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
static av_cold void set_constqp(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4);
ctx->max_surface_count = (num_mbs >= 8160) ? 32 : 48;
ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
ctx->encode_config.rcParams.constQP.qpInterB = avctx->global_quality;
ctx->encode_config.rcParams.constQP.qpInterP = avctx->global_quality;
ctx->encode_config.rcParams.constQP.qpIntra = avctx->global_quality;
}
if (ctx->buffer_delay >= ctx->max_surface_count)
ctx->buffer_delay = ctx->max_surface_count - 1;
static av_cold void set_vbr(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
ctx->init_encode_params.enableEncodeAsync = 0;
ctx->init_encode_params.enablePTD = 1;
ctx->encode_config.rcParams.enableMinQP = 1;
ctx->encode_config.rcParams.enableMaxQP = 1;
ctx->init_encode_params.presetGUID = encoder_preset;
ctx->encode_config.rcParams.minQP.qpInterB = avctx->qmin;
ctx->encode_config.rcParams.minQP.qpInterP = avctx->qmin;
ctx->encode_config.rcParams.minQP.qpIntra = avctx->qmin;
ctx->init_encode_params.encodeConfig = &ctx->encode_config;
memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
ctx->encode_config.version = NV_ENC_CONFIG_VER;
ctx->encode_config.rcParams.maxQP.qpInterB = avctx->qmax;
ctx->encode_config.rcParams.maxQP.qpInterP = avctx->qmax;
ctx->encode_config.rcParams.maxQP.qpIntra = avctx->qmax;
}
if (avctx->refs >= 0) {
/* 0 means "let the hardware decide" */
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
ctx->encode_config.encodeCodecConfig.h264Config.maxNumRefFrames = avctx->refs;
break;
case AV_CODEC_ID_H265:
ctx->encode_config.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = avctx->refs;
break;
/* Earlier switch/case will return if unknown codec is passed. */
}
}
static av_cold void set_lossless(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
if (avctx->gop_size > 0) {
if (avctx->max_b_frames >= 0) {
/* 0 is intra-only, 1 is I/P only, 2 is one B Frame, 3 two B frames, and so on. */
ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
}
ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
ctx->encode_config.rcParams.constQP.qpInterB = 0;
ctx->encode_config.rcParams.constQP.qpInterP = 0;
ctx->encode_config.rcParams.constQP.qpIntra = 0;
}
ctx->encode_config.gopLength = avctx->gop_size;
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = avctx->gop_size;
break;
case AV_CODEC_ID_H265:
ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = avctx->gop_size;
break;
/* Earlier switch/case will return if unknown codec is passed. */
}
} else if (avctx->gop_size == 0) {
ctx->encode_config.frameIntervalP = 0;
ctx->encode_config.gopLength = 1;
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = 1;
break;
case AV_CODEC_ID_H265:
ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = 1;
break;
/* Earlier switch/case will return if unknown codec is passed. */
}
}
static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx, int lossless)
{
NvencContext *ctx = avctx->priv_data;
/* when there're b frames, set dts offset */
if (ctx->encode_config.frameIntervalP >= 2)
ctx->last_dts = -2;
int qp_inter_p;
if (avctx->bit_rate > 0) {
ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
......@@ -780,10 +651,7 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
if (avctx->codec->id == AV_CODEC_ID_H264)
ctx->encode_config.encodeCodecConfig.h264Config.qpPrimeYZeroTransformBypassFlag = 1;
ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
ctx->encode_config.rcParams.constQP.qpInterB = 0;
ctx->encode_config.rcParams.constQP.qpInterP = 0;
ctx->encode_config.rcParams.constQP.qpIntra = 0;
set_lossless(avctx);
avctx->qmin = -1;
avctx->qmax = -1;
......@@ -802,30 +670,18 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
if (avctx->codec->id == AV_CODEC_ID_H264) {
ctx->encode_config.encodeCodecConfig.h264Config.outputBufferingPeriodSEI = 1;
ctx->encode_config.encodeCodecConfig.h264Config.outputPictureTimingSEI = 1;
} else if(avctx->codec->id == AV_CODEC_ID_H265) {
} else if (avctx->codec->id == AV_CODEC_ID_H265) {
ctx->encode_config.encodeCodecConfig.hevcConfig.outputBufferingPeriodSEI = 1;
ctx->encode_config.encodeCodecConfig.hevcConfig.outputPictureTimingSEI = 1;
}
} else if (avctx->global_quality > 0) {
ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
ctx->encode_config.rcParams.constQP.qpInterB = avctx->global_quality;
ctx->encode_config.rcParams.constQP.qpInterP = avctx->global_quality;
ctx->encode_config.rcParams.constQP.qpIntra = avctx->global_quality;
set_constqp(avctx);
avctx->qmin = -1;
avctx->qmax = -1;
} else {
if (avctx->qmin >= 0 && avctx->qmax >= 0) {
ctx->encode_config.rcParams.enableMinQP = 1;
ctx->encode_config.rcParams.enableMaxQP = 1;
ctx->encode_config.rcParams.minQP.qpInterB = avctx->qmin;
ctx->encode_config.rcParams.minQP.qpInterP = avctx->qmin;
ctx->encode_config.rcParams.minQP.qpIntra = avctx->qmin;
ctx->encode_config.rcParams.maxQP.qpInterB = avctx->qmax;
ctx->encode_config.rcParams.maxQP.qpInterP = avctx->qmax;
ctx->encode_config.rcParams.maxQP.qpIntra = avctx->qmax;
set_vbr(avctx);
qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin
......@@ -851,7 +707,7 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
ctx->encode_config.rcParams.enableInitialRCQP = 1;
ctx->encode_config.rcParams.initialRCQP.qpInterP = qp_inter_p;
if(avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
ctx->encode_config.rcParams.initialRCQP.qpIntra = av_clip(
qp_inter_p * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
ctx->encode_config.rcParams.initialRCQP.qpInterB = av_clip(
......@@ -867,15 +723,13 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
} else if (ctx->encode_config.rcParams.averageBitRate > 0) {
ctx->encode_config.rcParams.vbvBufferSize = 2 * ctx->encode_config.rcParams.averageBitRate;
}
}
if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
} else {
ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
}
static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx, int lossless)
{
NvencContext *ctx = avctx->priv_data;
int res;
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourMatrix = avctx->colorspace;
ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourPrimaries = avctx->color_primaries;
ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.transferCharacteristics = avctx->color_trc;
......@@ -918,7 +772,7 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
break;
}
} else if(!lossless) {
} else if (!lossless) {
if (!strcmp(ctx->profile, "high")) {
ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
avctx->profile = FF_PROFILE_H264_HIGH;
......@@ -933,8 +787,7 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
avctx->profile = FF_PROFILE_H264_HIGH_444_PREDICTIVE;
} else {
av_log(avctx, AV_LOG_FATAL, "Profile \"%s\" is unknown! Supported profiles: high, main, baseline\n", ctx->profile);
res = AVERROR(EINVAL);
goto error;
return AVERROR(EINVAL);
}
}
......@@ -951,14 +804,20 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
if (res) {
av_log(avctx, AV_LOG_FATAL, "Level \"%s\" is unknown! Supported levels: auto, 1, 1b, 1.1, 1.2, 1.3, 2, 2.1, 2.2, 3, 3.1, 3.2, 4, 4.1, 4.2, 5, 5.1\n", ctx->level);
goto error;
return res;
}
} else {
ctx->encode_config.encodeCodecConfig.h264Config.level = NV_ENC_LEVEL_AUTOSELECT;
}
break;
case AV_CODEC_ID_H265:
return 0;
}
static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
int res;
ctx->encode_config.encodeCodecConfig.hevcConfig.hevcVUIParameters.colourMatrix = avctx->colorspace;
ctx->encode_config.encodeCodecConfig.hevcConfig.hevcVUIParameters.colourPrimaries = avctx->color_primaries;
ctx->encode_config.encodeCodecConfig.hevcConfig.hevcVUIParameters.transferCharacteristics = avctx->color_trc;
......@@ -990,7 +849,7 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
if (res) {
av_log(avctx, AV_LOG_FATAL, "Level \"%s\" is unknown! Supported levels: auto, 1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 5.1, 5.2, 6, 6.1, 6.2\n", ctx->level);
goto error;
return res;
}
} else {
ctx->encode_config.encodeCodecConfig.hevcConfig.level = NV_ENC_LEVEL_AUTOSELECT;
......@@ -1003,39 +862,249 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
ctx->encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_HIGH;
} else {
av_log(avctx, AV_LOG_FATAL, "Tier \"%s\" is unknown! Supported tiers: main, high\n", ctx->tier);
res = AVERROR(EINVAL);
goto error;
return AVERROR(EINVAL);
}
}
return 0;
}
static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx, int lossless)
{
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
return nvenc_setup_h264_config(avctx, lossless);
case AV_CODEC_ID_H265:
return nvenc_setup_hevc_config(avctx);
/* Earlier switch/case will return if unknown codec is passed. */
}
return 0;
}
static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
NV_ENC_PRESET_CONFIG preset_config = { 0 };
GUID encoder_preset = NV_ENC_PRESET_HQ_GUID;
GUID codec;
NVENCSTATUS nv_status = NV_ENC_SUCCESS;
AVCPBProperties *cpb_props;
int num_mbs;
int isLL = 0;
int lossless = 0;
int res = 0;
int dw, dh;
ctx->last_dts = AV_NOPTS_VALUE;
ctx->encode_config.version = NV_ENC_CONFIG_VER;
ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
preset_config.version = NV_ENC_PRESET_CONFIG_VER;
preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
if (ctx->preset) {
if (!strcmp(ctx->preset, "slow")) {
encoder_preset = NV_ENC_PRESET_HQ_GUID;
ctx->twopass = 1;
} else if (!strcmp(ctx->preset, "medium")) {
encoder_preset = NV_ENC_PRESET_HQ_GUID;
ctx->twopass = 0;
} else if (!strcmp(ctx->preset, "fast")) {
encoder_preset = NV_ENC_PRESET_HP_GUID;
ctx->twopass = 0;
} else if (!strcmp(ctx->preset, "hq")) {
encoder_preset = NV_ENC_PRESET_HQ_GUID;
} else if (!strcmp(ctx->preset, "hp")) {
encoder_preset = NV_ENC_PRESET_HP_GUID;
} else if (!strcmp(ctx->preset, "bd")) {
encoder_preset = NV_ENC_PRESET_BD_GUID;
} else if (!strcmp(ctx->preset, "ll")) {
encoder_preset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID;
isLL = 1;
} else if (!strcmp(ctx->preset, "llhp")) {
encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
isLL = 1;
} else if (!strcmp(ctx->preset, "llhq")) {
encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
isLL = 1;
} else if (!strcmp(ctx->preset, "lossless")) {
encoder_preset = NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID;
lossless = 1;
} else if (!strcmp(ctx->preset, "losslesshp")) {
encoder_preset = NV_ENC_PRESET_LOSSLESS_HP_GUID;
lossless = 1;
} else if (!strcmp(ctx->preset, "default")) {
encoder_preset = NV_ENC_PRESET_DEFAULT_GUID;
} else {
av_log(avctx, AV_LOG_FATAL, "Preset \"%s\" is unknown! Supported presets: slow, medium, fast, hp, hq, bd, ll, llhp, llhq, lossless, losslesshp, default\n", ctx->preset);
return AVERROR(EINVAL);
}
}
if (ctx->twopass < 0) {
ctx->twopass = isLL;
}
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
codec = NV_ENC_CODEC_H264_GUID;
break;
case AV_CODEC_ID_H265:
codec = NV_ENC_CODEC_HEVC_GUID;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
return AVERROR(EINVAL);
}
nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, codec, encoder_preset, &preset_config);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "GetEncodePresetConfig failed: 0x%x\n", (int)nv_status);
return AVERROR_EXTERNAL;
}
ctx->init_encode_params.encodeGUID = codec;
ctx->init_encode_params.encodeHeight = avctx->height;
ctx->init_encode_params.encodeWidth = avctx->width;
if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den &&
(avctx->sample_aspect_ratio.num != 1 || avctx->sample_aspect_ratio.num != 1)) {
av_reduce(&dw, &dh,
avctx->width * avctx->sample_aspect_ratio.num,
avctx->height * avctx->sample_aspect_ratio.den,
1024 * 1024);
ctx->init_encode_params.darHeight = dh;
ctx->init_encode_params.darWidth = dw;
} else {
ctx->init_encode_params.darHeight = avctx->height;
ctx->init_encode_params.darWidth = avctx->width;
}
// De-compensate for hardware, dubiously, trying to compensate for
// playback at 704 pixel width.
if (avctx->width == 720 &&
(avctx->height == 480 || avctx->height == 576)) {
av_reduce(&dw, &dh,
ctx->init_encode_params.darWidth * 44,
ctx->init_encode_params.darHeight * 45,
1024 * 1024);
ctx->init_encode_params.darHeight = dh;
ctx->init_encode_params.darWidth = dw;
}
ctx->init_encode_params.frameRateNum = avctx->time_base.den;
ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4);
ctx->max_surface_count = (num_mbs >= 8160) ? 32 : 48;
if (ctx->buffer_delay >= ctx->max_surface_count)
ctx->buffer_delay = ctx->max_surface_count - 1;
ctx->init_encode_params.enableEncodeAsync = 0;
ctx->init_encode_params.enablePTD = 1;
ctx->init_encode_params.presetGUID = encoder_preset;
ctx->init_encode_params.encodeConfig = &ctx->encode_config;
memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
ctx->encode_config.version = NV_ENC_CONFIG_VER;
if (avctx->refs >= 0) {
/* 0 means "let the hardware decide" */
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
ctx->encode_config.encodeCodecConfig.h264Config.maxNumRefFrames = avctx->refs;
break;
case AV_CODEC_ID_H265:
ctx->encode_config.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = avctx->refs;
break;
/* Earlier switch/case will return if unknown codec is passed. */
}
}
if (avctx->gop_size > 0) {
if (avctx->max_b_frames >= 0) {
/* 0 is intra-only, 1 is I/P only, 2 is one B Frame, 3 two B frames, and so on. */
ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
}
ctx->encode_config.gopLength = avctx->gop_size;
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = avctx->gop_size;
break;
case AV_CODEC_ID_H265:
ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = avctx->gop_size;
break;
/* Earlier switch/case will return if unknown codec is passed. */
}
} else if (avctx->gop_size == 0) {
ctx->encode_config.frameIntervalP = 0;
ctx->encode_config.gopLength = 1;
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = 1;
break;
case AV_CODEC_ID_H265:
ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = 1;
break;
/* Earlier switch/case will return if unknown codec is passed. */
}
}
/* when there're b frames, set dts offset */
if (ctx->encode_config.frameIntervalP >= 2)
ctx->last_dts = -2;
nvenc_setup_rate_control(avctx, lossless);
if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
} else {
ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
}
res = nvenc_setup_codec_config(avctx, lossless);
if (res)
return res;
nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "InitializeEncoder failed: 0x%x\n", (int)nv_status);
res = AVERROR_EXTERNAL;
goto error;
return AVERROR_EXTERNAL;
}
ctx->input_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->input_surfaces));
if (ctx->encode_config.frameIntervalP > 1)
avctx->has_b_frames = 2;
if (!ctx->input_surfaces) {
res = AVERROR(ENOMEM);
goto error;
}
if (ctx->encode_config.rcParams.averageBitRate > 0)
avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
cpb_props = ff_add_cpb_side_data(avctx);
if (!cpb_props)
return AVERROR(ENOMEM);
cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate;
cpb_props->avg_bitrate = avctx->bit_rate;
cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize;
ctx->output_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->output_surfaces));
return 0;
}
if (!ctx->output_surfaces) {
res = AVERROR(ENOMEM);
goto error;
}
static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
{
NvencContext *ctx = avctx->priv_data;
NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
for (surfaceCount = 0; surfaceCount < ctx->max_surface_count; ++surfaceCount) {
NVENCSTATUS nv_status;
NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
......@@ -1059,22 +1128,20 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
default:
av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format\n");
res = AVERROR(EINVAL);
goto error;
return AVERROR(EINVAL);
}
nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "CreateInputBuffer failed\n");
res = AVERROR_EXTERNAL;
goto error;
return AVERROR_EXTERNAL;
}
ctx->input_surfaces[surfaceCount].lockCount = 0;
ctx->input_surfaces[surfaceCount].input_surface = allocSurf.inputBuffer;
ctx->input_surfaces[surfaceCount].format = allocSurf.bufferFmt;
ctx->input_surfaces[surfaceCount].width = allocSurf.width;
ctx->input_surfaces[surfaceCount].height = allocSurf.height;
ctx->input_surfaces[idx].lockCount = 0;
ctx->input_surfaces[idx].input_surface = allocSurf.inputBuffer;
ctx->input_surfaces[idx].format = allocSurf.bufferFmt;
ctx->input_surfaces[idx].width = allocSurf.width;
ctx->input_surfaces[idx].height = allocSurf.height;
/* 1MB is large enough to hold most output frames. NVENC increases this automaticaly if it's not enough. */
allocOut.size = 1024 * 1024;
......@@ -1084,17 +1151,50 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "CreateBitstreamBuffer failed\n");
ctx->output_surfaces[surfaceCount++].output_surface = NULL;
res = AVERROR_EXTERNAL;
goto error;
p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[idx].input_surface);
return AVERROR_EXTERNAL;
}
ctx->output_surfaces[idx].output_surface = allocOut.bitstreamBuffer;
ctx->output_surfaces[idx].size = allocOut.size;
ctx->output_surfaces[idx].busy = 0;
return 0;
}
static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx, int* surfaceCount)
{
int res;
NvencContext *ctx = avctx->priv_data;
ctx->input_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->input_surfaces));
if (!ctx->input_surfaces) {
return AVERROR(ENOMEM);
}
ctx->output_surfaces[surfaceCount].output_surface = allocOut.bitstreamBuffer;
ctx->output_surfaces[surfaceCount].size = allocOut.size;
ctx->output_surfaces[surfaceCount].busy = 0;
ctx->output_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->output_surfaces));
if (!ctx->output_surfaces) {
return AVERROR(ENOMEM);
}
if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
for (*surfaceCount = 0; *surfaceCount < ctx->max_surface_count; ++*surfaceCount) {
res = nvenc_alloc_surface(avctx, *surfaceCount);
if (res)
return res;
}
return 0;
}
static av_cold int nvenc_setup_extradata(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
NVENCSTATUS nv_status;
uint32_t outSize = 0;
char tmpHeader[256];
NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
......@@ -1107,32 +1207,55 @@ static av_cold int nvenc_encode_init(AVCodecContext *avctx)
nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "GetSequenceParams failed\n");
goto error;
return AVERROR_EXTERNAL;
}
avctx->extradata_size = outSize;
avctx->extradata = av_mallocz(outSize + AV_INPUT_BUFFER_PADDING_SIZE);
if (!avctx->extradata) {
res = AVERROR(ENOMEM);
goto error;
return AVERROR(ENOMEM);
}
memcpy(avctx->extradata, tmpHeader, outSize);
}
if (ctx->encode_config.frameIntervalP > 1)
avctx->has_b_frames = 2;
return 0;
}
if (ctx->encode_config.rcParams.averageBitRate > 0)
avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
static av_cold int nvenc_encode_init(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
cpb_props = ff_add_cpb_side_data(avctx);
if (!cpb_props)
return AVERROR(ENOMEM);
cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate;
cpb_props->avg_bitrate = avctx->bit_rate;
cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize;
int res;
int i;
int surfaceCount = 0;
if (!nvenc_dyload_nvenc(avctx))
return AVERROR_EXTERNAL;
res = nvenc_setup_device(avctx);
if (res)
goto error;
res = nvenc_open_session(avctx);
if (res)
goto error;
res = nvenc_setup_encoder(avctx);
if (res)
goto error;
res = nvenc_setup_surfaces(avctx, &surfaceCount);
if (res)
goto error;
if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
res = nvenc_setup_extradata(avctx);
if (res)
goto error;
}
return 0;
......@@ -1186,6 +1309,124 @@ static av_cold int nvenc_encode_close(AVCodecContext *avctx)
return 0;
}
static NvencInputSurface *get_free_frame(NvencContext *ctx)
{
int i;
for (i = 0; i < ctx->max_surface_count; ++i) {
if (!ctx->input_surfaces[i].lockCount) {
ctx->input_surfaces[i].lockCount = 1;
return &ctx->input_surfaces[i];
}
}
return NULL;
}
static int nvenc_copy_frame(AVCodecContext *avctx, NvencInputSurface *inSurf,
NV_ENC_LOCK_INPUT_BUFFER *lockBufferParams, const AVFrame *frame)
{
uint8_t *buf = lockBufferParams->bufferDataPtr;
int off = inSurf->height * lockBufferParams->pitch;
if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
av_image_copy_plane(buf, lockBufferParams->pitch,
frame->data[0], frame->linesize[0],
avctx->width, avctx->height);
buf += off;
av_image_copy_plane(buf, lockBufferParams->pitch >> 1,
frame->data[2], frame->linesize[2],
avctx->width >> 1, avctx->height >> 1);
buf += off >> 2;
av_image_copy_plane(buf, lockBufferParams->pitch >> 1,
frame->data[1], frame->linesize[1],
avctx->width >> 1, avctx->height >> 1);
} else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
av_image_copy_plane(buf, lockBufferParams->pitch,
frame->data[0], frame->linesize[0],
avctx->width, avctx->height);
buf += off;
av_image_copy_plane(buf, lockBufferParams->pitch,
frame->data[1], frame->linesize[1],
avctx->width, avctx->height >> 1);
} else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
av_image_copy_plane(buf, lockBufferParams->pitch,
frame->data[0], frame->linesize[0],
avctx->width, avctx->height);
buf += off;
av_image_copy_plane(buf, lockBufferParams->pitch,
frame->data[1], frame->linesize[1],
avctx->width, avctx->height);
buf += off;
av_image_copy_plane(buf, lockBufferParams->pitch,
frame->data[2], frame->linesize[2],
avctx->width, avctx->height);
} else {
av_log(avctx, AV_LOG_FATAL, "Invalid pixel format!\n");
return AVERROR(EINVAL);
}
return 0;
}
static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame,
NvencInputSurface *nvenc_frame)
{
NvencContext *ctx = avctx->priv_data;
NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
int res;
NVENCSTATUS nv_status;
NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
lockBufferParams.inputBuffer = nvenc_frame->input_surface;
nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_ERROR, "Failed locking nvenc input buffer\n");
return AVERROR_EXTERNAL;
}
res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame);
nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, nvenc_frame->input_surface);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "Failed unlocking input buffer!\n");
return AVERROR_EXTERNAL;
}
return res;
}
static void nvenc_codec_specific_pic_params(AVCodecContext *avctx,
NV_ENC_PIC_PARAMS *params)
{
NvencContext *ctx = avctx->priv_data;
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
params->codecPicParams.h264PicParams.sliceMode = ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
params->codecPicParams.h264PicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
break;
case AV_CODEC_ID_H265:
params->codecPicParams.hevcPicParams.sliceMode = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
params->codecPicParams.hevcPicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
break;
}
}
static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencOutputSurface *tmpoutsurf)
{
NvencContext *ctx = avctx->priv_data;
......@@ -1314,86 +1555,15 @@ static int nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
pic_params.version = NV_ENC_PIC_PARAMS_VER;
if (frame) {
NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
NvencInputSurface *inSurf = NULL;
for (i = 0; i < ctx->max_surface_count; ++i) {
if (!ctx->input_surfaces[i].lockCount) {
inSurf = &ctx->input_surfaces[i];
break;
}
}
NvencInputSurface *inSurf;
inSurf = get_free_frame(ctx);
av_assert0(inSurf);
inSurf->lockCount = 1;
lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
lockBufferParams.inputBuffer = inSurf->input_surface;
nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_ERROR, "Failed locking nvenc input buffer\n");
return 0;
}
if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
uint8_t *buf = lockBufferParams.bufferDataPtr;
av_image_copy_plane(buf, lockBufferParams.pitch,
frame->data[0], frame->linesize[0],
avctx->width, avctx->height);
buf += inSurf->height * lockBufferParams.pitch;
av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
frame->data[2], frame->linesize[2],
avctx->width >> 1, avctx->height >> 1);
buf += (inSurf->height * lockBufferParams.pitch) >> 2;
av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
frame->data[1], frame->linesize[1],
avctx->width >> 1, avctx->height >> 1);
} else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
uint8_t *buf = lockBufferParams.bufferDataPtr;
av_image_copy_plane(buf, lockBufferParams.pitch,
frame->data[0], frame->linesize[0],
avctx->width, avctx->height);
buf += inSurf->height * lockBufferParams.pitch;
av_image_copy_plane(buf, lockBufferParams.pitch,
frame->data[1], frame->linesize[1],
avctx->width, avctx->height >> 1);
} else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
uint8_t *buf = lockBufferParams.bufferDataPtr;
av_image_copy_plane(buf, lockBufferParams.pitch,
frame->data[0], frame->linesize[0],
avctx->width, avctx->height);
buf += inSurf->height * lockBufferParams.pitch;
av_image_copy_plane(buf, lockBufferParams.pitch,
frame->data[1], frame->linesize[1],
avctx->width, avctx->height);
buf += inSurf->height * lockBufferParams.pitch;
av_image_copy_plane(buf, lockBufferParams.pitch,
frame->data[2], frame->linesize[2],
avctx->width, avctx->height);
} else {
av_log(avctx, AV_LOG_FATAL, "Invalid pixel format!\n");
return AVERROR(EINVAL);
}
nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, inSurf->input_surface);
if (nv_status != NV_ENC_SUCCESS) {
av_log(avctx, AV_LOG_FATAL, "Failed unlocking input buffer!\n");
return AVERROR_EXTERNAL;
res = nvenc_upload_frame(avctx, frame, inSurf);
if (res) {
inSurf->lockCount = 0;
return res;
}
for (i = 0; i < ctx->max_surface_count; ++i)
......@@ -1428,24 +1598,15 @@ static int nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
pic_params.encodePicFlags = 0;
pic_params.inputTimeStamp = frame->pts;
pic_params.inputDuration = 0;
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
pic_params.codecPicParams.h264PicParams.sliceMode = ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
pic_params.codecPicParams.h264PicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
break;
case AV_CODEC_ID_H265:
pic_params.codecPicParams.hevcPicParams.sliceMode = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
pic_params.codecPicParams.hevcPicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
return AVERROR(EINVAL);
}
nvenc_codec_specific_pic_params(avctx, &pic_params);
res = timestamp_queue_enqueue(&ctx->timestamp_list, frame->pts);
if (res)
if (res) {
inSurf->lockCount = 0;
return res;
}
} else {
pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment