From patchwork Mon Jan 24 14:45:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 33775 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:e71a:0:0:0:0:0 with SMTP id b26csp387213ioh; Mon, 24 Jan 2022 06:48:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJyCf68f15BcxRqI4Le1TAYXcBbqjr8uB7XISey8fYCaGV9iWl/BJJWTW4stVIe9ypkjf5SX X-Received: by 2002:a17:906:4983:: with SMTP id p3mr12567032eju.589.1643035723545; Mon, 24 Jan 2022 06:48:43 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id nb42si10109078ejc.13.2022.01.24.06.48.43; Mon, 24 Jan 2022 06:48:43 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=bLwkF6Di; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5EEDF68B0E0; Mon, 24 Jan 2022 16:47:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2017.outbound.protection.outlook.com [40.92.89.17]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0C5B868B0A4 for ; Mon, 24 Jan 2022 16:47:13 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SlqrahoZm7PM6ac34csNjIoItAbg7eQUnk1RcdebefW0cMUQ3YE4Jqwp4e3RmgIe4j6LvnBDnBIXjqZKcCPMazwYK/ep3szTtJMosBQWQ1V8kMRR9rdvle4dO/IMOs/k0OLlcagT2hT1fFcLUo/JmDd1ASR+C6DbeYILpTe8CD30fwWaM/zpZ5xNCRHUdfEph/Ndiymau+OAscyicdZS2/aXrl0UAkUKoqGnx/a4o/36n3cD7/o/1QiGowoI9rgldqsOAqgIWOK6kQBbPvjYlejgUtMc30AVpwALCzIcG80IFU0qSwsCb8kuadjAzFP6uc0Zbym5petLXkFejW3IKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=f2H/cuvgyjDfjCW36L1M5pBrWvTWjulprZgoo2voRvc=; b=mN/GdTwiFTGTuq6fbxCpA/vmnLI9d8Fwu2etzYdzzhWO169KAn3gp81LFlBdnSjd096Va3Ml/Swv8ykyVeFMIJxrnbQEFrPX/wL/gwS5XrYz5WaTZD+yYB87V0ceytX/fbcD2FwPWqf8H9Ho1yqMpq5ivvsFTXp9Bg2qFacf5TbqmWXzvcMfyqiuUJdfkSy9qd82p5vzMjJGbV09QbuNh3iydV4L0DGoILWh+W4r6Mo2eBhpVM74ilKEfVB7kuXGZCNrTNEJX5GtS42Y5x3CwMhzW+LL4On4t2VKu8DIK3fJxqoI0GGCs28y+x24TXMg20QdUSl4NRGTTsMy31revw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=f2H/cuvgyjDfjCW36L1M5pBrWvTWjulprZgoo2voRvc=; b=bLwkF6DiOpyL0FC6bWKZbm7UEJ6SfxryZHolD02tFk3muTjX3oI2jSOgD1/bS9P9w2Uqo4zAsKxVZ43Qtt48cyG01or29iqyrvaE0ad92lcoowybBLQpN/SBgitvdTvxpyxtob+qI+MYGXvztyM5DkLuN2blmzY/2OiSBkenXfBE0Acp1AQy0qVKGkTUGenfwKMoTHZOAeCyavCWYtbOgzFe0vJAHazbIYQK3uMkOhd+lnGeYPMjSvcrEg5CT/mx620T2w6aatFRua/T8buWdxyEkfAppnsOFpdRaka7e3Dq0Du/Tz5k8uaIl+0/CKhZTyKZMYibYeI8Ngb8+xvaFA== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by VI1PR03MB3088.eurprd03.prod.outlook.com (2603:10a6:802:2f::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.17; Mon, 24 Jan 2022 14:47:12 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::ac56:2ff4:d304:ab22]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::ac56:2ff4:d304:ab22%4]) with mapi id 15.20.4909.017; Mon, 24 Jan 2022 14:47:12 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Jan 2022 15:45:58 +0100 Message-ID: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-TMN: [Gnp6U6i9iwB9sq0suBdKLgfbVnx+vhr9] X-ClientProxiedBy: AM5PR0201CA0016.eurprd02.prod.outlook.com (2603:10a6:203:3d::26) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20220124144616.559446-13-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9a8d4e08-5c53-491c-5061-08d9df4867d7 X-MS-TrafficTypeDiagnostic: VI1PR03MB3088:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: a0iof7aWb0ghf87tTc6RdDmIJDqXXAzAfkrNqXiMs9rSLygBEM9sePx/bZBPuV98cFwS5IqbPB1wOlIHKwObD94Hf28YLZ/9rPRQ0Zo5/SClQSRwKPmkp4qvclq946j8WHHT2QLu6AsCQORcS4roUtq+6vrdQuquaiQwsnR5pFm4pdBglruw6RUtE7YrF3AM0ZzvATaDuJvqTfqpJxnXlLf9RBkoJJrYkyPxtutoCjZXGoZl6xwuhU7F/JDPZ6sQLJCiqKlPIXYp00QuC6Zs0vjt2SMccqbuAU6bo6j05x4KTsqLGpYjkEyYYZ41FL95RksbMNoR8e3aUpJrmrsakS01eU7L75HdfXdIp4xXDLfifjHcaGxmrONMbnwoEWSWJ8iZYEy4NT5xSScEO9S1UZT9G9sz6pLH0SvswvHGvFJ4or2/xFfm2Jv44E1NE4XDJzGVVpCRlv4UtOs09QHuODWK2Bf5mf3je8OU4DRyo+1zuTb2HWhzYsVSbpsHgXHE6cqhlHjtV39ui8lenfbmfvxvkY30g5Zp0UI8ZqmTpRsSioqcJtE7kr7s+5Cr/iKre+88pQf5yWD0bf/xODVySg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8ZTQF4gWSIpMWGkJunrtakN1vh11ld4wFX3BqRLGrKEc5Ueno65HuutGgclo7EWVbMQvHzjS8jiR032W6cIfYLe0VqG1FCdgsdxDn5yfvW65T0nSVZlNE9pebDXDw8nmVFdEyTn8WQtDWpIVM2swjKtaVHUoba3BZ/bB9BB2n+4mcftuDkswh1t+44sAM5P1OkHTSbQC0fLnbSZz40xd0rVRpijffSeeXbkMy4WWWqLEl/Kls5LAAq9nGx05Hnd75O3QZWfRhxQ5aTypmY1SGfAwodanPhhqROnpA2MY1em0MfDJv2kbHsdZHQcYv6n54Z0d7dF8z2iQSFsA3mgyoRn+BZ2/gUcaXgWP1oVgni4tKDdW62Oai9esBARLwAtp+JqERFE3PW2SwlftfPXmslAL4qK55Xh/N8gma5dUAc3xZWQ/MVBGt5Z9iAKVvy6D5g0x5xqwxOTXxsPjLobGV4FFNfHZhoK7bvh+KeZts40SZREeAT05i0fqRjxRjXVB8raqQAK14pL3mgi8Si8uoY4lfTliz9AME/n01ptqJxhqLzeiFdRsdKOAMAcrIQ4obpddIs3SUEyyYwIJnPr2ubKXXUsb84cgwzAaD/n/o3R4gMZPCntXj6J+Vl9ZaL1NleNstTJfcw2DMef99z06wVzz/wXMRer3lKAyRXIXfKF422TMmJ6TH8S3dgjUdYDYbzbzsfXmS2lddxPc/9B/rGyvyJKGMSV+Gh5Mu0xt3bwNd4hmwk6u3Lxg87/TwcKJ77m5U7g6Ztaq53lVW1ZD3mLgIdhIwHdcU5I/gbPo9Bhr0VkldlHGWyVEyUV+dtSoFgImTUoMvdw+dghsitqUnHZqfaQa6ykGB3Frjd/VAvuh/mBErIYy6wbN0rE+JJhhbYSIdCMwnZmEISgaXRzyTYvSH52D8qpHRtMqndR4dWiz0UYYb8YiS7eA9wf7wtr3PYCmwYDczNN0Ube/QFlRkA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9a8d4e08-5c53-491c-5061-08d9df4867d7 X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2022 14:47:12.4951 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR03MB3088 Subject: [FFmpeg-devel] [PATCH v2 13/31] lavc/nvenc: switch to the new FIFO API X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Anton Khirnov Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: eMd43mG8svsk From: Anton Khirnov --- libavcodec/nvenc.c | 49 ++++++++++++++++++++++------------------------ libavcodec/nvenc.h | 8 ++++---- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c index 850c46022b..effd6381da 100644 --- a/libavcodec/nvenc.c +++ b/libavcodec/nvenc.c @@ -1568,7 +1568,7 @@ static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx) ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer; - av_fifo_generic_write(ctx->unused_surface_queue, &tmp_surface, sizeof(tmp_surface), NULL); + av_fifo_write(ctx->unused_surface_queue, &tmp_surface, 1); return 0; } @@ -1582,18 +1582,18 @@ static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx) if (!ctx->surfaces) return AVERROR(ENOMEM); - ctx->timestamp_list = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t)); + ctx->timestamp_list = av_fifo_alloc2(ctx->nb_surfaces, sizeof(int64_t), 0); if (!ctx->timestamp_list) return AVERROR(ENOMEM); - ctx->unused_surface_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*)); + ctx->unused_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0); if (!ctx->unused_surface_queue) return AVERROR(ENOMEM); - ctx->output_surface_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*)); + ctx->output_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0); if (!ctx->output_surface_queue) return AVERROR(ENOMEM); - ctx->output_surface_ready_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*)); + ctx->output_surface_ready_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0); if (!ctx->output_surface_ready_queue) return AVERROR(ENOMEM); @@ -1666,10 +1666,10 @@ av_cold int ff_nvenc_encode_close(AVCodecContext *avctx) p_nvenc->nvEncEncodePicture(ctx->nvencoder, ¶ms); } - av_fifo_freep(&ctx->timestamp_list); - av_fifo_freep(&ctx->output_surface_ready_queue); - av_fifo_freep(&ctx->output_surface_queue); - av_fifo_freep(&ctx->unused_surface_queue); + av_fifo_freep2(&ctx->timestamp_list); + av_fifo_freep2(&ctx->output_surface_ready_queue); + av_fifo_freep2(&ctx->output_surface_queue); + av_fifo_freep2(&ctx->unused_surface_queue); if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) { for (i = 0; i < ctx->nb_registered_frames; i++) { @@ -1777,11 +1777,10 @@ static NvencSurface *get_free_frame(NvencContext *ctx) { NvencSurface *tmp_surf; - if (!(av_fifo_size(ctx->unused_surface_queue) > 0)) + if (av_fifo_read(ctx->unused_surface_queue, &tmp_surf, 1) < 0) // queue empty return NULL; - av_fifo_generic_read(ctx->unused_surface_queue, &tmp_surf, sizeof(tmp_surf), NULL); return tmp_surf; } @@ -1998,16 +1997,16 @@ static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, } } -static inline void timestamp_queue_enqueue(AVFifoBuffer* queue, int64_t timestamp) +static inline void timestamp_queue_enqueue(AVFifo *queue, int64_t timestamp) { - av_fifo_generic_write(queue, ×tamp, sizeof(timestamp), NULL); + av_fifo_write(queue, ×tamp, 1); } -static inline int64_t timestamp_queue_dequeue(AVFifoBuffer* queue) +static inline int64_t timestamp_queue_dequeue(AVFifo *queue) { int64_t timestamp = AV_NOPTS_VALUE; - if (av_fifo_size(queue) > 0) - av_fifo_generic_read(queue, ×tamp, sizeof(timestamp), NULL); + // The following call might fail if the queue is empty. + av_fifo_read(queue, ×tamp, 1); return timestamp; } @@ -2152,8 +2151,8 @@ static int output_ready(AVCodecContext *avctx, int flush) NvencContext *ctx = avctx->priv_data; int nb_ready, nb_pending; - nb_ready = av_fifo_size(ctx->output_surface_ready_queue) / sizeof(NvencSurface*); - nb_pending = av_fifo_size(ctx->output_surface_queue) / sizeof(NvencSurface*); + nb_ready = av_fifo_can_read(ctx->output_surface_ready_queue); + nb_pending = av_fifo_can_read(ctx->output_surface_queue); if (flush) return nb_ready > 0; return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth); @@ -2442,16 +2441,14 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) return nvenc_print_error(avctx, nv_status, "EncodePicture failed!"); if (frame && frame->buf[0]) { - av_fifo_generic_write(ctx->output_surface_queue, &in_surf, sizeof(in_surf), NULL); + av_fifo_write(ctx->output_surface_queue, &in_surf, 1); timestamp_queue_enqueue(ctx->timestamp_list, frame->pts); } /* all the pending buffers are now ready for output */ if (nv_status == NV_ENC_SUCCESS) { - while (av_fifo_size(ctx->output_surface_queue) > 0) { - av_fifo_generic_read(ctx->output_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); - av_fifo_generic_write(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); - } + while (av_fifo_read(ctx->output_surface_queue, &tmp_out_surf, 1) >= 0) + av_fifo_write(ctx->output_surface_ready_queue, &tmp_out_surf, 1); } return 0; @@ -2483,7 +2480,7 @@ int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) av_frame_unref(frame); if (output_ready(avctx, avctx->internal->draining)) { - av_fifo_generic_read(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); + av_fifo_read(ctx->output_surface_ready_queue, &tmp_out_surf, 1); res = nvenc_push_context(avctx); if (res < 0) @@ -2498,7 +2495,7 @@ int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) if (res) return res; - av_fifo_generic_write(ctx->unused_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); + av_fifo_write(ctx->unused_surface_queue, &tmp_out_surf, 1); } else if (avctx->internal->draining) { return AVERROR_EOF; } else { @@ -2513,5 +2510,5 @@ av_cold void ff_nvenc_encode_flush(AVCodecContext *avctx) NvencContext *ctx = avctx->priv_data; nvenc_send_frame(avctx, NULL); - av_fifo_reset(ctx->timestamp_list); + av_fifo_reset2(ctx->timestamp_list); } diff --git a/libavcodec/nvenc.h b/libavcodec/nvenc.h index b42a156c56..9eb129952e 100644 --- a/libavcodec/nvenc.h +++ b/libavcodec/nvenc.h @@ -168,10 +168,10 @@ typedef struct NvencContext int nb_surfaces; NvencSurface *surfaces; - AVFifoBuffer *unused_surface_queue; - AVFifoBuffer *output_surface_queue; - AVFifoBuffer *output_surface_ready_queue; - AVFifoBuffer *timestamp_list; + AVFifo *unused_surface_queue; + AVFifo *output_surface_queue; + AVFifo *output_surface_ready_queue; + AVFifo *timestamp_list; NV_ENC_SEI_PAYLOAD *sei_data; int sei_data_size;