History

avcodec/nvenc: handle frame durations and AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE

Timo Rothenpieler committed 3 years ago

Commit 16fdb481 parent 6c418ae5 files · +104 −3Browse files

Unified · Split

5 files
  1. libavcodec/nvenc.c +81 −0
  2. libavcodec/nvenc.h +17 −0
  3. libavcodec/nvenc_av1.c +2 −1
  4. libavcodec/nvenc_h264.c +2 −1
  5. libavcodec/nvenc_hevc.c +2 −1
libavcodec/nvenc.c+81−0
@@ -971,6 +971,10 @@ static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx) · 970 unmodified lines
971971 ctx->nb_surfaces = FFMAX(1, FFMIN(MAX_REGISTERED_FRAMES, ctx->nb_surfaces));
972972 ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1);
973973
974+ // Output in the worst case will only start when the surface buffer is completely full.
975+ // Hence we need to keep at least the max amount of surfaces plus the max reorder delay around.
976+ ctx->frame_data_array_nb = ctx->nb_surfaces + ctx->encode_config.frameIntervalP - 1;
977+
974978 return 0;
975979 }
976980
@@ -1767,6 +1771,10 @@ static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx) · 790 unmodified lines
17671771 if (!ctx->surfaces)
17681772 return AVERROR(ENOMEM);
17691773
1774+ ctx->frame_data_array = av_calloc(ctx->frame_data_array_nb, sizeof(*ctx->frame_data_array));
1775+ if (!ctx->frame_data_array)
1776+ return AVERROR(ENOMEM);
1777+
17701778 ctx->timestamp_list = av_fifo_alloc2(ctx->nb_surfaces, sizeof(int64_t), 0);
17711779 if (!ctx->timestamp_list)
17721780 return AVERROR(ENOMEM);
@@ -1857,6 +1865,12 @@ av_cold int ff_nvenc_encode_close(AVCodecContext *avctx) · 84 unmodified lines
18571865 av_fifo_freep2(&ctx->output_surface_queue);
18581866 av_fifo_freep2(&ctx->unused_surface_queue);
18591867
1868+ if (ctx->frame_data_array) {
1869+ for (i = 0; i < ctx->nb_surfaces; i++)
1870+ av_buffer_unref(&ctx->frame_data_array[i].frame_opaque_ref);
1871+ av_freep(&ctx->frame_data_array);
1872+ }
1873+
18601874 if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) {
18611875 for (i = 0; i < ctx->nb_registered_frames; i++) {
18621876 if (ctx->registered_frames[i].mapped)
@@ -2233,6 +2247,65 @@ FF_ENABLE_DEPRECATION_WARNINGS · 370 unmodified lines
22332247 return 0;
22342248 }
22352249
2250+static int nvenc_store_frame_data(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *pic_params, const AVFrame *frame)
2251+{
2252+ NvencContext *ctx = avctx->priv_data;
2253+ int res = 0;
2254+
2255+ int idx = ctx->frame_data_array_pos;
2256+ NvencFrameData *frame_data = &ctx->frame_data_array[idx];
2257+
2258+ // in case the encoder got reconfigured, there might be leftovers
2259+ av_buffer_unref(&frame_data->frame_opaque_ref);
2260+
2261+ if (frame && frame->opaque_ref && avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
2262+ frame_data->frame_opaque_ref = av_buffer_ref(frame->opaque_ref);
2263+ if (!frame_data->frame_opaque_ref)
2264+ return AVERROR(ENOMEM);
2265+ }
2266+
2267+ frame_data->duration = frame->duration;
2268+ frame_data->frame_opaque = frame->opaque;
2269+
2270+#if FF_API_REORDERED_OPAQUE
2271+FF_DISABLE_DEPRECATION_WARNINGS
2272+ frame_data->reordered_opaque = frame->reordered_opaque;
2273+FF_ENABLE_DEPRECATION_WARNINGS
2274+#endif
2275+
2276+ ctx->frame_data_array_pos = (ctx->frame_data_array_pos + 1) % ctx->frame_data_array_nb;
2277+ pic_params->inputDuration = idx;
2278+
2279+ return res;
2280+}
2281+
2282+static int nvenc_retrieve_frame_data(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *lock_params, AVPacket *pkt)
2283+{
2284+ NvencContext *ctx = avctx->priv_data;
2285+ int res = 0;
2286+
2287+ int idx = lock_params->outputDuration;
2288+ NvencFrameData *frame_data = &ctx->frame_data_array[idx];
2289+
2290+ pkt->duration = frame_data->duration;
2291+
2292+#if FF_API_REORDERED_OPAQUE
2293+FF_DISABLE_DEPRECATION_WARNINGS
2294+ avctx->reordered_opaque = frame_data->reordered_opaque;
2295+FF_ENABLE_DEPRECATION_WARNINGS
2296+#endif
2297+
2298+ if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
2299+ pkt->opaque = frame_data->frame_opaque;
2300+ pkt->opaque_ref = frame_data->frame_opaque_ref;
2301+ frame_data->frame_opaque_ref = NULL;
2302+ }
2303+
2304+ av_buffer_unref(&frame_data->frame_opaque_ref);
2305+
2306+ return res;
2307+}
2308+
22362309 static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)
22372310 {
22382311 NvencContext *ctx = avctx->priv_data;
@@ -2319,6 +2392,10 @@ static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSur · 80 unmodified lines
23192392 if (res < 0)
23202393 goto error2;
23212394
2395+ res = nvenc_retrieve_frame_data(avctx, &lock_params, pkt);
2396+ if (res < 0)
2397+ goto error2;
2398+
23222399 return 0;
23232400
23242401 error:
@@ -2614,6 +2691,10 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) · 289 unmodified lines
26142691 sei_count = res;
26152692 }
26162693
2694+ res = nvenc_store_frame_data(avctx, &pic_params, frame);
2695+ if (res < 0)
2696+ return res;
2697+
26172698 nvenc_codec_specific_pic_params(avctx, &pic_params, ctx->sei_data, sei_count);
26182699 } else {
26192700 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
libavcodec/nvenc.h+17−0
@@ -31,6 +31,7 @@ typedef void ID3D11Device; · 30 unmodified lines
3131 #include <ffnvcodec/nvEncodeAPI.h>
3232
3333 #include "compat/cuda/dynlink_loader.h"
34+#include "libavutil/buffer.h"
3435 #include "libavutil/fifo.h"
3536 #include "libavutil/opt.h"
3637 #include "hwconfig.h"
@@ -95,6 +96,18 @@ typedef struct NvencSurface · 58 unmodified lines
9596 NV_ENC_BUFFER_FORMAT format;
9697 } NvencSurface;
9798
99+typedef struct NvencFrameData
100+{
101+ int64_t duration;
102+
103+#if FF_API_REORDERED_OPAQUE
104+ int64_t reordered_opaque;
105+#endif
106+
107+ void *frame_opaque;
108+ AVBufferRef *frame_opaque_ref;
109+} NvencFrameData;
110+
98111 typedef struct NvencDynLoadFunctions
99112 {
100113 CudaFunctions *cuda_dl;
@@ -173,6 +186,10 @@ typedef struct NvencContext · 72 unmodified lines
173186 int nb_surfaces;
174187 NvencSurface *surfaces;
175188
189+ NvencFrameData *frame_data_array;
190+ int frame_data_array_nb;
191+ int frame_data_array_pos;
192+
176193 AVFifo *unused_surface_queue;
177194 AVFifo *output_surface_queue;
178195 AVFifo *output_surface_ready_queue;
libavcodec/nvenc_av1.c+2−1
@@ -181,7 +181,8 @@ const FFCodec ff_av1_nvenc_encoder = { · 180 unmodified lines
181181 .defaults = defaults,
182182 .p.pix_fmts = ff_nvenc_pix_fmts,
183183 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
184− AV_CODEC_CAP_ENCODER_FLUSH | AV_CODEC_CAP_DR1,
184+ AV_CODEC_CAP_ENCODER_FLUSH | AV_CODEC_CAP_DR1 |
185+ AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
185186 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
186187 FF_CODEC_CAP_INIT_CLEANUP,
187188 .p.wrapper_name = "nvenc",
libavcodec/nvenc_h264.c+2−1
@@ -244,7 +244,8 @@ const FFCodec ff_h264_nvenc_encoder = { · 243 unmodified lines
244244 .p.priv_class = &h264_nvenc_class,
245245 .defaults = defaults,
246246 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
247− AV_CODEC_CAP_ENCODER_FLUSH | AV_CODEC_CAP_DR1,
247+ AV_CODEC_CAP_ENCODER_FLUSH | AV_CODEC_CAP_DR1 |
248+ AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
248249 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
249250 FF_CODEC_CAP_INIT_CLEANUP,
250251 .p.pix_fmts = ff_nvenc_pix_fmts,
libavcodec/nvenc_hevc.c+2−1
@@ -226,7 +226,8 @@ const FFCodec ff_hevc_nvenc_encoder = { · 225 unmodified lines
226226 .defaults = defaults,
227227 .p.pix_fmts = ff_nvenc_pix_fmts,
228228 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
229− AV_CODEC_CAP_ENCODER_FLUSH | AV_CODEC_CAP_DR1,
229+ AV_CODEC_CAP_ENCODER_FLUSH | AV_CODEC_CAP_DR1 |
230+ AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
230231 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
231232 FF_CODEC_CAP_INIT_CLEANUP,
232233 .p.wrapper_name = "nvenc",

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t