Lines Matching refs:avctx

39 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
139 static int nvenc_print_error(AVCodecContext *avctx, NVENCSTATUS err,
147 NvencContext *ctx = avctx->priv_data;
154 av_log(avctx, AV_LOG_ERROR, "%s: %s (%d): %s\n", error_string, desc, err, details);
223 static void nvenc_print_driver_requirement(AVCodecContext *avctx, int level)
276 av_log(avctx, level, "The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
279 static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
281 NvencContext *ctx = avctx->priv_data;
287 ret = cuda_load_functions(&dl_fn->cuda_dl, avctx);
291 ret = nvenc_load_functions(&dl_fn->nvenc_dl, avctx);
293 nvenc_print_driver_requirement(avctx, AV_LOG_ERROR);
299 return nvenc_print_error(avctx, err, "Failed to query nvenc max version");
301 av_log(avctx, AV_LOG_VERBOSE, "Loaded Nvenc version %d.%d\n", nvenc_max_ver >> 4, nvenc_max_ver & 0xf);
304 av_log(avctx, AV_LOG_ERROR, "Driver does not support the required nvenc API version. "
308 nvenc_print_driver_requirement(avctx, AV_LOG_ERROR);
316 return nvenc_print_error(avctx, err, "Failed to create nvenc instance");
318 av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
323 static int nvenc_push_context(AVCodecContext *avctx)
325 NvencContext *ctx = avctx->priv_data;
334 static int nvenc_pop_context(AVCodecContext *avctx)
336 NvencContext *ctx = avctx->priv_data;
346 static av_cold int nvenc_open_session(AVCodecContext *avctx)
349 NvencContext *ctx = avctx->priv_data;
366 return nvenc_print_error(avctx, ret, "OpenEncodeSessionEx failed");
372 static int nvenc_check_codec_support(AVCodecContext *avctx)
374 NvencContext *ctx = avctx->priv_data;
408 static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
410 NvencContext *ctx = avctx->priv_data;
425 static int nvenc_check_capabilities(AVCodecContext *avctx)
427 NvencContext *ctx = avctx->priv_data;
430 ret = nvenc_check_codec_support(avctx);
432 av_log(avctx, AV_LOG_WARNING, "Codec not supported\n");
436 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_YUV444_ENCODE);
438 av_log(avctx, AV_LOG_WARNING, "YUV444P not supported\n");
442 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE);
444 av_log(avctx, AV_LOG_WARNING, "Lossless encoding not supported\n");
448 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_WIDTH_MAX);
449 if (ret < avctx->width) {
450 av_log(avctx, AV_LOG_WARNING, "Width %d exceeds %d\n",
451 avctx->width, ret);
455 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_HEIGHT_MAX);
456 if (ret < avctx->height) {
457 av_log(avctx, AV_LOG_WARNING, "Height %d exceeds %d\n",
458 avctx->height, ret);
462 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_NUM_MAX_BFRAMES);
463 if (ret < avctx->max_b_frames) {
464 av_log(avctx, AV_LOG_WARNING, "Max B-frames %d exceed %d\n",
465 avctx->max_b_frames, ret);
470 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_FIELD_ENCODING);
471 if (ret < 1 && avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
472 av_log(avctx, AV_LOG_WARNING,
478 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_10BIT_ENCODE);
480 av_log(avctx, AV_LOG_WARNING, "10 bit encode not supported\n");
484 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOOKAHEAD);
486 av_log(avctx, AV_LOG_WARNING, "RC lookahead not supported\n");
490 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ);
492 av_log(avctx, AV_LOG_WARNING, "Temporal AQ not supported\n");
496 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION);
498 av_log (avctx, AV_LOG_WARNING, "Weighted Prediction not supported\n");
502 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_CABAC);
504 av_log(avctx, AV_LOG_WARNING, "CABAC entropy coding not supported\n");
509 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE);
511 av_log(avctx, AV_LOG_WARNING, "Each B frame as reference is not supported\n");
514 av_log(avctx, AV_LOG_WARNING, "B frames as references are not supported\n");
519 av_log(avctx, AV_LOG_WARNING, "B frames as references need SDK 8.1 at build time\n");
525 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES);
526 if(avctx->refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT && ret <= 0) {
527 av_log(avctx, AV_LOG_WARNING, "Multiple reference frames are not supported by the device\n");
531 if(avctx->refs != 0) {
532 av_log(avctx, AV_LOG_WARNING, "Multiple reference frames need SDK 9.1 at build time\n");
538 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SINGLE_SLICE_INTRA_REFRESH);
540 av_log(avctx, AV_LOG_WARNING, "Single slice intra refresh not supported by the device\n");
545 av_log(avctx, AV_LOG_WARNING, "Single slice intra refresh needs SDK 11.1 at build time\n");
550 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_INTRA_REFRESH);
552 av_log(avctx, AV_LOG_WARNING, "Intra refresh not supported by the device\n");
557 if (ctx->constrained_encoding && avctx->codec->id == AV_CODEC_ID_HEVC) {
558 av_log(avctx, AV_LOG_WARNING, "HEVC constrained encoding needs SDK 10.0 at build time\n");
563 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING);
565 av_log(avctx, AV_LOG_WARNING, "Constrained encoding not supported by the device\n");
569 ctx->support_dyn_bitrate = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
574 static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
576 NvencContext *ctx = avctx->priv_data;
599 av_log(avctx, loglevel, "[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx, name, major, minor);
601 av_log(avctx, loglevel, "does not support NVENC\n");
615 if ((ret = nvenc_pop_context(avctx)) < 0)
618 if ((ret = nvenc_open_session(avctx)) < 0)
621 if ((ret = nvenc_check_capabilities(avctx)) < 0)
624 av_log(avctx, loglevel, "supports NVENC\n");
632 if ((ret = nvenc_push_context(avctx)) < 0)
638 if ((ret = nvenc_pop_context(avctx)) < 0)
649 static av_cold int nvenc_setup_device(AVCodecContext *avctx)
651 NvencContext *ctx = avctx->priv_data;
654 switch (avctx->codec->id) {
668 av_log(avctx, AV_LOG_WARNING, "The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
670 if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11 || avctx->hw_frames_ctx || avctx->hw_device_ctx) {
679 if (avctx->hw_frames_ctx) {
680 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
689 } else if (avctx->hw_device_ctx) {
690 hwdev_ctx = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
714 ret = nvenc_open_session(avctx);
718 ret = nvenc_check_capabilities(avctx);
720 av_log(avctx, AV_LOG_FATAL, "Provided device doesn't support required NVENC features\n");
733 av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
737 av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", nb_devices);
741 if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES)
749 av_log(avctx, AV_LOG_FATAL, "No capable devices found\n");
753 av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->device, nb_devices);
760 static av_cold void set_constqp(AVCodecContext *avctx)
762 NvencContext *ctx = avctx->priv_data;
772 } else if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
774 rc->constQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51);
776 rc->constQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51);
783 if (avctx->b_quant_factor != 0.0)
784 rc->constQP.qpInterB = av_clip(ctx->cqp * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51);
785 if (avctx->i_quant_factor != 0.0)
786 rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51);
789 avctx->qmin = -1;
790 avctx->qmax = -1;
793 static av_cold void set_vbr(AVCodecContext *avctx)
795 NvencContext *ctx = avctx->priv_data;
799 if (avctx->qmin >= 0 && avctx->qmax >= 0) {
803 rc->minQP.qpInterB = avctx->qmin;
804 rc->minQP.qpInterP = avctx->qmin;
805 rc->minQP.qpIntra = avctx->qmin;
807 rc->maxQP.qpInterB = avctx->qmax;
808 rc->maxQP.qpInterP = avctx->qmax;
809 rc->maxQP.qpIntra = avctx->qmax;
811 qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin
812 } else if (avctx->qmin >= 0) {
815 rc->minQP.qpInterB = avctx->qmin;
816 rc->minQP.qpInterP = avctx->qmin;
817 rc->minQP.qpIntra = avctx->qmin;
819 qp_inter_p = avctx->qmin;
833 if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
835 rc->initialRCQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51);
844 if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
846 rc->initialRCQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51);
855 static av_cold void set_lossless(AVCodecContext *avctx)
857 NvencContext *ctx = avctx->priv_data;
865 avctx->qmin = -1;
866 avctx->qmax = -1;
869 static void nvenc_override_rate_control(AVCodecContext *avctx)
871 NvencContext *ctx = avctx->priv_data;
876 set_constqp(avctx);
879 if (avctx->qmin < 0) {
880 av_log(avctx, AV_LOG_WARNING,
883 set_vbr(avctx);
889 set_vbr(avctx);
900 static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx)
902 NvencContext *ctx = avctx->priv_data;
915 av_log(avctx, AV_LOG_WARNING,
923 av_log(avctx, AV_LOG_WARNING,
939 static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx)
941 NvencContext *ctx = avctx->priv_data;
943 if (avctx->global_quality > 0)
944 av_log(avctx, AV_LOG_WARNING, "Using global_quality with nvenc is deprecated. Use qp instead.\n");
946 if (ctx->cqp < 0 && avctx->global_quality > 0)
947 ctx->cqp = avctx->global_quality;
949 if (avctx->bit_rate > 0) {
950 ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
955 if (avctx->rc_max_rate > 0)
956 ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
995 } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
1002 av_log(avctx, AV_LOG_WARNING, "Specified rc mode is deprecated.\n");
1003 av_log(avctx, AV_LOG_WARNING, "Use -rc constqp/cbr/vbr, -tune and -multipass instead.\n");
1013 av_log(avctx, AV_LOG_WARNING, "Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1022 set_lossless(avctx);
1024 nvenc_override_rate_control(avctx);
1027 set_vbr(avctx);
1030 if (avctx->rc_buffer_size > 0) {
1031 ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size;
1033 avctx->rc_buffer_size = ctx->encode_config.rcParams.vbvBufferSize = 2 * ctx->encode_config.rcParams.averageBitRate;
1039 av_log(avctx, AV_LOG_VERBOSE, "AQ enabled.\n");
1044 av_log(avctx, AV_LOG_VERBOSE, "Temporal AQ enabled.\n");
1052 av_log(avctx, AV_LOG_WARNING,
1059 av_log(avctx, AV_LOG_VERBOSE,
1069 av_log(avctx, AV_LOG_VERBOSE, "Strict GOP target enabled.\n");
1084 av_log(avctx, AV_LOG_VERBOSE, "CQ(%d) mode enabled.\n", tmp_quality);
1087 ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate = 0;
1088 ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size = 0;
1089 ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
1093 static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx)
1095 NvencContext *ctx = avctx->priv_data;
1100 vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace;
1101 vui->colourPrimaries = avctx->color_primaries;
1102 vui->transferCharacteristics = avctx->color_trc;
1103 vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
1115 h264->sliceModeData = avctx->slices > 0 ? avctx->slices : 1;
1119 h264->intraRefreshPeriod = avctx->gop_size;
1120 h264->intraRefreshCnt = avctx->gop_size - 1;
1129 h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1130 h264->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1140 } else if (avctx->gop_size >= 0) {
1141 h264->idrPeriod = avctx->gop_size;
1163 avctx->profile = FF_PROFILE_H264_BASELINE;
1167 avctx->profile = FF_PROFILE_H264_MAIN;
1171 avctx->profile = FF_PROFILE_H264_HIGH;
1175 avctx->profile = FF_PROFILE_H264_HIGH_444_PREDICTIVE;
1183 avctx->profile = FF_PROFILE_H264_HIGH_444_PREDICTIVE;
1186 h264->chromaFormatIDC = avctx->profile == FF_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1;
1198 h264->numRefL0 = avctx->refs;
1199 h264->numRefL1 = avctx->refs;
1205 static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
1207 NvencContext *ctx = avctx->priv_data;
1212 vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace;
1213 vui->colourPrimaries = avctx->color_primaries;
1214 vui->transferCharacteristics = avctx->color_trc;
1215 vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
1227 hevc->sliceModeData = avctx->slices > 0 ? avctx->slices : 1;
1231 hevc->intraRefreshPeriod = avctx->gop_size;
1232 hevc->intraRefreshCnt = avctx->gop_size - 1;
1243 hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1244 hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1254 } else if (avctx->gop_size >= 0) {
1255 hevc->idrPeriod = avctx->gop_size;
1267 avctx->profile = FF_PROFILE_HEVC_MAIN;
1271 avctx->profile = FF_PROFILE_HEVC_MAIN_10;
1275 avctx->profile = FF_PROFILE_HEVC_REXT;
1282 avctx->profile = FF_PROFILE_HEVC_MAIN_10;
1288 avctx->profile = FF_PROFILE_HEVC_REXT;
1304 hevc->numRefL0 = avctx->refs;
1305 hevc->numRefL1 = avctx->refs;
1311 static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx)
1313 switch (avctx->codec->id) {
1315 return nvenc_setup_h264_config(avctx);
1317 return nvenc_setup_hevc_config(avctx);
1324 static void compute_dar(AVCodecContext *avctx, int *dw, int *dh) {
1327 sw = avctx->width;
1328 sh = avctx->height;
1330 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
1331 sw *= avctx->sample_aspect_ratio.num;
1332 sh *= avctx->sample_aspect_ratio.den;
1338 static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
1340 NvencContext *ctx = avctx->priv_data;
1353 ctx->init_encode_params.encodeHeight = avctx->height;
1354 ctx->init_encode_params.encodeWidth = avctx->width;
1381 return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration");
1387 compute_dar(avctx, &dw, &dh);
1391 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
1392 ctx->init_encode_params.frameRateNum = avctx->framerate.num;
1393 ctx->init_encode_params.frameRateDen = avctx->framerate.den;
1395 ctx->init_encode_params.frameRateNum = avctx->time_base.den;
1396 ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
1415 ctx->dpb_size = FFMIN(FFMAX(avctx->refs, 0), 6);
1416 avctx->max_b_frames = FFMIN(avctx->max_b_frames, 3);
1417 switch (avctx->codec->id) {
1428 if (avctx->gop_size > 0) {
1429 if (avctx->max_b_frames >= 0) {
1431 ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
1434 ctx->encode_config.gopLength = avctx->gop_size;
1435 } else if (avctx->gop_size == 0) {
1447 nvenc_recalc_surfaces(avctx);
1449 nvenc_setup_rate_control(avctx);
1451 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
1457 res = nvenc_setup_codec_config(avctx);
1461 res = nvenc_push_context(avctx);
1467 nvenc_pop_context(avctx);
1468 return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed");
1475 nvenc_pop_context(avctx);
1476 return nvenc_print_error(avctx, nv_status, "SetIOCudaStreams failed");
1481 res = nvenc_pop_context(avctx);
1486 avctx->has_b_frames = 2;
1489 avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
1491 cpb_props = ff_add_cpb_side_data(avctx);
1495 cpb_props->avg_bitrate = avctx->bit_rate;
1532 static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
1534 NvencContext *ctx = avctx->priv_data;
1543 if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1552 av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
1558 allocSurf.width = avctx->width;
1559 allocSurf.height = avctx->height;
1564 return nvenc_print_error(avctx, nv_status, "CreateInputBuffer failed");
1574 int err = nvenc_print_error(avctx, nv_status, "CreateBitstreamBuffer failed");
1575 if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1588 static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx)
1590 NvencContext *ctx = avctx->priv_data;
1612 res = nvenc_push_context(avctx);
1617 if ((res = nvenc_alloc_surface(avctx, i)) < 0)
1622 res2 = nvenc_pop_context(avctx);
1629 static av_cold int nvenc_setup_extradata(AVCodecContext *avctx)
1631 NvencContext *ctx = avctx->priv_data;
1647 return nvenc_print_error(avctx, nv_status, "GetSequenceParams failed");
1650 avctx->extradata_size = outSize;
1651 avctx->extradata = av_mallocz(outSize + AV_INPUT_BUFFER_PADDING_SIZE);
1653 if (!avctx->extradata) {
1657 memcpy(avctx->extradata, tmpHeader, outSize);
1662 av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
1664 NvencContext *ctx = avctx->priv_data;
1674 res = nvenc_push_context(avctx);
1686 if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) {
1698 if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1714 res = nvenc_pop_context(avctx);
1736 av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
1741 av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
1743 NvencContext *ctx = avctx->priv_data;
1746 if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1748 if (!avctx->hw_frames_ctx) {
1749 av_log(avctx, AV_LOG_ERROR,
1753 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1754 if (frames_ctx->format != avctx->pix_fmt) {
1755 av_log(avctx, AV_LOG_ERROR,
1761 ctx->data_pix_fmt = avctx->pix_fmt;
1768 if ((ret = nvenc_load_libraries(avctx)) < 0)
1771 if ((ret = nvenc_setup_device(avctx)) < 0)
1774 if ((ret = nvenc_setup_encoder(avctx)) < 0)
1777 if ((ret = nvenc_setup_surfaces(avctx)) < 0)
1780 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1781 if ((ret = nvenc_setup_extradata(avctx)) < 0)
1799 static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface,
1824 avctx->width, avctx->height);
1829 static int nvenc_find_free_reg_resource(AVCodecContext *avctx)
1831 NvencContext *ctx = avctx->priv_data;
1847 return nvenc_print_error(avctx, nv_status, "Failed unregistering unused input resource");
1859 av_log(avctx, AV_LOG_ERROR, "Too many registered CUDA frames\n");
1863 static int nvenc_register_frame(AVCodecContext *avctx, const AVFrame *frame)
1865 NvencContext *ctx = avctx->priv_data;
1874 if (avctx->pix_fmt == AV_PIX_FMT_CUDA && ctx->registered_frames[i].ptr == frame->data[0])
1876 else if (avctx->pix_fmt == AV_PIX_FMT_D3D11 && ctx->registered_frames[i].ptr == frame->data[0] && ctx->registered_frames[i].ptr_index == (intptr_t)frame->data[1])
1880 idx = nvenc_find_free_reg_resource(avctx);
1890 if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
1893 else if (avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1900 av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
1907 nvenc_print_error(avctx, ret, "Error registering an input resource");
1917 static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame,
1920 NvencContext *ctx = avctx->priv_data;
1927 if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1928 int reg_idx = nvenc_register_frame(avctx, frame);
1930 av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n");
1944 return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");
1964 return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer");
1968 res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame);
1972 return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!");
1979 static void nvenc_codec_specific_pic_params(AVCodecContext *avctx,
1984 NvencContext *ctx = avctx->priv_data;
1986 switch (avctx->codec->id) {
2026 static int nvenc_set_timestamp(AVCodecContext *avctx,
2030 NvencContext *ctx = avctx->priv_data;
2035 pkt->dts -= FFMAX(ctx->encode_config.frameIntervalP - 1, 0) * FFMAX(avctx->ticks_per_frame, 1) * FFMAX(avctx->time_base.num, 1);
2040 static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)
2042 NvencContext *ctx = avctx->priv_data;
2054 switch (avctx->codec->id) {
2062 av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
2081 res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer");
2085 res = ff_get_encode_buffer(avctx, pkt, lock_params.bitstreamSizeInBytes, 0);
2096 res = nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");
2101 if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2106 res = nvenc_print_error(avctx, nv_status, "Failed unmapping input resource");
2135 av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
2136 av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
2144 res = nvenc_set_timestamp(avctx, &lock_params, pkt);
2161 static int output_ready(AVCodecContext *avctx, int flush)
2163 NvencContext *ctx = avctx->priv_data;
2173 static int prepare_sei_data_array(AVCodecContext *avctx, const AVFrame *frame)
2175 NvencContext *ctx = avctx->priv_data;
2209 if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, &tc_data, &tc_size) < 0) {
2271 static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
2273 NvencContext *ctx = avctx->priv_data;
2286 compute_dar(avctx, &dw, &dh);
2288 av_log(avctx, AV_LOG_VERBOSE,
2301 if (avctx->bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->bit_rate) {
2302 av_log(avctx, AV_LOG_VERBOSE,
2305 (uint32_t)avctx->bit_rate);
2307 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->bit_rate;
2311 if (avctx->rc_max_rate > 0 && ctx->encode_config.rcParams.maxBitRate != avctx->rc_max_rate) {
2312 av_log(avctx, AV_LOG_VERBOSE,
2315 (uint32_t)avctx->rc_max_rate);
2317 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->rc_max_rate;
2321 if (avctx->rc_buffer_size > 0 && ctx->encode_config.rcParams.vbvBufferSize != avctx->rc_buffer_size) {
2322 av_log(avctx, AV_LOG_VERBOSE,
2325 avctx->rc_buffer_size);
2327 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->rc_buffer_size;
2346 nvenc_print_error(avctx, ret, "failed to reconfigure nvenc");
2363 static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
2371 NvencContext *ctx = avctx->priv_data;
2386 res = nvenc_push_context(avctx);
2390 reconfig_encoder(avctx, frame);
2392 res = nvenc_upload_frame(avctx, frame, in_surf);
2394 res2 = nvenc_pop_context(avctx);
2408 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2427 res = prepare_sei_data_array(avctx, frame);
2433 nvenc_codec_specific_pic_params(avctx, &pic_params, ctx->sei_data, sei_count);
2438 res = nvenc_push_context(avctx);
2447 res = nvenc_pop_context(avctx);
2453 return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
2469 int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
2474 NvencContext *ctx = avctx->priv_data;
2482 res = ff_encode_get_frame(avctx, frame);
2487 res = nvenc_send_frame(avctx, frame);
2494 if (output_ready(avctx, avctx->internal->draining)) {
2497 res = nvenc_push_context(avctx);
2501 res = process_output_surface(avctx, pkt, tmp_out_surf);
2503 res2 = nvenc_pop_context(avctx);
2511 } else if (avctx->internal->draining) {
2520 av_cold void ff_nvenc_encode_flush(AVCodecContext *avctx)
2522 NvencContext *ctx = avctx->priv_data;
2524 nvenc_send_frame(avctx, NULL);