From patchwork Wed May  8 07:11:13 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: David Rosca <nowrep@gmail.com>
X-Patchwork-Id: 48664
Delivered-To: ffmpegpatchwork2@gmail.com
Received: by 2002:a05:6a20:9c8f:b0:1af:cdee:28c5 with SMTP id mj15csp25694pzb;
        Wed, 8 May 2024 00:13:24 -0700 (PDT)
X-Forwarded-Encrypted: i=2;
 AJvYcCUU0WAejd+vI8Md/fk24xz5o+Vf/o8BRW6HaRPRrGxjq6S//PLkmyUZRPzd+gHeFBzdlMsfYeq9DopcaKEGU11Y8gnU172C7IbtRw==
X-Google-Smtp-Source: 
 AGHT+IG3oXW6Btb0nOsLsgP/8AeKUJDwzM/PSxrC5jUdm6vhgOZQDuI+wXt7iXGX4rN6ViSYQ3D/
X-Received: by 2002:a17:906:6b9a:b0:a59:bae0:b12f with SMTP id
 a640c23a62f3a-a59fb9ce9d9mr94975366b.57.1715152404530;
        Wed, 08 May 2024 00:13:24 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1715152404; cv=none;
        d=google.com; s=arc-20160816;
        b=TyPiCxBIX4CkQJzb/tN61GGUtaed+8YXJp0fMtMCWRLhJtQcyPEGQfgMMxf8ud7SjV
         XasOofP00bXz6aA/3IDgRNOOR8W9kwSTI6uLYm67idB/vFjKHziHELvkE8reWyYGGUnm
         3Wac/MdE30bLkfj5e5xLKTuAjS+tfDiqPQoY+x0bVErkuGZ5kvsVFBY36zForQQI6gI6
         7KRKgu69eooP/7GjdfgBJDpRoV5cOimWHTnOrFicoplIhvkiGya3wLg8ewhvYd0FvEPl
         mV9jr2J4EkZvwXU6mU0H4kfz3XnZ5lm3VcvN1/7IyVvb2vbUar5H/YpFy/Egakk6kZDq
         E2WA==
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:dkim-signature:delivered-to;
        bh=lG1MGLor19Gww/xCmlAJWXZbhpue6ooFmY8SVpcxzoo=;
        fh=mbb85L8FZ404unHdLI+iF4ZFmTMGr6nZHIcfO/oKEvw=;
        b=Gf3+xGKpwkxLAdDWLnNlyqQItLga7dBAeke4JLFO0nfgfO9y88/lSZaHMQrNLrFA22
         vJ95hHGS2oaiaJcDbE1P6tmZrp0qjDqKSW02GplFORvI92l80hmqZfUjnZ7Ck72ZMj8e
         3xPxoevI40uz1A0v7kgYvTpMvHezDaOOn3/p6/lEDhWNieas1xoZv8/EUvXGVMyyslOJ
         BjLbxoV90InDYTDuDSJyFBl91A/gqOWEATTQJk/mABJhXpbljeejfWyg7jaWnbViIoUD
         eIDt7wsyJYmNQkCISpWuISkqBrWXlKxkQvB6MihxwIdtPcYmQ6ra6rKNRI924EX2VAEK
         yXVA==;
        dara=google.com
ARC-Authentication-Results: i=1; mx.google.com;
       dkim=neutral (body hash did not verify) header.i=@gmail.com
 header.s=20230601 header.b=OypvhYGi;
       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=gmail.com
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100])
        by mx.google.com with ESMTP id
 b27-20020a170906151b00b00a59befed255si3984295ejd.576.2024.05.08.00.13.24;
        Wed, 08 May 2024 00:13:24 -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=@gmail.com
 header.s=20230601 header.b=OypvhYGi;
       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=gmail.com
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6258868D5D8;
	Wed,  8 May 2024 10:13:16 +0300 (EEST)
X-Original-To: ffmpeg-devel@ffmpeg.org
Delivered-To: ffmpeg-devel@ffmpeg.org
Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com
 [209.85.218.42])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8ED3B68D5C6
 for <ffmpeg-devel@ffmpeg.org>; Wed,  8 May 2024 10:13:09 +0300 (EEST)
Received: by mail-ej1-f42.google.com with SMTP id
 a640c23a62f3a-a59a934ad50so920836666b.1
 for <ffmpeg-devel@ffmpeg.org>; Wed, 08 May 2024 00:13:09 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1715152388; x=1715757188; darn=ffmpeg.org;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:from:to:cc:subject:date
 :message-id:reply-to;
 bh=2gr9y0ycJUz7g/8locnNp/xbvm4Xt9bm5oGOJk63n1Q=;
 b=OypvhYGiEgVBtlATeDRoSQ8uoRlMpZj4OZFdaLx8eHWuZ+dnUTUL04V8v+psg5iqL9
 A53WZ9lLffgZpj3KrBtoMKXb4wdW7gf4xM5/PKTx5VPQdK6BRLXEKVLSaRzFBWHFu/9b
 bkQfxnsTF+tKwKnuxomZEnZKdtxK9GzoMx5uPJnT27LqxU6DGSWe4OY9or5BO4iRGThe
 suvUHkZ7K3MRjncUVtPcm2CF8wIXlbDsD2MROp/GnG7UWUwCZlVIwNiyDlfrygvGSqrb
 5hToNlkEs+yKgcH3mQsYQQOOS7zPWj3OZ+3xHufHNmYxXvwlf/l2NVxED2RpgPFHLTIe
 mYEQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1715152388; x=1715757188;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=2gr9y0ycJUz7g/8locnNp/xbvm4Xt9bm5oGOJk63n1Q=;
 b=Bd1s9ViPxXl4TdTBYOfQAT9wrkmI1uBRar/MUyJEIHHv1GVO6yy0mGvKXzK9dA7JRf
 ADq8nqUSLuJm7HVqX03X8vNl8lhQzOHeq0lnTNP8OWU3WLn+8vgINDqrg7u2TBhVJChu
 fk0aAuG361EckiGCujtlnjWPmUmjaCyWwVqDoPGJQW31L8jqPklOKIEr2Le6Zc7+eX67
 1tU4u4MMWZqo4UPVdhYopV1em2EVLvq9eu3PdqK9Bx4Q+DwCYyC3KPxZEJm5xt9EAir0
 b4k642TSP4uTtbQCPt/qDI32CsnA+nvz1p5iCJIS3n6WWrKBha+I+Bl1nvtLwQCRCRSP
 Ap5A==
X-Gm-Message-State: AOJu0YyCCVm0AjHkgA3h1/7JMutpn1Aexcggxj5vAwjMMJVANDxR5Prh
 oeODe3Hr6qmpyzUfxgYYzOINyE0APs4dadLz45mg5zgYptS+BHmNCBegTQ==
X-Received: by 2002:a17:906:80c2:b0:a55:5e89:a158 with SMTP id
 a640c23a62f3a-a59fb9e7628mr98238166b.68.1715152388430;
 Wed, 08 May 2024 00:13:08 -0700 (PDT)
Received: from mitsuki.. (ip-78-45-35-87.bb.vodafone.cz. [78.45.35.87])
 by smtp.gmail.com with ESMTPSA id
 t16-20020a170906609000b00a59d7505366sm2872828ejj.62.2024.05.08.00.13.07
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Wed, 08 May 2024 00:13:08 -0700 (PDT)
From: David Rosca <nowrep@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Date: Wed,  8 May 2024 09:11:13 +0200
Message-ID: <20240508071247.133615-4-nowrep@gmail.com>
X-Mailer: git-send-email 2.45.0
In-Reply-To: <20240508071247.133615-2-nowrep@gmail.com>
References: <20240508071247.133615-2-nowrep@gmail.com>
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH v2 2/2] lavc/vaapi_av1: Avoid sending the
 same slice buffer multiple times
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: David Rosca <nowrep@gmail.com>
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
X-TUID: q0qvq9b+ZBYX

When there are multiple tiles in one slice buffer, use multiple slice
params to avoid sending the same slice buffer multiple times and thus
increasing the bitstream size the driver will need to upload to hw.
---
v2: Avoid allocations every slice.

 libavcodec/vaapi_av1.c | 47 +++++++++++++++++++++++++++++-------------
 1 file changed, 33 insertions(+), 14 deletions(-)

diff --git a/libavcodec/vaapi_av1.c b/libavcodec/vaapi_av1.c
index 4a90db1e09..4ee33a3ae3 100644
--- a/libavcodec/vaapi_av1.c
+++ b/libavcodec/vaapi_av1.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/frame.h"
+#include "libavutil/mem.h"
 #include "hwaccel_internal.h"
 #include "vaapi_decode.h"
 #include "internal.h"
@@ -42,6 +43,9 @@ typedef struct VAAPIAV1DecContext {
     */
     VAAPIAV1FrameRef ref_tab[AV1_NUM_REF_FRAMES];
     AVFrame *tmp_frame;
+
+    int nb_slice_params;
+    VASliceParameterBufferAV1 *slice_params;
 } VAAPIAV1DecContext;
 
 static VASurfaceID vaapi_av1_surface_id(AV1Frame *vf)
@@ -97,6 +101,8 @@ static int vaapi_av1_decode_uninit(AVCodecContext *avctx)
     for (int i = 0; i < FF_ARRAY_ELEMS(ctx->ref_tab); i++)
         av_frame_free(&ctx->ref_tab[i].frame);
 
+    av_freep(&ctx->slice_params);
+
     return ff_vaapi_decode_uninit(avctx);
 }
 
@@ -393,13 +399,24 @@ static int vaapi_av1_decode_slice(AVCodecContext *avctx,
 {
     const AV1DecContext *s = avctx->priv_data;
     VAAPIDecodePicture *pic = s->cur_frame.hwaccel_picture_private;
-    VASliceParameterBufferAV1 slice_param;
-    int err = 0;
+    VAAPIAV1DecContext *ctx = avctx->internal->hwaccel_priv_data;
+    int err, nb_params;
+
+    nb_params = s->tg_end - s->tg_start + 1;
+    if (ctx->nb_slice_params < nb_params) {
+        ctx->slice_params = av_realloc_array(ctx->slice_params,
+                                             nb_params,
+                                             sizeof(*ctx->slice_params));
+        if (!ctx->slice_params) {
+            ctx->nb_slice_params = 0;
+            err = AVERROR(ENOMEM);
+            goto fail;
+        }
+        ctx->nb_slice_params = nb_params;
+    }
 
     for (int i = s->tg_start; i <= s->tg_end; i++) {
-        memset(&slice_param, 0, sizeof(VASliceParameterBufferAV1));
-
-        slice_param = (VASliceParameterBufferAV1) {
+        ctx->slice_params[i - s->tg_start] = (VASliceParameterBufferAV1) {
             .slice_data_size   = s->tile_group_info[i].tile_size,
             .slice_data_offset = s->tile_group_info[i].tile_offset,
             .slice_data_flag   = VA_SLICE_DATA_FLAG_ALL,
@@ -408,18 +425,20 @@ static int vaapi_av1_decode_slice(AVCodecContext *avctx,
             .tg_start          = s->tg_start,
             .tg_end            = s->tg_end,
         };
-
-        err = ff_vaapi_decode_make_slice_buffer(avctx, pic, &slice_param, 1,
-                                                sizeof(VASliceParameterBufferAV1),
-                                                buffer,
-                                                size);
-        if (err) {
-            ff_vaapi_decode_cancel(avctx, pic);
-            return err;
-        }
     }
 
+    err = ff_vaapi_decode_make_slice_buffer(avctx, pic, ctx->slice_params, nb_params,
+                                            sizeof(VASliceParameterBufferAV1),
+                                            buffer,
+                                            size);
+    if (err)
+        goto fail;
+
     return 0;
+
+fail:
+    ff_vaapi_decode_cancel(avctx, pic);
+    return err;
 }
 
 const FFHWAccel ff_av1_vaapi_hwaccel = {
