From patchwork Sun Sep 26 06:40:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 30585 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp3046559iob; Sat, 25 Sep 2021 23:41:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwoV7xLW3sBtZrx31UFKpMQGKnrGePijaETxu8ERHRMHjs6N/P6nmMeTAgZIm+SFNYFHMpU X-Received: by 2002:aa7:c74c:: with SMTP id c12mr16009521eds.87.1632638465403; Sat, 25 Sep 2021 23:41:05 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bf24si14537616edb.7.2021.09.25.23.41.05; Sat, 25 Sep 2021 23:41:05 -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; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=C5pR65cP; 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 F180168A94E; Sun, 26 Sep 2021 09:40:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR03-AM5-obe.outbound.protection.outlook.com (mail-oln040092070041.outbound.protection.outlook.com [40.92.70.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 885E968A943 for ; Sun, 26 Sep 2021 09:40:50 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l9yL9D3kK6hYMDb/5pFXcn3cOgEEWr3RdQSgNt9mop0ea6wByvCZnQEgYk9nBeB6N+ZdqgPRDyFXy5D6IrsBYt9IBV8QHAPkdsU4sZO9YK0MuO5EFRzCwOfl0tVl6DjKOfw4OEmygHnSx48ajfvQHS8KLIlIRyxgXOiLX+h9GOli4NZ0jTN7j4jzGqLS1+W684qfAACxmBFYOgGCJNHScJAvSnessh+SelmsAj5fnHjGMEXa+zYNLPiUrMwHnCQsznizrd7MKqdGQYbsBfPIUpQIyAim0A3m6LoHx7MY9rGQYq4UwXH5sfciU6aQQWj6pqmkc9HZirDBhk6GVq7Lrg== 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; bh=+rExv4ZYtVhmgZbvKscQKyTavE/xSNSkRalwbBXldEs=; b=O28j7by/hMmyMKH3DREBrAO1gYIbxxaNn9g866BVgLCoPAhaOF83gJkcvmyQJlXRPgJRDHBkIKCaJ+nf9e6IWS5fRm8NYgAplhUA0f6vYFjZ066tzW+PkRpwWwSmbPBEFJILyqiB3S2rPtWZHCIAQDRUrlbrQnE63Th10Jge4LtzHHjcW+FBG97Ppm2FGW6XZEBRm+bXO6k+SviWXcwLs99UgWZzQlKLi6XAKTIae16cJq8KdCAr3IdknAud+vxNQYIOxzE7lUedG7VU6AL/p/wuegZjSeWms5IwZIb4uizEFBjc8g+/fynLAs90MKVPYcsnGo2hpCUYguZS0UAXrg== 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=+rExv4ZYtVhmgZbvKscQKyTavE/xSNSkRalwbBXldEs=; b=C5pR65cPifqf+lUqCc2y/St8oMXVW2Ml3Y8E+npZuABw5Zp0c7MQ0xeKCw75Xnmguf0CLXe459Ha+tqUPyj+9K0LPyV+jrhfncpPRTkCf7unnfCHT0rJ5EaBcHykvP11UeHVq1EEC9swVoNf/Dn7q1slCsZAzeiS2rghA92M+JKQYS9vBfhcs5XVQH73efq19B1ADxxoWhBRWtuxfshJ3MCTPwwyAW7A/ksBBJzV4R118Qco6i2McqWCJmS3+lv7lPtK7f+XD/iJSbRvvm67kRgh+wjua48AlUKnybsJOoH2ILfLWXRvqB/O0u00DSAKyy8T3zzDZqq3K4TgSZmx8g== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM6PR03MB3768.eurprd03.prod.outlook.com (2603:10a6:209:37::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.16; Sun, 26 Sep 2021 06:40:47 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::787b:2156:ca99:fe00]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::787b:2156:ca99:fe00%3]) with mapi id 15.20.4544.021; Sun, 26 Sep 2021 06:40:47 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sun, 26 Sep 2021 08:40:25 +0200 Message-ID: X-Mailer: git-send-email 2.30.2 In-Reply-To: References: X-TMN: [tntyimkDkX8sWNaGgZyS1BFxOFLZlkcM] X-ClientProxiedBy: AM4PR0302CA0010.eurprd03.prod.outlook.com (2603:10a6:205:2::23) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20210926064027.344398-3-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.142.38) by AM4PR0302CA0010.eurprd03.prod.outlook.com (2603:10a6:205:2::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.14 via Frontend Transport; Sun, 26 Sep 2021 06:40:47 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7ca966b1-ae1e-45a3-a77f-08d980b892af X-MS-Exchange-SLBlob-MailProps: q+fD6XS3/UIAJF/k4bgfswNNO8o0J1y9ephhUD51wECJIISOVB5iKKgxsE6d80ZlYH0BELZz39SpEU8p5669O7HPmkDSZqvu66XXGGduLxjrTvRhNflQXj3EfkW0Q38jPrY6eA++t59HVgqwQC3Y07/G2xCeW4lU1Z78SblK1vmH332Kt8hibO1YbRcowiwLI/4F94fOWO7k33aFl5w040z/CQSdFeAGaP/qUcdfZt228oQICqCjkmyP8M3+D0La/MVgzDXxk6lJlYctdLtLSf52wEeUidQGENKlbxlrKtom50TckVY+gQiQHzTUuXnCfM4zcKz9Z6p+psM7/o8Ey7usZAzJiHAMDkWB3Sv6KQb1/F8NSYH3DgCDHYkbXTZEH5k4cC58EX/6s/ObNzeb9oAJ1dC4AfKFLocMR3li29vJtptKohvWBYf0by4LnKQn0fwWMD0FFCDWIIIW/xuAYP9quAREBkJrv8i7hwQq40nBGbRsEgw4Ldwvv5VJLj/EaDCPipjecCAyLtS2tvmpES562SodRQX+oi42cdCmQPUZhjtsBtoGk//m6CZy1OMVFT5UILt/hEGUW6kah0MT1ZgbndJza43MnrJ4oC0TfLg6Ugi/wgaeDZKlyPh96+zSvjIhmkLoPYE2CyvXKgDFKqi0CGYV1zyeBM+mrsx6NT4Gpq9BIDy+Xg2H6n/kOX9+OayX68Tftv67a+otjnExaQ== X-MS-TrafficTypeDiagnostic: AM6PR03MB3768: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cofICdwwktNYG8Wfx0oDtRUAYb3YbpCCRd9gYPtZrqAsFtMXEVuUg5wuuTBO5Aj7uoCthyDh4ASdTBJJlgiVgLMVRbjVsxzA5eDqeSumU6rBnPtHQExSaRmRKXHmDexenvZikVSZLyLThg7IMljVznAdfece8sl4YkYKPfpdbguHDY5clVWYbCzPKtdVUt8uhM/CrBGqjARFGg5TssUW8A7kNrNpiZGu1T6eHwQNNum3VkfO6mztmkVK0GEWKAF+7C4mpvj6DFW3zBRvRqBQ15RIndR6XOQSqTjCjSNeVWpOBZXPlmEvMrhQb8V6ZNT7vle53/YCrpaIkOkapOOW9KYjEl87zNEHMAj7x+d8T1NGoA7XQkfgCVd1u4RHkq6RAndcV8zgFwT+cXKsoAFYWSV3y29+NW7+MbC6fQ2R7fZ3xvACEj9BKihjK8iRlAnQ X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: adkSNFRhk7nihRkRgztoYFUjR45DtF7lem2pea8b5RZ8yOt53yEe8Sogoo0moebgjMrVCzljGv76nMDoA+J1s2uMBIn3EJAUxW/TjRe33ecTFVfnS91fnkqhWZqweDdwEJIN++vKTZpO4gqHwvjcbA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7ca966b1-ae1e-45a3-a77f-08d980b892af X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Sep 2021 06:40:47.5955 (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: AM6PR03MB3768 Subject: [FFmpeg-devel] [PATCH 4/6] avcodec/qsvenc: Combine multiple allocations 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: VVsKuUQ6xgMX It makes the cleanup code smaller (and reduces the amount of allocations). Signed-off-by: Andreas Rheinhardt --- Better naming suggestions for the structures welcome. One could also stop using an av_fifo altogether and use an ordinary array (that is only allocated once) with FIFO semantics. Or one could combine the AVPacket and the pointer to the new structure to one structure, so that one can read and write it from/to the FIFO in one call. libavcodec/qsvenc.c | 111 ++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 65 deletions(-) diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 26a94cd419..1650d89a17 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -111,6 +111,19 @@ static const struct { #endif }; +typedef struct QSVTmp { + mfxBitstream bs; + mfxSyncPoint sync; +} QSVTmp; + +#if QSV_VERSION_ATLEAST(1, 26) +typedef struct QSVH264Tmp { + QSVTmp common; + mfxExtAVCEncodedFrameInfo enc_info; + mfxExtBuffer *enc_buf; +} QSVH264Tmp; +#endif + static const char *print_ratecontrol(mfxU16 rc_mode) { int i; @@ -1110,7 +1123,7 @@ static int qsvenc_init_session(AVCodecContext *avctx, QSVEncContext *q) static inline unsigned int qsv_fifo_item_size(void) { - return sizeof(AVPacket) + sizeof(mfxSyncPoint*) + sizeof(mfxBitstream*); + return sizeof(AVPacket) + sizeof(QSVTmp*); } static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo) @@ -1414,16 +1427,12 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, const AVFrame *frame) { AVPacket new_pkt = { 0 }; - mfxBitstream *bs = NULL; -#if QSV_VERSION_ATLEAST(1, 26) - mfxExtAVCEncodedFrameInfo *enc_info = NULL; - mfxExtBuffer **enc_buf = NULL; -#endif + QSVTmp *tmp_struct; mfxFrameSurface1 *surf = NULL; - mfxSyncPoint *sync = NULL; QSVFrame *qsv_frame = NULL; mfxEncodeCtrl* enc_ctrl = NULL; + size_t alloc_size; int ret; if (frame) { @@ -1443,34 +1452,36 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, enc_ctrl->FrameType |= MFX_FRAMETYPE_IDR; } } - ret = av_new_packet(&new_pkt, q->packet_size); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n"); return ret; } - - bs = av_mallocz(sizeof(*bs)); - if (!bs) - goto nomem; - bs->Data = new_pkt.data; - bs->MaxLength = new_pkt.size; + alloc_size = sizeof(QSVTmp); +#if QSV_VERSION_ATLEAST(1, 26) + if (avctx->codec_id == AV_CODEC_ID_H264) + alloc_size = sizeof(QSVH264Tmp); +#endif + tmp_struct = av_mallocz(alloc_size); + if (!tmp_struct) { + ret = AVERROR(ENOMEM); + goto free; + } + tmp_struct->bs.Data = new_pkt.data; + tmp_struct->bs.MaxLength = new_pkt.size; #if QSV_VERSION_ATLEAST(1, 26) if (avctx->codec_id == AV_CODEC_ID_H264) { - enc_info = av_mallocz(sizeof(*enc_info)); - if (!enc_info) - goto nomem; + QSVH264Tmp *const h264_tmp_struct = (QSVH264Tmp*)tmp_struct; + mfxExtAVCEncodedFrameInfo *const enc_info = &h264_tmp_struct->enc_info; + mfxExtBuffer **const enc_buf = &h264_tmp_struct->enc_buf; enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO; enc_info->Header.BufferSz = sizeof (*enc_info); - bs->NumExtParam = 1; - enc_buf = av_mallocz(sizeof(mfxExtBuffer *)); - if (!enc_buf) - goto nomem; + tmp_struct->bs.NumExtParam = 1; enc_buf[0] = (mfxExtBuffer *)enc_info; - bs->ExtParam = enc_buf; + tmp_struct->bs.ExtParam = enc_buf; } #endif @@ -1478,12 +1489,9 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, q->set_encode_ctrl_cb(avctx, frame, &qsv_frame->enc_ctrl); } - sync = av_mallocz(sizeof(*sync)); - if (!sync) - goto nomem; - do { - ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, bs, sync); + ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, + &tmp_struct->bs, &tmp_struct->sync); if (ret == MFX_WRN_DEVICE_BUSY) av_usleep(500); } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_WRN_IN_EXECUTION); @@ -1502,27 +1510,16 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, ret = 0; - if (*sync) { + if (tmp_struct->sync) { av_fifo_generic_write(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL); - av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL); - av_fifo_generic_write(q->async_fifo, &bs, sizeof(bs), NULL); + av_fifo_generic_write(q->async_fifo, &tmp_struct, sizeof(tmp_struct), NULL); } else { free: - av_freep(&sync); + av_freep(&tmp_struct); av_packet_unref(&new_pkt); - av_freep(&bs); -#if QSV_VERSION_ATLEAST(1, 26) - if (avctx->codec_id == AV_CODEC_ID_H264) { - av_freep(&enc_info); - av_freep(&enc_buf); - } -#endif } return ret; -nomem: - ret = AVERROR(ENOMEM); - goto free; } int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, @@ -1537,20 +1534,19 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) || (!frame && av_fifo_size(q->async_fifo))) { AVPacket new_pkt; + QSVTmp *tmp_struct; mfxBitstream *bs; - mfxSyncPoint *sync; #if QSV_VERSION_ATLEAST(1, 26) mfxExtAVCEncodedFrameInfo *enc_info; - mfxExtBuffer **enc_buf; #endif enum AVPictureType pict_type; av_fifo_generic_read(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL); - av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL); - av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL); + av_fifo_generic_read(q->async_fifo, &tmp_struct, sizeof(tmp_struct), NULL); + bs = &tmp_struct->bs; do { - ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000); + ret = MFXVideoCORE_SyncOperation(q->session, tmp_struct->sync, 1000); } while (ret == MFX_WRN_IN_EXECUTION); new_pkt.dts = av_rescale_q(bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base); @@ -1576,16 +1572,12 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, #if QSV_VERSION_ATLEAST(1, 26) if (avctx->codec_id == AV_CODEC_ID_H264) { - enc_buf = bs->ExtParam; enc_info = (mfxExtAVCEncodedFrameInfo *)(*bs->ExtParam); ff_side_data_set_encoder_stats(&new_pkt, enc_info->QP * FF_QP2LAMBDA, NULL, 0, pict_type); - av_freep(&enc_info); - av_freep(&enc_buf); } #endif - av_freep(&bs); - av_freep(&sync); + av_freep(&tmp_struct); av_packet_move_ref(pkt, &new_pkt); @@ -1619,23 +1611,12 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q) while (q->async_fifo && av_fifo_size(q->async_fifo)) { AVPacket pkt; - mfxSyncPoint *sync; - mfxBitstream *bs; + QSVTmp *tmp_struct; av_fifo_generic_read(q->async_fifo, &pkt, sizeof(pkt), NULL); - av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL); - av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL); + av_fifo_generic_read(q->async_fifo, &tmp_struct, sizeof(tmp_struct), NULL); -#if QSV_VERSION_ATLEAST(1, 26) - if (avctx->codec_id == AV_CODEC_ID_H264) { - mfxExtBuffer **enc_buf = bs->ExtParam; - mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*bs->ExtParam); - av_freep(&enc_info); - av_freep(&enc_buf); - } -#endif - av_freep(&sync); - av_freep(&bs); + av_freep(&tmp_struct); av_packet_unref(&pkt); } av_fifo_free(q->async_fifo);