1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avcodec/nvenc: use persistent sei data buffer

Signed-off-by: Brad Hards <bradh@frogmouth.net>
Signed-off-by: Timo Rothenpieler <timo@rothenpieler.org>
This commit is contained in:
Brad Hards 2021-05-25 20:11:57 +10:00 committed by Timo Rothenpieler
parent 2fac1e370c
commit 63948a6170
2 changed files with 44 additions and 14 deletions

View File

@ -1610,6 +1610,8 @@ av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
av_frame_free(&ctx->frame);
av_freep(&ctx->sei_data);
if (ctx->nvencoder) {
p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
@ -2170,7 +2172,6 @@ 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[8];
int sei_count = 0;
int i;
@ -2233,15 +2234,25 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
void *a53_data = NULL;
size_t a53_size = 0;
if (ff_alloc_a53_sei(frame, 0, (void**)&a53_data, &a53_size) < 0) {
if (ff_alloc_a53_sei(frame, 0, &a53_data, &a53_size) < 0) {
av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
}
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 ++;
void *tmp = av_fast_realloc(ctx->sei_data,
&ctx->sei_data_size,
(sei_count + 1) * sizeof(*ctx->sei_data));
if (!tmp) {
av_free(a53_data);
res = AVERROR(ENOMEM);
goto sei_failed;
} else {
ctx->sei_data = tmp;
ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
ctx->sei_data[sei_count].payloadType = 4;
ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
sei_count++;
}
}
}
@ -2249,19 +2260,29 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
void *tc_data = NULL;
size_t tc_size = 0;
if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, (void**)&tc_data, &tc_size) < 0) {
if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, &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 ++;
void *tmp = av_fast_realloc(ctx->sei_data,
&ctx->sei_data_size,
(sei_count + 1) * sizeof(*ctx->sei_data));
if (!tmp) {
av_free(tc_data);
res = AVERROR(ENOMEM);
goto sei_failed;
} else {
ctx->sei_data = tmp;
ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
ctx->sei_data[sei_count].payloadType = SEI_TYPE_TIME_CODE;
ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
sei_count++;
}
}
}
nvenc_codec_specific_pic_params(avctx, &pic_params, sei_data, sei_count);
nvenc_codec_specific_pic_params(avctx, &pic_params, ctx->sei_data, sei_count);
} else {
pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
}
@ -2272,8 +2293,8 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
for ( i = 0; i < sei_count; i++)
av_freep(&sei_data[i].payload);
for (i = 0; i < sei_count; i++)
av_freep(&(ctx->sei_data[i].payload));
res = nvenc_pop_context(avctx);
if (res < 0)
@ -2297,6 +2318,12 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
}
return 0;
sei_failed:
for (i = 0; i < sei_count; i++)
av_freep(&(ctx->sei_data[i].payload));
return res;
}
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)

View File

@ -166,6 +166,9 @@ typedef struct NvencContext
AVFifoBuffer *output_surface_ready_queue;
AVFifoBuffer *timestamp_list;
NV_ENC_SEI_PAYLOAD *sei_data;
int sei_data_size;
struct {
void *ptr;
int ptr_index;