static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)

{

    NvencContext *ctx = avctx->priv_data;

    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;

    NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;



    uint32_t slice_mode_data;

    uint32_t *slice_offsets;

    NV_ENC_LOCK_BITSTREAM lock_params = { 0 };

    NVENCSTATUS nv_status;

    int res = 0;



    enum AVPictureType pict_type;



    switch (avctx->codec->id) {

    case AV_CODEC_ID_H264:

      slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;

      break;

    case AV_CODEC_ID_H265:

      slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;

      break;

    default:

      av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");

      res = AVERROR(EINVAL);

      goto error;

    }

    slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));



    if (!slice_offsets)

        goto error;



    lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;



    lock_params.doNotWait = 0;

    lock_params.outputBitstream = tmpoutsurf->output_surface;

    lock_params.sliceOffsets = slice_offsets;



    nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);

    if (nv_status != NV_ENC_SUCCESS) {

        res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer");

        goto error;

    }



    if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes,0)) {

        p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);

        goto error;

    }



    memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);



    nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);

    if (nv_status != NV_ENC_SUCCESS)

        nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");





    if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {

        p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, tmpoutsurf->in_map.mappedResource);

        av_frame_unref(tmpoutsurf->in_ref);

        ctx->registered_frames[tmpoutsurf->reg_idx].mapped = 0;



        tmpoutsurf->input_surface = NULL;

    }



    switch (lock_params.pictureType) {

    case NV_ENC_PIC_TYPE_IDR:

        pkt->flags |= AV_PKT_FLAG_KEY;

    case NV_ENC_PIC_TYPE_I:

        pict_type = AV_PICTURE_TYPE_I;

        break;

    case NV_ENC_PIC_TYPE_P:

        pict_type = AV_PICTURE_TYPE_P;

        break;

    case NV_ENC_PIC_TYPE_B:

        pict_type = AV_PICTURE_TYPE_B;

        break;

    case NV_ENC_PIC_TYPE_BI:

        pict_type = AV_PICTURE_TYPE_BI;

        break;

    default:

        av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");

        av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");

        res = AVERROR_EXTERNAL;

        goto error;

    }



#if FF_API_CODED_FRAME

FF_DISABLE_DEPRECATION_WARNINGS

    avctx->coded_frame->pict_type = pict_type;

FF_ENABLE_DEPRECATION_WARNINGS

#endif



    ff_side_data_set_encoder_stats(pkt,

        (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type);



    res = nvenc_set_timestamp(avctx, &lock_params, pkt);

    if (res < 0)

        goto error2;



    av_free(slice_offsets);



    return 0;



error:

    timestamp_queue_dequeue(ctx->timestamp_list);



error2:

    av_free(slice_offsets);



    return res;

}

static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)

{

    NvencContext *ctx = avctx->priv_data;

    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;

    NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;



    uint32_t slice_mode_data;

    uint32_t *slice_offsets;

    NV_ENC_LOCK_BITSTREAM lock_params = { 0 };

    NVENCSTATUS nv_status;

    int res = 0;



    enum AVPictureType pict_type;



    switch (avctx->codec->id) {

    case AV_CODEC_ID_H264:

      slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;

      break;

    case AV_CODEC_ID_H265:

      slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;

      break;

    default:

      av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");

      res = AVERROR(EINVAL);

      goto error;

    }

    slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));



    if (!slice_offsets)

        goto error;



    lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;



    lock_params.doNotWait = 0;

    lock_params.outputBitstream = tmpoutsurf->output_surface;

    lock_params.sliceOffsets = slice_offsets;



    nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);

    if (nv_status != NV_ENC_SUCCESS) {

        res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer");

        goto error;

    }



    if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes,0)) {

        p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);

        goto error;

    }



    memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);



    nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);

    if (nv_status != NV_ENC_SUCCESS)

        nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");





    if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {

        p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, tmpoutsurf->in_map.mappedResource);

        av_frame_unref(tmpoutsurf->in_ref);

        ctx->registered_frames[tmpoutsurf->reg_idx].mapped = 0;



        tmpoutsurf->input_surface = NULL;

    }



    switch (lock_params.pictureType) {

    case NV_ENC_PIC_TYPE_IDR:

        pkt->flags |= AV_PKT_FLAG_KEY;

    case NV_ENC_PIC_TYPE_I:

        pict_type = AV_PICTURE_TYPE_I;

        break;

    case NV_ENC_PIC_TYPE_P:

        pict_type = AV_PICTURE_TYPE_P;

        break;

    case NV_ENC_PIC_TYPE_B:

        pict_type = AV_PICTURE_TYPE_B;

        break;

    case NV_ENC_PIC_TYPE_BI:

        pict_type = AV_PICTURE_TYPE_BI;

        break;

    default:

        av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");

        av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");

        res = AVERROR_EXTERNAL;

        goto error;

    }



#if FF_API_CODED_FRAME

FF_DISABLE_DEPRECATION_WARNINGS

    avctx->coded_frame->pict_type = pict_type;

FF_ENABLE_DEPRECATION_WARNINGS

#endif



    ff_side_data_set_encoder_stats(pkt,

        (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type);



    res = nvenc_set_timestamp(avctx, &lock_params, pkt);

    if (res < 0)

        goto error2;



    av_free(slice_offsets);



    return 0;



error:

    timestamp_queue_dequeue(ctx->timestamp_list);



error2:

    av_free(slice_offsets);



    return res;

}

    uint32_t *slice_offsets;
static int FUNC_0(AVCodecContext *VAR_0, AVPacket *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;



    uint32_t slice_mode_data;

    uint32_t *slice_offsets;

    NV_ENC_LOCK_BITSTREAM lock_params = { 0 };

    NVENCSTATUS nv_status;

    int VAR_3 = 0;



    enum AVPictureType VAR_4;



    switch (VAR_0->codec->id) {

    case AV_CODEC_ID_H264:

      slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;

      break;

    case AV_CODEC_ID_H265:

      slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;

      break;

    default:

      av_log(VAR_0, AV_LOG_ERROR, "Unknown codec name\n");

      VAR_3 = AVERROR(EINVAL);

      goto error;

    }

    slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));



    if (!slice_offsets)

        goto error;



    lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;



    lock_params.doNotWait = 0;

    lock_params.outputBitstream = VAR_2->output_surface;

    lock_params.sliceOffsets = slice_offsets;



    nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);

    if (nv_status != NV_ENC_SUCCESS) {

        VAR_3 = nvenc_print_error(VAR_0, nv_status, "Failed locking bitstream buffer");

        goto error;

    }



    if (VAR_3 = ff_alloc_packet2(VAR_0, VAR_1, lock_params.bitstreamSizeInBytes,0)) {

        p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, VAR_2->output_surface);

        goto error;

    }



    memcpy(VAR_1->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);



    nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, VAR_2->output_surface);

    if (nv_status != NV_ENC_SUCCESS)

        nvenc_print_error(VAR_0, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");





    if (VAR_0->pix_fmt == AV_PIX_FMT_CUDA) {

        p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, VAR_2->in_map.mappedResource);

        av_frame_unref(VAR_2->in_ref);

        ctx->registered_frames[VAR_2->reg_idx].mapped = 0;



        VAR_2->input_surface = NULL;

    }



    switch (lock_params.pictureType) {

    case NV_ENC_PIC_TYPE_IDR:

        VAR_1->flags |= AV_PKT_FLAG_KEY;

    case NV_ENC_PIC_TYPE_I:

        VAR_4 = AV_PICTURE_TYPE_I;

        break;

    case NV_ENC_PIC_TYPE_P:

        VAR_4 = AV_PICTURE_TYPE_P;

        break;

    case NV_ENC_PIC_TYPE_B:

        VAR_4 = AV_PICTURE_TYPE_B;

        break;

    case NV_ENC_PIC_TYPE_BI:

        VAR_4 = AV_PICTURE_TYPE_BI;

        break;

    default:

        av_log(VAR_0, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");

        av_log(VAR_0, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");

        VAR_3 = AVERROR_EXTERNAL;

        goto error;

    }



#if FF_API_CODED_FRAME

FF_DISABLE_DEPRECATION_WARNINGS

    VAR_0->coded_frame->VAR_4 = VAR_4;

FF_ENABLE_DEPRECATION_WARNINGS

#endif



    ff_side_data_set_encoder_stats(VAR_1,

        (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, VAR_4);



    VAR_3 = nvenc_set_timestamp(VAR_0, &lock_params, VAR_1);

    if (VAR_3 < 0)

        goto error2;



    av_free(slice_offsets);



    return 0;



error:

    timestamp_queue_dequeue(ctx->timestamp_list);



error2:

    av_free(slice_offsets);



    return VAR_3;

}

static int FUNC_0(AVCodecContext *VAR_0, AVPacket *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; uint32_t slice_mode_data; uint32_t *slice_offsets; NV_ENC_LOCK_BITSTREAM lock_params = { 0 }; NVENCSTATUS nv_status; int VAR_3 = 0; enum AVPictureType VAR_4; switch (VAR_0->codec->id) { case AV_CODEC_ID_H264:
slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData; break; case AV_CODEC_ID_H265:
slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData; break; default:
av_log(VAR_0, AV_LOG_ERROR, "Unknown codec name\n"); VAR_3 = AVERROR(EINVAL); goto error; } slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets)); if (!slice_offsets)
goto error; lock_params.version = NV_ENC_LOCK_BITSTREAM_VER; lock_params.doNotWait = 0; lock_params.outputBitstream = VAR_2->output_surface; lock_params.sliceOffsets = slice_offsets; nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params); if (nv_status != NV_ENC_SUCCESS) { VAR_3 = nvenc_print_error(VAR_0, nv_status, "Failed locking bitstream buffer"); goto error; } if (VAR_3 = ff_alloc_packet2(VAR_0, VAR_1, lock_params.bitstreamSizeInBytes,0)) { p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, VAR_2->output_surface); goto error; } memcpy(VAR_1->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes); nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, VAR_2->output_surface); if (nv_status != NV_ENC_SUCCESS)
nvenc_print_error(VAR_0, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open"); if (VAR_0->pix_fmt == AV_PIX_FMT_CUDA) { p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, VAR_2->in_map.mappedResource); av_frame_unref(VAR_2->in_ref); ctx->registered_frames[VAR_2->reg_idx].mapped = 0; VAR_2->input_surface = NULL; } switch (lock_params.pictureType) { case NV_ENC_PIC_TYPE_IDR:
VAR_1->flags |= AV_PKT_FLAG_KEY; case NV_ENC_PIC_TYPE_I:
VAR_4 = AV_PICTURE_TYPE_I; break; case NV_ENC_PIC_TYPE_P:
VAR_4 = AV_PICTURE_TYPE_P; break; case NV_ENC_PIC_TYPE_B:
VAR_4 = AV_PICTURE_TYPE_B; break; case NV_ENC_PIC_TYPE_BI:
VAR_4 = AV_PICTURE_TYPE_BI; break; default:
av_log(VAR_0, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n"); av_log(VAR_0, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n"); VAR_3 = AVERROR_EXTERNAL; goto error; } #if FF_API_CODED_FRAME
FF_DISABLE_DEPRECATION_WARNINGS
VAR_0->coded_frame->VAR_4 = VAR_4; FF_ENABLE_DEPRECATION_WARNINGS
#endif
ff_side_data_set_encoder_stats(VAR_1,
(lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, VAR_4); VAR_3 = nvenc_set_timestamp(VAR_0, &lock_params, VAR_1); if (VAR_3 < 0)
goto error2; av_free(slice_offsets); return 0; error:
timestamp_queue_dequeue(ctx->timestamp_list); error2:
av_free(slice_offsets); return VAR_3; }