From patchwork Sun Jan 27 23:47:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 11891 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id D202444DDD3 for ; Mon, 28 Jan 2019 01:47:30 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 022D968B037; Mon, 28 Jan 2019 01:47:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A8E8268B02C for ; Mon, 28 Jan 2019 01:47:11 +0200 (EET) Received: by mail-wr1-f67.google.com with SMTP id z5so16074852wrt.11 for ; Sun, 27 Jan 2019 15:47:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=OLzaeUS06+yhltBxU+w+m2N7paugxw/aDMH684EFsfI=; b=iZB7o+gfTaAdJw/7nRhIeDXUALSPWA3PPbMr5cW7mIvP0s4exbuHlpqg8ki4fKXd9j +BLT1UHhA7C9sffTx+iG4PmMISp/4O39sXKH26/c7KgNvpxXZgGRY1o6TPjxId8tqeEq zqk7XEK7Q537PEooXNmaRHhsJoQgEPSQIY7MPHgTRJBytEcLdoE7A2oNtRSpvVgIa1oJ F0aOiw8pW7kGvTD6EfQAJezJGQSAAr86eJcZekxritPmIn2ADoFFAjcAeJAr98LD8kfq k59ikOAd46VafMqVCp6Et4H2Tnk6+LpSXCE0Qs3VcsDoVs+vddEdgEDYrMB2qZunNrSG buWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OLzaeUS06+yhltBxU+w+m2N7paugxw/aDMH684EFsfI=; b=WJSsyGLAQCfcNJmemEmeUYw7Y2/GZmfPc8W6c1UeqBpwRWTrk5U4IgiESVDBjnkFLM iYmI9XU0TDzaKBXKS3XCjq/Yh3Bu61F3DSBzsPSHEvgAgcw+iJMb92FRG0BjDOemJjig JYoVW6z1B5Ivinr0sHATCEnA9FkcMzCeUcFtqcjy1Al2AnR1juloFTNKQGX4wdyDKL6f RBgqsX1mlWoEkk6+vqrv+cHp/PUfqejLElJGsZUjytEgYzlO6d4zRWO6szWy7NVdaObd 48P/bbuUr6K79RzICEgwXJIN1JRrbvsjN5q+M2oxlPG7cCXb1o/If7LU/9itNq903hVv 9GIg== X-Gm-Message-State: AJcUukcp5xpefhk9mMw2lmsRX0RoibFR2H7D3859UfPavO5la5Pz6PYE 67VG/dlnVNb36UIhVeJaVTJzwiIoG3E= X-Google-Smtp-Source: ALg8bN6i7CFrQ1QHG3/JyhZMeptEgmLann7j6labeeWiSnhgW+gBZjyQX9tkYWwZSz9Ssqo2HmM3pA== X-Received: by 2002:a5d:43d0:: with SMTP id v16mr20610766wrr.67.1548632850852; Sun, 27 Jan 2019 15:47:30 -0800 (PST) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id g21sm3642603wmd.0.2019.01.27.15.47.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 27 Jan 2019 15:47:30 -0800 (PST) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Sun, 27 Jan 2019 23:47:07 +0000 Message-Id: <20190127234707.27689-11-sw@jkqxz.net> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190127234707.27689-1-sw@jkqxz.net> References: <20190127234707.27689-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 11/11] vaapi_encode: Support limiting slice size X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- doc/encoders.texi | 4 +++ libavcodec/vaapi_encode.c | 57 ++++++++++++++++++++++++++++++++++++--- libavcodec/vaapi_encode.h | 9 ++++++- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/doc/encoders.texi b/doc/encoders.texi index b8322c4e95..07d9b46b2a 100644 --- a/doc/encoders.texi +++ b/doc/encoders.texi @@ -2855,6 +2855,10 @@ the stream, and requires driver support. The minimum interval between frames must not be smaller than this, and there may be problems if the maximum interval is more than a small multiple of it. +@item max_slice_bytes +Set the maximum number of bytes allowed in a single slice. Requires driver +support. Not limited by default. + @end table Each encoder also has its own specific options: diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index e5491fcd03..5326245003 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -316,14 +316,14 @@ static int vaapi_encode_issue(AVCodecContext *avctx, if (pic->nb_slices == 0) pic->nb_slices = ctx->nb_slices; if (pic->nb_slices > 0) { - int rounding; - pic->slices = av_mallocz_array(pic->nb_slices, sizeof(*pic->slices)); if (!pic->slices) { err = AVERROR(ENOMEM); goto fail; } - + } + if (pic->nb_slices > 0 && ctx->max_slice_bytes == 0) { + int rounding; for (i = 0; i < pic->nb_slices; i++) pic->slices[i].row_size = ctx->slice_size; @@ -435,6 +435,31 @@ static int vaapi_encode_issue(AVCodecContext *avctx, } #endif +#if VA_CHECK_VERSION(1, 0, 0) + if (ctx->max_slice_bytes > 0) { + struct { + VAEncMiscParameterBuffer misc; + VAEncMiscParameterMaxSliceSize slice; + } param = { + .misc = { + .type = VAEncMiscParameterTypeMaxSliceSize, + }, + .slice = { + // VAAPI wants this value in bits. + .max_slice_size = 8 * ctx->max_slice_bytes, + }, + }; + + err = vaapi_encode_make_param_buffer(avctx, pic, + VAEncMiscParameterBufferType, + (char*)¶m, sizeof(param)); + if (err < 0) + goto fail; + } +#else + av_assert0(ctx->max_slice_bytes == 0); +#endif + vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context, pic->input_surface); if (vas != VA_STATUS_SUCCESS) { @@ -1810,7 +1835,7 @@ static av_cold int vaapi_encode_init_slice_structure(AVCodecContext *avctx) ctx->slice_block_cols = (avctx->width + ctx->slice_block_width - 1) / ctx->slice_block_width; - if (avctx->slices <= 1) { + if (avctx->slices <= 1 && ctx->max_slice_bytes == 0) { ctx->nb_slices = 1; ctx->slice_size = ctx->slice_block_rows; return 0; @@ -1834,6 +1859,30 @@ static av_cold int vaapi_encode_init_slice_structure(AVCodecContext *avctx) return AVERROR(EINVAL); } + if (ctx->max_slice_bytes > 0) { +#if VA_CHECK_VERSION(1, 0, 0) + if (slice_structure & VA_ENC_SLICE_STRUCTURE_MAX_SLICE_SIZE) { + av_log(avctx, AV_LOG_VERBOSE, "Encoding pictures using " + "at most %d bytes per slice.\n", + ctx->max_slice_bytes); + // In this mode we supply only a single slice structure and + // no packed headers - the driver generates the headers for + // each slice itself. + ctx->nb_slices = 1; + ctx->desired_packed_headers &= ~VA_ENC_PACKED_HEADER_SLICE; + return 0; + } else { + av_log(avctx, AV_LOG_ERROR, "Driver does not support " + "encoding pictures with a slice size limit.\n"); + return AVERROR(EINVAL); + } +#else + av_log(avctx, AV_LOG_ERROR, "Slice size limiting is " + "not supported with this VAAPI version.\n"); + return AVERROR(EINVAL); +#endif + } + // For fixed-size slices currently we only support whole rows, making // rectangular slices. This could be extended to arbitrary runs of // blocks, but since slices tend to be a conformance requirement and diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index 054cdaafbb..15a9284eaf 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -191,6 +191,9 @@ typedef struct VAAPIEncodeContext { // framerate. AVRational vfr_max_fps; + // Requested maximum slice size in bytes. Ignored if zero. + unsigned int max_slice_bytes; + // Desired packed headers. unsigned int desired_packed_headers; @@ -441,7 +444,11 @@ int ff_vaapi_encode_close(AVCodecContext *avctx); { "b_depth", \ "Maximum B-frame reference depth", \ OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \ - { .i64 = 1 }, 1, INT_MAX, FLAGS } + { .i64 = 1 }, 1, INT_MAX, FLAGS }, \ + { "max_slice_bytes", \ + "Maximum number of bytes allowed in a single slice", \ + OFFSET(common.max_slice_bytes), AV_OPT_TYPE_INT, \ + { .i64 = 0 }, 0, INT_MAX, FLAGS } #define VAAPI_ENCODE_RC_MODE(name, desc) \ { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \