X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fnvenc.c;h=d33ee489d001a79b40fe8e75f08e832a6a59dfd3;hb=f34521266ec5816eefa4c10db6098cb91e03c695;hp=880454c960868020dc609ac5d5bea8d43da2a333;hpb=6e1903938befc0bce2f64e01770af2f65d625cfc;p=ffmpeg diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c index 880454c9608..d33ee489d00 100644 --- a/libavcodec/nvenc.c +++ b/libavcodec/nvenc.c @@ -22,6 +22,7 @@ #include "config.h" #include "nvenc.h" +#include "hevc_sei.h" #include "libavutil/hwcontext_cuda.h" #include "libavutil/hwcontext.h" @@ -30,6 +31,8 @@ #include "libavutil/avassert.h" #include "libavutil/mem.h" #include "libavutil/pixdesc.h" +#include "atsc_a53.h" +#include "encode.h" #include "internal.h" #include "packet_internal.h" @@ -49,6 +52,8 @@ const enum AVPixelFormat ff_nvenc_pix_fmts[] = { AV_PIX_FMT_YUV444P16, // Truncated to 10bits AV_PIX_FMT_0RGB32, AV_PIX_FMT_0BGR32, + AV_PIX_FMT_GBRP, + AV_PIX_FMT_GBRP16, // Truncated to 10bits AV_PIX_FMT_CUDA, #if CONFIG_D3D11VA AV_PIX_FMT_D3D11, @@ -56,7 +61,7 @@ const enum AVPixelFormat ff_nvenc_pix_fmts[] = { AV_PIX_FMT_NONE }; -const AVCodecHWConfigInternal *ff_nvenc_hw_configs[] = { +const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[] = { HW_CONFIG_ENCODER_FRAMES(CUDA, CUDA), HW_CONFIG_ENCODER_DEVICE(NONE, CUDA), #if CONFIG_D3D11VA @@ -66,12 +71,18 @@ const AVCodecHWConfigInternal *ff_nvenc_hw_configs[] = { NULL, }; -#define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \ - pix_fmt == AV_PIX_FMT_P016 || \ - pix_fmt == AV_PIX_FMT_YUV444P16) +#define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \ + pix_fmt == AV_PIX_FMT_P016 || \ + pix_fmt == AV_PIX_FMT_YUV444P16 || \ + pix_fmt == AV_PIX_FMT_GBRP16) -#define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \ - pix_fmt == AV_PIX_FMT_YUV444P16) +#define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \ + pix_fmt == AV_PIX_FMT_YUV444P16 || \ + pix_fmt == AV_PIX_FMT_GBRP || \ + pix_fmt == AV_PIX_FMT_GBRP16) + +#define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \ + pix_fmt == AV_PIX_FMT_GBRP16) static const struct { NVENCSTATUS nverr; @@ -141,10 +152,86 @@ static int nvenc_print_error(AVCodecContext *avctx, NVENCSTATUS err, return ret; } +typedef struct GUIDTuple { + const GUID guid; + int flags; +} GUIDTuple; + +#define PRESET_ALIAS(alias, name, ...) \ + [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ } + +#define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__) + +static void nvenc_map_preset(NvencContext *ctx) +{ + GUIDTuple presets[] = { +#ifdef NVENC_HAVE_NEW_PRESETS + PRESET(P1), + PRESET(P2), + PRESET(P3), + PRESET(P4), + PRESET(P5), + PRESET(P6), + PRESET(P7), + PRESET_ALIAS(SLOW, P7, NVENC_TWO_PASSES), + PRESET_ALIAS(MEDIUM, P4, NVENC_ONE_PASS), + PRESET_ALIAS(FAST, P1, NVENC_ONE_PASS), + // Compat aliases + PRESET_ALIAS(DEFAULT, P4, NVENC_DEPRECATED_PRESET), + PRESET_ALIAS(HP, P1, NVENC_DEPRECATED_PRESET), + PRESET_ALIAS(HQ, P7, NVENC_DEPRECATED_PRESET), + PRESET_ALIAS(BD, P5, NVENC_DEPRECATED_PRESET), + PRESET_ALIAS(LOW_LATENCY_DEFAULT, P4, NVENC_DEPRECATED_PRESET | NVENC_LOWLATENCY), + PRESET_ALIAS(LOW_LATENCY_HP, P1, NVENC_DEPRECATED_PRESET | NVENC_LOWLATENCY), + PRESET_ALIAS(LOW_LATENCY_HQ, P7, NVENC_DEPRECATED_PRESET | NVENC_LOWLATENCY), + PRESET_ALIAS(LOSSLESS_DEFAULT, P4, NVENC_DEPRECATED_PRESET | NVENC_LOSSLESS), + PRESET_ALIAS(LOSSLESS_HP, P1, NVENC_DEPRECATED_PRESET | NVENC_LOSSLESS), +#else + PRESET(DEFAULT), + PRESET(HP), + PRESET(HQ), + PRESET(BD), + PRESET_ALIAS(SLOW, HQ, NVENC_TWO_PASSES), + PRESET_ALIAS(MEDIUM, HQ, NVENC_ONE_PASS), + PRESET_ALIAS(FAST, HP, NVENC_ONE_PASS), + PRESET(LOW_LATENCY_DEFAULT, NVENC_LOWLATENCY), + PRESET(LOW_LATENCY_HP, NVENC_LOWLATENCY), + PRESET(LOW_LATENCY_HQ, NVENC_LOWLATENCY), + PRESET(LOSSLESS_DEFAULT, NVENC_LOSSLESS), + PRESET(LOSSLESS_HP, NVENC_LOSSLESS), +#endif + }; + + GUIDTuple *t = &presets[ctx->preset]; + + ctx->init_encode_params.presetGUID = t->guid; + ctx->flags = t->flags; + +#ifdef NVENC_HAVE_NEW_PRESETS + if (ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS) + ctx->flags |= NVENC_LOSSLESS; +#endif +} + +#undef PRESET +#undef PRESET_ALIAS + static void nvenc_print_driver_requirement(AVCodecContext *avctx, int level) { -#if NVENCAPI_CHECK_VERSION(9, 2) +#if NVENCAPI_CHECK_VERSION(11, 1) const char *minver = "(unknown)"; +#elif NVENCAPI_CHECK_VERSION(11, 0) +# if defined(_WIN32) || defined(__CYGWIN__) + const char *minver = "456.71"; +# else + const char *minver = "455.28"; +# endif +#elif NVENCAPI_CHECK_VERSION(10, 0) +# if defined(_WIN32) || defined(__CYGWIN__) + const char *minver = "450.51"; +# else + const char *minver = "445.87"; +# endif #elif NVENCAPI_CHECK_VERSION(9, 1) # if defined(_WIN32) || defined(__CYGWIN__) const char *minver = "436.15"; @@ -343,7 +430,7 @@ static int nvenc_check_capabilities(AVCodecContext *avctx) } ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE); - if (ctx->preset >= PRESET_LOSSLESS_DEFAULT && ret <= 0) { + if (ctx->flags & NVENC_LOSSLESS && ret <= 0) { av_log(avctx, AV_LOG_WARNING, "Lossless encoding not supported\n"); return AVERROR(ENOSYS); } @@ -410,7 +497,7 @@ static int nvenc_check_capabilities(AVCodecContext *avctx) #ifdef NVENC_HAVE_BFRAME_REF_MODE ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE); - if (ctx->b_ref_mode == NV_ENC_BFRAME_REF_MODE_EACH && ret != 1) { + if (ctx->b_ref_mode == NV_ENC_BFRAME_REF_MODE_EACH && ret != 1 && ret != 3) { av_log(avctx, AV_LOG_WARNING, "Each B frame as reference is not supported\n"); return AVERROR(ENOSYS); } else if (ctx->b_ref_mode != NV_ENC_BFRAME_REF_MODE_DISABLED && ret == 0) { @@ -533,6 +620,11 @@ static av_cold int nvenc_setup_device(AVCodecContext *avctx) return AVERROR_BUG; } + nvenc_map_preset(ctx); + + if (ctx->flags & NVENC_DEPRECATED_PRESET) + av_log(avctx, AV_LOG_WARNING, "The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n"); + if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11 || avctx->hw_frames_ctx || avctx->hw_device_ctx) { AVHWFramesContext *frames_ctx; AVHWDeviceContext *hwdev_ctx; @@ -623,42 +715,6 @@ static av_cold int nvenc_setup_device(AVCodecContext *avctx) return 0; } -typedef struct GUIDTuple { - const GUID guid; - int flags; -} GUIDTuple; - -#define PRESET_ALIAS(alias, name, ...) \ - [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ } - -#define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__) - -static void nvenc_map_preset(NvencContext *ctx) -{ - GUIDTuple presets[] = { - PRESET(DEFAULT), - PRESET(HP), - PRESET(HQ), - PRESET(BD), - PRESET_ALIAS(SLOW, HQ, NVENC_TWO_PASSES), - PRESET_ALIAS(MEDIUM, HQ, NVENC_ONE_PASS), - PRESET_ALIAS(FAST, HP, NVENC_ONE_PASS), - PRESET(LOW_LATENCY_DEFAULT, NVENC_LOWLATENCY), - PRESET(LOW_LATENCY_HP, NVENC_LOWLATENCY), - PRESET(LOW_LATENCY_HQ, NVENC_LOWLATENCY), - PRESET(LOSSLESS_DEFAULT, NVENC_LOSSLESS), - PRESET(LOSSLESS_HP, NVENC_LOSSLESS), - }; - - GUIDTuple *t = &presets[ctx->preset]; - - ctx->init_encode_params.presetGUID = t->guid; - ctx->flags = t->flags; -} - -#undef PRESET -#undef PRESET_ALIAS - static av_cold void set_constqp(AVCodecContext *avctx) { NvencContext *ctx = avctx->priv_data; @@ -857,6 +913,24 @@ static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx) if (avctx->rc_max_rate > 0) ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate; +#ifdef NVENC_HAVE_MULTIPASS + ctx->encode_config.rcParams.multiPass = ctx->multipass; + + if (ctx->flags & NVENC_ONE_PASS) + ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED; + if (ctx->flags & NVENC_TWO_PASSES || ctx->twopass > 0) + ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION; + + if (ctx->rc < 0) { + if (ctx->cbr) { + ctx->rc = NV_ENC_PARAMS_RC_CBR; + } else if (ctx->cqp >= 0) { + ctx->rc = NV_ENC_PARAMS_RC_CONSTQP; + } else if (ctx->quality >= 0.0f) { + ctx->rc = NV_ENC_PARAMS_RC_VBR; + } + } +#else if (ctx->rc < 0) { if (ctx->flags & NVENC_ONE_PASS) ctx->twopass = 0; @@ -880,17 +954,20 @@ static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx) ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP; } } +#endif if (ctx->rc >= 0 && ctx->rc & RC_MODE_DEPRECATED) { av_log(avctx, AV_LOG_WARNING, "Specified rc mode is deprecated.\n"); - av_log(avctx, AV_LOG_WARNING, "\tll_2pass_quality -> cbr_ld_hq\n"); - av_log(avctx, AV_LOG_WARNING, "\tll_2pass_size -> cbr_hq\n"); - av_log(avctx, AV_LOG_WARNING, "\tvbr_2pass -> vbr_hq\n"); - av_log(avctx, AV_LOG_WARNING, "\tvbr_minqp -> (no replacement)\n"); + av_log(avctx, AV_LOG_WARNING, "Use -rc constqp/cbr/vbr, -tune and -multipass instead.\n"); ctx->rc &= ~RC_MODE_DEPRECATED; } +#ifdef NVENC_HAVE_LDKFS + if (ctx->ldkfs) + ctx->encode_config.rcParams.lowDelayKeyFrameScale = ctx->ldkfs; +#endif + if (ctx->flags & NVENC_LOSSLESS) { set_lossless(avctx); } else if (ctx->rc >= 0) { @@ -956,9 +1033,9 @@ static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx) av_log(avctx, AV_LOG_VERBOSE, "CQ(%d) mode enabled.\n", tmp_quality); - //CQ mode shall discard max & avg bitrate; - avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate = 0; - avctx->rc_max_rate = ctx->encode_config.rcParams.maxBitRate = 0; + //CQ mode shall discard avg bitrate & honor max bitrate; + ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate = 0; + ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate; } } @@ -969,14 +1046,14 @@ static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx) NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config; NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters; - vui->colourMatrix = avctx->colorspace; + vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace; vui->colourPrimaries = avctx->color_primaries; vui->transferCharacteristics = avctx->color_trc; vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); vui->colourDescriptionPresentFlag = - (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2); + (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2); vui->videoSignalTypePresentFlag = (vui->colourDescriptionPresentFlag @@ -1035,7 +1112,7 @@ static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx) } // force setting profile as high444p if input is AV_PIX_FMT_YUV444P - if (ctx->data_pix_fmt == AV_PIX_FMT_YUV444P) { + if (IS_YUV444(ctx->data_pix_fmt)) { cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID; avctx->profile = FF_PROFILE_H264_HIGH_444_PREDICTIVE; } @@ -1066,14 +1143,14 @@ static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx) NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig; NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters; - vui->colourMatrix = avctx->colorspace; + vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace; vui->colourPrimaries = avctx->color_primaries; vui->transferCharacteristics = avctx->color_trc; vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); vui->colourDescriptionPresentFlag = - (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2); + (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2); vui->videoSignalTypePresentFlag = (vui->colourDescriptionPresentFlag @@ -1195,15 +1272,28 @@ static av_cold int nvenc_setup_encoder(AVCodecContext *avctx) ctx->init_encode_params.encodeConfig = &ctx->encode_config; - nvenc_map_preset(ctx); - preset_config.version = NV_ENC_PRESET_CONFIG_VER; preset_config.presetCfg.version = NV_ENC_CONFIG_VER; +#ifdef NVENC_HAVE_NEW_PRESETS + ctx->init_encode_params.tuningInfo = ctx->tuning_info; + + if (ctx->flags & NVENC_LOSSLESS) + ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS; + else if (ctx->flags & NVENC_LOWLATENCY) + ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY; + + nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(ctx->nvencoder, + ctx->init_encode_params.encodeGUID, + ctx->init_encode_params.presetGUID, + ctx->init_encode_params.tuningInfo, + &preset_config); +#else nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, - ctx->init_encode_params.encodeGUID, - ctx->init_encode_params.presetGUID, - &preset_config); + ctx->init_encode_params.encodeGUID, + ctx->init_encode_params.presetGUID, + &preset_config); +#endif if (nv_status != NV_ENC_SUCCESS) return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration"); @@ -1226,6 +1316,14 @@ static av_cold int nvenc_setup_encoder(AVCodecContext *avctx) ctx->init_encode_params.enableEncodeAsync = 0; ctx->init_encode_params.enablePTD = 1; +#ifdef NVENC_HAVE_NEW_PRESETS + /* If lookahead isn't set from CLI, use value from preset. + * P6 & P7 presets may enable lookahead for better quality. + * */ + if (ctx->rc_lookahead == 0 && ctx->encode_config.rcParams.enableLookahead) + ctx->rc_lookahead = ctx->encode_config.rcParams.lookaheadDepth; +#endif + if (ctx->weighted_pred == 1) ctx->init_encode_params.enableWeightedPrediction = 1; @@ -1320,8 +1418,10 @@ static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt) case AV_PIX_FMT_P010: case AV_PIX_FMT_P016: return NV_ENC_BUFFER_FORMAT_YUV420_10BIT; + case AV_PIX_FMT_GBRP: case AV_PIX_FMT_YUV444P: return NV_ENC_BUFFER_FORMAT_YUV444_PL; + case AV_PIX_FMT_GBRP16: case AV_PIX_FMT_YUV444P16: return NV_ENC_BUFFER_FORMAT_YUV444_10BIT; case AV_PIX_FMT_0RGB32: @@ -1383,7 +1483,6 @@ static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx) } ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer; - ctx->surfaces[idx].size = allocOut.size; av_fifo_generic_write(ctx->unused_surface_queue, &tmp_surface, sizeof(tmp_surface), NULL); @@ -1509,6 +1608,8 @@ av_cold int ff_nvenc_encode_close(AVCodecContext *avctx) av_freep(&ctx->surfaces); ctx->nb_surfaces = 0; + av_frame_free(&ctx->frame); + if (ctx->nvencoder) { p_nvenc->nvEncDestroyEncoder(ctx->nvencoder); @@ -1562,6 +1663,10 @@ av_cold int ff_nvenc_encode_init(AVCodecContext *avctx) ctx->data_pix_fmt = avctx->pix_fmt; } + ctx->frame = av_frame_alloc(); + if (!ctx->frame) + return AVERROR(ENOMEM); + if ((ret = nvenc_load_libraries(avctx)) < 0) return ret; @@ -1776,7 +1881,8 @@ static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame, static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *params, - NV_ENC_SEI_PAYLOAD *sei_data) + NV_ENC_SEI_PAYLOAD *sei_data, + int sei_count) { NvencContext *ctx = avctx->priv_data; @@ -1786,9 +1892,9 @@ static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, ctx->encode_config.encodeCodecConfig.h264Config.sliceMode; params->codecPicParams.h264PicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData; - if (sei_data) { + if (sei_count > 0) { params->codecPicParams.h264PicParams.seiPayloadArray = sei_data; - params->codecPicParams.h264PicParams.seiPayloadArrayCnt = 1; + params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count; } break; @@ -1797,9 +1903,9 @@ static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode; params->codecPicParams.hevcPicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData; - if (sei_data) { + if (sei_count > 0) { params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data; - params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = 1; + params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count; } break; @@ -1829,7 +1935,7 @@ static int nvenc_set_timestamp(AVCodecContext *avctx, pkt->pts = params->outputTimeStamp; pkt->dts = timestamp_queue_dequeue(ctx->timestamp_list); - pkt->dts -= FFMAX(avctx->max_b_frames, 0) * FFMIN(avctx->ticks_per_frame, 1); + pkt->dts -= FFMAX(ctx->encode_config.frameIntervalP - 1, 0) * FFMAX(avctx->ticks_per_frame, 1); return 0; } @@ -1879,9 +1985,7 @@ static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSur goto error; } - res = pkt->data ? - ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes, lock_params.bitstreamSizeInBytes) : - av_new_packet(pkt, lock_params.bitstreamSizeInBytes); + res = ff_get_encode_buffer(avctx, pkt, lock_params.bitstreamSizeInBytes, 0); if (res < 0) { p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface); @@ -2067,13 +2171,14 @@ static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame) } } -int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) +static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) { NVENCSTATUS nv_status; NvencSurface *tmp_out_surf, *in_surf; int res, res2; - NV_ENC_SEI_PAYLOAD *sei_data = NULL; - size_t sei_size; + NV_ENC_SEI_PAYLOAD sei_data[8]; + int sei_count = 0; + int i; NvencContext *ctx = avctx->priv_data; NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; @@ -2085,15 +2190,7 @@ int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder) return AVERROR(EINVAL); - if (ctx->encoder_flushing) { - if (avctx->internal->draining) - return AVERROR_EOF; - - ctx->encoder_flushing = 0; - av_fifo_reset(ctx->timestamp_list); - } - - if (frame) { + if (frame && frame->buf[0]) { in_surf = get_free_frame(ctx); if (!in_surf) return AVERROR(EAGAIN); @@ -2139,21 +2236,40 @@ int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) pic_params.inputTimeStamp = frame->pts; if (ctx->a53_cc && av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC)) { - if (ff_alloc_a53_sei(frame, sizeof(NV_ENC_SEI_PAYLOAD), (void**)&sei_data, &sei_size) < 0) { + void *a53_data = NULL; + size_t a53_size = 0; + + if (ff_alloc_a53_sei(frame, 0, (void**)&a53_data, &a53_size) < 0) { av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n"); } - if (sei_data) { - sei_data->payloadSize = (uint32_t)sei_size; - sei_data->payloadType = 4; - sei_data->payload = (uint8_t*)(sei_data + 1); + if (a53_data) { + sei_data[sei_count].payloadSize = (uint32_t)a53_size; + sei_data[sei_count].payloadType = 4; + sei_data[sei_count].payload = (uint8_t*)a53_data; + sei_count ++; + } + } + + if (ctx->s12m_tc && av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE)) { + void *tc_data = NULL; + size_t tc_size = 0; + + if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, (void**)&tc_data, &tc_size) < 0) { + av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n"); + } + + if (tc_data) { + sei_data[sei_count].payloadSize = (uint32_t)tc_size; + sei_data[sei_count].payloadType = SEI_TYPE_TIME_CODE; + sei_data[sei_count].payload = (uint8_t*)tc_data; + sei_count ++; } } - nvenc_codec_specific_pic_params(avctx, &pic_params, sei_data); + nvenc_codec_specific_pic_params(avctx, &pic_params, sei_data, sei_count); } else { pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS; - ctx->encoder_flushing = 1; } res = nvenc_push_context(avctx); @@ -2161,7 +2277,9 @@ int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) return res; nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params); - av_free(sei_data); + + for ( i = 0; i < sei_count; i++) + av_freep(&sei_data[i].payload); res = nvenc_pop_context(avctx); if (res < 0) @@ -2171,7 +2289,7 @@ int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) nv_status != NV_ENC_ERR_NEED_MORE_INPUT) return nvenc_print_error(avctx, nv_status, "EncodePicture failed!"); - if (frame) { + if (frame && frame->buf[0]) { av_fifo_generic_write(ctx->output_surface_queue, &in_surf, sizeof(in_surf), NULL); timestamp_queue_enqueue(ctx->timestamp_list, frame->pts); } @@ -2194,10 +2312,25 @@ int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) NvencContext *ctx = avctx->priv_data; + AVFrame *frame = ctx->frame; + if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder) return AVERROR(EINVAL); - if (output_ready(avctx, ctx->encoder_flushing)) { + if (!frame->buf[0]) { + res = ff_encode_get_frame(avctx, frame); + if (res < 0 && res != AVERROR_EOF) + return res; + } + + res = nvenc_send_frame(avctx, frame); + if (res < 0) { + if (res != AVERROR(EAGAIN)) + return res; + } else + av_frame_unref(frame); + + if (output_ready(avctx, avctx->internal->draining)) { av_fifo_generic_read(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); res = nvenc_push_context(avctx); @@ -2214,7 +2347,7 @@ int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) return res; av_fifo_generic_write(ctx->unused_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); - } else if (ctx->encoder_flushing) { + } else if (avctx->internal->draining) { return AVERROR_EOF; } else { return AVERROR(EAGAIN); @@ -2223,31 +2356,10 @@ int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) return 0; } -int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, - const AVFrame *frame, int *got_packet) +av_cold void ff_nvenc_encode_flush(AVCodecContext *avctx) { NvencContext *ctx = avctx->priv_data; - int res; - - if (!ctx->encoder_flushing) { - res = ff_nvenc_send_frame(avctx, frame); - if (res < 0) - return res; - } - res = ff_nvenc_receive_packet(avctx, pkt); - if (res == AVERROR(EAGAIN) || res == AVERROR_EOF) { - *got_packet = 0; - } else if (res < 0) { - return res; - } else { - *got_packet = 1; - } - - return 0; -} - -av_cold void ff_nvenc_encode_flush(AVCodecContext *avctx) -{ - ff_nvenc_send_frame(avctx, NULL); + nvenc_send_frame(avctx, NULL); + av_fifo_reset(ctx->timestamp_list); }