| @@ -971,6 +971,10 @@ static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx) · 970 unmodified lines |
| 971 | 971 | ctx->nb_surfaces = FFMAX(1, FFMIN(MAX_REGISTERED_FRAMES, ctx->nb_surfaces)); |
| 972 | 972 | ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1); |
| 973 | 973 | |
| 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 | + |
| 974 | 978 | return 0; |
| 975 | 979 | } |
| 976 | 980 | |
| @@ -1767,6 +1771,10 @@ static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx) · 790 unmodified lines |
| 1767 | 1771 | if (!ctx->surfaces) |
| 1768 | 1772 | return AVERROR(ENOMEM); |
| 1769 | 1773 | |
| 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 | + |
| 1770 | 1778 | ctx->timestamp_list = av_fifo_alloc2(ctx->nb_surfaces, sizeof(int64_t), 0); |
| 1771 | 1779 | if (!ctx->timestamp_list) |
| 1772 | 1780 | return AVERROR(ENOMEM); |
| @@ -1857,6 +1865,12 @@ av_cold int ff_nvenc_encode_close(AVCodecContext *avctx) · 84 unmodified lines |
| 1857 | 1865 | av_fifo_freep2(&ctx->output_surface_queue); |
| 1858 | 1866 | av_fifo_freep2(&ctx->unused_surface_queue); |
| 1859 | 1867 | |
| 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 | + |
| 1860 | 1874 | if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) { |
| 1861 | 1875 | for (i = 0; i < ctx->nb_registered_frames; i++) { |
| 1862 | 1876 | if (ctx->registered_frames[i].mapped) |
| @@ -2233,6 +2247,65 @@ FF_ENABLE_DEPRECATION_WARNINGS · 370 unmodified lines |
| 2233 | 2247 | return 0; |
| 2234 | 2248 | } |
| 2235 | 2249 | |
| 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 | + |
| 2236 | 2309 | static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf) |
| 2237 | 2310 | { |
| 2238 | 2311 | NvencContext *ctx = avctx->priv_data; |
| @@ -2319,6 +2392,10 @@ static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSur · 80 unmodified lines |
| 2319 | 2392 | if (res < 0) |
| 2320 | 2393 | goto error2; |
| 2321 | 2394 | |
| 2395 | + res = nvenc_retrieve_frame_data(avctx, &lock_params, pkt); |
| 2396 | + if (res < 0) |
| 2397 | + goto error2; |
| 2398 | + |
| 2322 | 2399 | return 0; |
| 2323 | 2400 | |
| 2324 | 2401 | error: |
| @@ -2614,6 +2691,10 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) · 289 unmodified lines |
| 2614 | 2691 | sei_count = res; |
| 2615 | 2692 | } |
| 2616 | 2693 | |
| 2694 | + res = nvenc_store_frame_data(avctx, &pic_params, frame); |
| 2695 | + if (res < 0) |
| 2696 | + return res; |
| 2697 | + |
| 2617 | 2698 | nvenc_codec_specific_pic_params(avctx, &pic_params, ctx->sei_data, sei_count); |
| 2618 | 2699 | } else { |
| 2619 | 2700 | pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS; |