From patchwork Sat May 15 05:43:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brad Hards X-Patchwork-Id: 27782 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:b214:0:0:0:0:0 with SMTP id b20csp1068090iof; Fri, 14 May 2021 22:44:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwtqjE1o+tsrSfvetbrwR0Hj8UrC/MpWiU0gABXZfgtE3+PJFDmScKjHSbESJRK+ex9UUu7 X-Received: by 2002:a17:907:7747:: with SMTP id kx7mr35268626ejc.400.1621057466460; Fri, 14 May 2021 22:44:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621057466; cv=none; d=google.com; s=arc-20160816; b=fIOLzrI0RycHVH/Ts9s1SK7ROryI5//X/KFlwIiMTj7L/Aapl5ixSrAW9L4T8CntP8 1iwcWm8j2f3nkhMEdBCgvqZ5Gk/hW0kqYxQhvnYxzpCKueZBRj1Sl6NVqskIESuNjQTP EYbJtJgRFBsaMb6rx8KviGUSPCqPiQEHbXmFOFx82FArcuvxg+YqrZIaqPcaIdNHYUbA 4s8jKjyRtZN3JIYhVG41VNrNS2XIr51/PUoszQiFPz52qoJd3wrk6QKIcRCpfmWjNwY8 ZtWrRPt0S0+jctCfuFZGM01MP78IHi7zkmzvM9keHesKFlRYd/7YTloaSwfYJSNzrTvg VexA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to; bh=ZcX9wr4PcSQqXPXKXYFNp+WJjj9wGYVf2aW2NqYCXas=; b=tVdwbW1kjtCVnn14wQyrtk7gWxVDwcOTSv09QfXbJMjbnMpAjjnFAvO/hkXR1PNhXF 0+DXYxuYcL2vXG7FIEHM1k9/WoKdPJB8yoa+5kFMxqj1qzoVJg6MnPU2Qmvz57hF6dJA RBQozRnCp+UWg3Tx1JCWz7fpJuTf+hjvoSbwkVyGUXlmgSmImyugOlNLndFPulExUf47 hmgZxX9WEn9u7nS51CNP7zenht7pJHuOt2Af+ZBttbRkcj/WUozdhY6JIk64eUxRIckB +rUxgC2x0XfupO1FpBWys+gVEuVr0DIzyrj18S1m2+73mEwI0YtMW8Olof2jEpFaWw6m sBZQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id f17si8189728edx.76.2021.05.14.22.44.26; Fri, 14 May 2021 22:44:26 -0700 (PDT) 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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 37F6A688178; Sat, 15 May 2021 08:43:50 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from cp1.nerdvana.net.au (cp1-new.nerdvana.net.au [113.212.99.70]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9C868687F43 for ; Sat, 15 May 2021 08:43:39 +0300 (EEST) Received: from ppp115-205.static.internode.on.net ([150.101.115.205]:60460 helo=audax.fritz.box) by cp1.nerdvana.net.au with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1lhn5N-0009gp-CQ; Sat, 15 May 2021 15:43:37 +1000 From: Brad Hards To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 May 2021 15:43:17 +1000 Message-Id: <20210515054317.30414-4-bradh@frogmouth.net> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210515054317.30414-1-bradh@frogmouth.net> References: <20210515054317.30414-1-bradh@frogmouth.net> MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - cp1.nerdvana.net.au X-AntiAbuse: Original Domain - ffmpeg.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - frogmouth.net X-Get-Message-Sender-Via: cp1.nerdvana.net.au: authenticated_id: bradh@frogmouth.net X-Authenticated-Sender: cp1.nerdvana.net.au: bradh@frogmouth.net X-Source: X-Source-Args: X-Source-Dir: Subject: [FFmpeg-devel] [PATCH v3 3/3] libavcodec: write out user data unregistered SEI for nvenc 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: Brad Hards Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: tmw7B3HZQkmW Signed-off-by: Brad Hards --- libavcodec/nvenc.c | 64 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c index 0dcd93a99c..9bf2728cba 100644 --- a/libavcodec/nvenc.c +++ b/libavcodec/nvenc.c @@ -2170,9 +2170,10 @@ 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]; + NV_ENC_SEI_PAYLOAD *sei_data = 0; int sei_count = 0; int i; + int total_unregistered_sei = 0; NvencContext *ctx = avctx->priv_data; NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; @@ -2185,6 +2186,8 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) return AVERROR(EINVAL); if (frame && frame->buf[0]) { + void *a53_data = NULL; + void *tc_data = NULL; in_surf = get_free_frame(ctx); if (!in_surf) return AVERROR(EAGAIN); @@ -2230,7 +2233,6 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) pic_params.inputTimeStamp = frame->pts; if (ctx->a53_cc && av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC)) { - void *a53_data = NULL; size_t a53_size = 0; if (ff_alloc_a53_sei(frame, 0, (void**)&a53_data, &a53_size) < 0) { @@ -2238,15 +2240,21 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) } 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 ++; + sei_data = av_realloc_array(sei_data, sei_count + 1, sizeof(NV_ENC_SEI_PAYLOAD)); + if (!sei_data) { + av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions SEI, skipping\n"); + av_free(a53_data); + sei_count = 0; + } else { + 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 ++; + } } } if (ctx->s12m_tc && av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE)) { - void *tc_data = NULL; size_t tc_size = 0; if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, (void**)&tc_data, &tc_size) < 0) { @@ -2254,13 +2262,46 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) } 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 ++; + sei_data = av_realloc_array(sei_data, sei_count + 1, sizeof(NV_ENC_SEI_PAYLOAD)); + if (!sei_data) { + av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode SEI, skipping\n"); + av_free(a53_data); + av_free(tc_data); + sei_count = 0; + } else { + 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 ++; + } } } + for (int j = 0; j < frame->nb_side_data; j++) + if (frame->side_data[j]->type == AV_FRAME_DATA_SEI_UNREGISTERED) + total_unregistered_sei++; + if (total_unregistered_sei > 0) { + sei_data = av_realloc_array(sei_data, + sei_count + total_unregistered_sei, + sizeof(NV_ENC_SEI_PAYLOAD)); + if (!sei_data) { + av_log(ctx, AV_LOG_ERROR, "Not enough memory for unregistered SEI, skipping\n"); + av_free(a53_data); + av_free(tc_data); + sei_count = 0; + } else + for (int j = 0; j < frame->nb_side_data; j++) { + AVFrameSideData *side_data = frame->side_data[j]; + if (side_data->type == AV_FRAME_DATA_SEI_UNREGISTERED) { + sei_data[sei_count].payloadSize = side_data->size; + sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_UNREGISTERED; + sei_data[sei_count].payload = av_memdup(side_data->data, side_data->size); + if (sei_data[sei_count].payload) + sei_count ++; + } + } + } + nvenc_codec_specific_pic_params(avctx, &pic_params, sei_data, sei_count); } else { pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS; @@ -2274,6 +2315,7 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) for ( i = 0; i < sei_count; i++) av_freep(&sei_data[i].payload); + av_free(sei_data); res = nvenc_pop_context(avctx); if (res < 0)