static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame,

                                      NvencSurface *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;



    if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {

        int reg_idx = nvenc_register_frame(avctx, frame);

        if (reg_idx < 0) {

            av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n");

            return reg_idx;

        }



        res = av_frame_ref(nvenc_frame->in_ref, frame);

        if (res < 0)

            return res;



        nvenc_frame->in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;

        nvenc_frame->in_map.registeredResource = ctx->registered_frames[reg_idx].regptr;

        nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &nvenc_frame->in_map);

        if (nv_status != NV_ENC_SUCCESS) {

            av_frame_unref(nvenc_frame->in_ref);

            return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");

        }



        ctx->registered_frames[reg_idx].mapped = 1;

        nvenc_frame->reg_idx                   = reg_idx;

        nvenc_frame->input_surface             = nvenc_frame->in_map.mappedResource;

        nvenc_frame->format                    = nvenc_frame->in_map.mappedBufferFmt;

        nvenc_frame->pitch                     = frame->linesize[0];

        return 0;

    } else {

        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) {

            return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer");

        }



        nvenc_frame->pitch = lockBufferParams.pitch;

        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) {

            return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!");

        }



        return res;

    }

}

static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame,

                                      NvencSurface *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;



    if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {

        int reg_idx = nvenc_register_frame(avctx, frame);

        if (reg_idx < 0) {

            av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n");

            return reg_idx;

        }



        res = av_frame_ref(nvenc_frame->in_ref, frame);

        if (res < 0)

            return res;



        nvenc_frame->in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;

        nvenc_frame->in_map.registeredResource = ctx->registered_frames[reg_idx].regptr;

        nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &nvenc_frame->in_map);

        if (nv_status != NV_ENC_SUCCESS) {

            av_frame_unref(nvenc_frame->in_ref);

            return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");

        }



        ctx->registered_frames[reg_idx].mapped = 1;

        nvenc_frame->reg_idx                   = reg_idx;

        nvenc_frame->input_surface             = nvenc_frame->in_map.mappedResource;

        nvenc_frame->format                    = nvenc_frame->in_map.mappedBufferFmt;

        nvenc_frame->pitch                     = frame->linesize[0];

        return 0;

    } else {

        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) {

            return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer");

        }



        nvenc_frame->pitch = lockBufferParams.pitch;

        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) {

            return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!");

        }



        return res;

    }

}

        nvenc_frame->in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;         nvenc_frame->in_map.registeredResource = ctx->registered_frames[reg_idx].regptr;         nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &nvenc_frame->in_map);         if (nv_status != NV_ENC_SUCCESS) {             av_frame_unref(nvenc_frame->in_ref);             return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");         ctx->registered_frames[reg_idx].mapped = 1;         nvenc_frame->input_surface             = nvenc_frame->in_map.mappedResource;         nvenc_frame->format                    = nvenc_frame->in_map.mappedBufferFmt;
static int FUNC_0(AVCodecContext *VAR_0, const AVFrame *VAR_1,

                                      NvencSurface *VAR_2)

{

    NvencContext *ctx = VAR_0->priv_data;

    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;

    NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;



    int VAR_3;

    NVENCSTATUS nv_status;



    if (VAR_0->pix_fmt == AV_PIX_FMT_CUDA || VAR_0->pix_fmt == AV_PIX_FMT_D3D11) {

        int VAR_4 = nvenc_register_frame(VAR_0, VAR_1);

        if (VAR_4 < 0) {

            av_log(VAR_0, AV_LOG_ERROR, "Could not register an input HW VAR_1\n");

            return VAR_4;

        }



        VAR_3 = av_frame_ref(VAR_2->in_ref, VAR_1);

        if (VAR_3 < 0)

            return VAR_3;



        VAR_2->in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;

        VAR_2->in_map.registeredResource = ctx->registered_frames[VAR_4].regptr;

        nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &VAR_2->in_map);

        if (nv_status != NV_ENC_SUCCESS) {

            av_frame_unref(VAR_2->in_ref);

            return nvenc_print_error(VAR_0, nv_status, "Error mapping an input resource");

        }



        ctx->registered_frames[VAR_4].mapped = 1;

        VAR_2->VAR_4                   = VAR_4;

        VAR_2->input_surface             = VAR_2->in_map.mappedResource;

        VAR_2->format                    = VAR_2->in_map.mappedBufferFmt;

        VAR_2->pitch                     = VAR_1->linesize[0];

        return 0;

    } else {

        NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };



        lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;

        lockBufferParams.inputBuffer = VAR_2->input_surface;



        nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);

        if (nv_status != NV_ENC_SUCCESS) {

            return nvenc_print_error(VAR_0, nv_status, "Failed locking nvenc input buffer");

        }



        VAR_2->pitch = lockBufferParams.pitch;

        VAR_3 = nvenc_copy_frame(VAR_0, VAR_2, &lockBufferParams, VAR_1);



        nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, VAR_2->input_surface);

        if (nv_status != NV_ENC_SUCCESS) {

            return nvenc_print_error(VAR_0, nv_status, "Failed unlocking input buffer!");

        }



        return VAR_3;

    }

}

static int FUNC_0(AVCodecContext *VAR_0, const AVFrame *VAR_1,
NvencSurface *VAR_2)
{ NvencContext *ctx = VAR_0->priv_data; NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; int VAR_3; NVENCSTATUS nv_status; if (VAR_0->pix_fmt == AV_PIX_FMT_CUDA || VAR_0->pix_fmt == AV_PIX_FMT_D3D11) { int VAR_4 = nvenc_register_frame(VAR_0, VAR_1); if (VAR_4 < 0) { av_log(VAR_0, AV_LOG_ERROR, "Could not register an input HW VAR_1\n"); return VAR_4; } VAR_3 = av_frame_ref(VAR_2->in_ref, VAR_1); if (VAR_3 < 0)
return VAR_3; VAR_2->in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER; VAR_2->in_map.registeredResource = ctx->registered_frames[VAR_4].regptr; nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &VAR_2->in_map); if (nv_status != NV_ENC_SUCCESS) { av_frame_unref(VAR_2->in_ref); return nvenc_print_error(VAR_0, nv_status, "Error mapping an input resource"); } ctx->registered_frames[VAR_4].mapped = 1; VAR_2->VAR_4                   = VAR_4; VAR_2->input_surface             = VAR_2->in_map.mappedResource; VAR_2->format                    = VAR_2->in_map.mappedBufferFmt; VAR_2->pitch                     = VAR_1->linesize[0]; return 0; } else { NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 }; lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER; lockBufferParams.inputBuffer = VAR_2->input_surface; nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams); if (nv_status != NV_ENC_SUCCESS) { return nvenc_print_error(VAR_0, nv_status, "Failed locking nvenc input buffer"); } VAR_2->pitch = lockBufferParams.pitch; VAR_3 = nvenc_copy_frame(VAR_0, VAR_2, &lockBufferParams, VAR_1); nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, VAR_2->input_surface); if (nv_status != NV_ENC_SUCCESS) { return nvenc_print_error(VAR_0, nv_status, "Failed unlocking input buffer!"); } return VAR_3; } }