From patchwork Tue Jul 23 21:53:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Araz X-Patchwork-Id: 50720 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:a742:0:b0:482:c625:d099 with SMTP id f2csp2889262vqm; Tue, 23 Jul 2024 14:54:13 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUkDI/2kwVb/ozYxV/XyhV6736TgXYaChH/6q2X1FUrdmuaHi2K2Hxw2yvIvaTCkKM+MUT9/nlayEWanxFc3Sk6ltZcPDq6Pu77+A== X-Google-Smtp-Source: AGHT+IGVi2dmv+MKYzUs/XbxxgK50aMDw3CDd3ribz4hz5w8rJG/dKM8fGgvLyvUKz3YyOcqpvv1 X-Received: by 2002:a17:907:7209:b0:a77:c043:5b5a with SMTP id a640c23a62f3a-a7ab0e24457mr13597066b.39.1721771653500; Tue, 23 Jul 2024 14:54:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721771653; cv=none; d=google.com; s=arc-20160816; b=JcBdp2pQwlAF2bIjHgYlB0Yu5FLd1MFEjp5Xtgp3T/wTW/djwKWt1UzCLmtyIP6yI3 U92FWkIFyop/vMWholzLT650VvVSe/BnjU7nokxO89e+4H1VFmnFu3JayqaYMTixRGGx tBDOblYxbzlyI3BP7Qz+trOZM4chV1qbQ1yLbDEvamsPn1Z1sFTq0Nfh9qLKUphXQ9Z4 01siT21uZPyXmA7Ukg9NIjMkcNyl0flByhqT1sGi2OjAfcu/7QdgztprDLYBbNb+98oE 8dkOzIi9wntSe9feg+zl4+dyYZIKP56gelztaM7yWGB7OobWpFkZ89Ha6wJQoY72ZcDT VrKA== 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:message-id:date:to:from :dkim-signature:delivered-to; bh=VMr8cAp6/A+acY95u4M7qvHJhPGwjWL3GTkNT+EZ4bY=; fh=OWLPUf18s9dlUQuxhl6xLimy4NybloePU6lVbhxT1Ck=; b=QTpuk9gxCx3DZY5r1ElT6l4k8EQH1KSa+7YFaEHattbbwAEd2LVQ8CdEMNE+o0+sCM pVCBK+4g2D81scbHmk7axfga7y2fIFgqBwEBtDxW29IfdGE6vLAgIFhmYJVuO5rKgEkd ky6PJUDMfi2BpZg7bmS3V1AGXiDc7KREzd6DDi1uHQs3DL6WxsOTmV+7gpx1ArEmEOFQ cfrH2YAeH+kk9LecH5athx1WivHKLAwW6WnFNsMWJyKoQKEC44WfOBvs/Hhp3tJ0l3sx nvAC5paM4ZYCrkIuHkRXASWXk4tDJvvVt5DUUAUN3/EztV3RR/5eK3By2RIc/boOezDw 3lIw==; 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=ALkq2o51; 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; dara=fail header.i=@gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a640c23a62f3a-a7a3c95129fsi583929566b.737.2024.07.23.14.54.12; Tue, 23 Jul 2024 14:54:13 -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=ALkq2o51; 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; dara=fail header.i=@gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A7AE068D601; Wed, 24 Jul 2024 00:54:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6BC5D68D218 for ; Wed, 24 Jul 2024 00:54:02 +0300 (EEST) Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-3685a5e7d3cso3406676f8f.1 for ; Tue, 23 Jul 2024 14:54:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721771641; x=1722376441; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=abtPFMZC39DGwBm0+6Ak7/RNb0HSssd2oFIji6YRHCQ=; b=ALkq2o51c2ECHHb8vN42GVs3Dlu8AFj9sXxjQX1Ms/Hsyt6xuNbeIkZERzryI9aoez nETTf5WSMBJKCb7YRH5MbLOTHx5yxEVhhhy8x4bwgcUKCD200QyndPPyXCpMyHzfBJEk Xdjfg0g7aKBXotBFJ6/5IKg8+r9UAPZmUNkt59MOueBswujmcH9Jz4ReGwOZPaM2cdjx GbmakAc8rPuopGvJvYh5yBMq8rSwPxu0rRAF9YuxTRe5JEnmL5uaaDThBQkzt26JZ5NN cm9dZtBR9qpx6cbcuTUfIJ9OLUIh5kHN8+pnwgwXlxFssBKJXp1u62UfKfCCrwNlFMI2 l43Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721771641; x=1722376441; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=abtPFMZC39DGwBm0+6Ak7/RNb0HSssd2oFIji6YRHCQ=; b=W8nlFCMLdVN1LbQHm0JUMSJeb2k89+cAm9PrLUtCEcvwNWeyeAUw8e0AvaDL9MZlET nsKtEorRHWT2FKXUPaaraVbHWlu5ikukONdvbuX6ZOUs0Za71ieB9iUIk+pcqrNcHqYS b5D/wDcNUwieEnz+A/MaFlb3VKPu2nxC7ruGo3q13RDE+bVvQwfYen2D06B9Ii7nnmlm NGnezucsnMGWlsvf+IoT6gHYWdtjg58n2jLf51AhGHF7Vurt5Dv5pW+80TVRyjDHzNYV B4sBfa3SCH+HQyyaBP+4WntW7cLH5KZ1Q7B1tYjQRJ8zSoQ4Q2JxQ+4iZJDwosg3kB4n t42w== X-Gm-Message-State: AOJu0Yzv1BUDgszRLO/l+v2kLy5yTcb3i9NF1HA0A4dG73mPyHh0bMrs hHGOyu4DJK8Wu5q9pXTAhS7G2SsESau6c2ISCj3+rahdDeC5GIhxkjEdNGhl0O4= X-Received: by 2002:adf:e74a:0:b0:367:8fee:443b with SMTP id ffacd0b85a97d-369f5b8104emr101299f8f.41.1721771641158; Tue, 23 Jul 2024 14:54:01 -0700 (PDT) Received: from aiusubov.amd.com ([87.116.132.31]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36878684839sm12577295f8f.22.2024.07.23.14.54.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 14:54:00 -0700 (PDT) From: Araz Iusubov X-Google-Original-From: Araz Iusubov To: ffmpeg-devel@ffmpeg.org Date: Tue, 23 Jul 2024 23:53:56 +0200 Message-ID: <20240723215356.1737-1-Primeadvice@gmail.com> X-Mailer: git-send-email 2.45.2.windows.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/amf_enc: av1 cropping support 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: Araz Iusubov Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: fgO7EFVSvRKb --- libavcodec/amfenc_av1.c | 82 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/libavcodec/amfenc_av1.c b/libavcodec/amfenc_av1.c index d40c71cb33..27599b9fbe 100644 --- a/libavcodec/amfenc_av1.c +++ b/libavcodec/amfenc_av1.c @@ -22,6 +22,9 @@ #include "amfenc.h" #include "codec_internal.h" +#define AMF_VIDEO_ENCODER_AV1_CAP_WIDTH_ALIGNMENT_FACTOR_LOCAL L"Av1WidthAlignmentFactor" // amf_int64; default = 1 +#define AMF_VIDEO_ENCODER_AV1_CAP_HEIGHT_ALIGNMENT_FACTOR_LOCAL L"Av1HeightAlignmentFactor" // amf_int64; default = 1 + #define OFFSET(x) offsetof(AmfContext, x) #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { @@ -167,7 +170,13 @@ static av_cold int amf_encode_init_av1(AVCodecContext* avctx) AMFRate framerate; AMFSize framesize = AMFConstructSize(avctx->width, avctx->height); - + //for av1 alignment and crop + AVPacketSideData* sd_crop = NULL; + uint32_t* crop = NULL; + uint32_t crop_right = 0; + uint32_t crop_bottom = 0; + int width_alignment_factor = -1; + int height_alignment_factor = -1; if (avctx->framerate.num > 0 && avctx->framerate.den > 0) { framerate = AMFConstructRate(avctx->framerate.num, avctx->framerate.den); @@ -482,6 +491,77 @@ FF_ENABLE_DEPRECATION_WARNINGS buffer->pVtbl->Release(buffer); var.pInterface->pVtbl->Release(var.pInterface); + //processing crop informaiton according to alignment + if (ctx->encoder->pVtbl->GetProperty(ctx->encoder, AMF_VIDEO_ENCODER_AV1_CAP_WIDTH_ALIGNMENT_FACTOR_LOCAL, &var) != AMF_OK) + { + // assume older driver and Navi3x + width_alignment_factor = 64; + } + else + { + width_alignment_factor = (int)var.int64Value; + } + + if (ctx->encoder->pVtbl->GetProperty(ctx->encoder, AMF_VIDEO_ENCODER_AV1_CAP_HEIGHT_ALIGNMENT_FACTOR_LOCAL, &var) != AMF_OK) + { + // assume older driver and Navi3x + height_alignment_factor = 16; + } + else + { + height_alignment_factor = (int)var.int64Value; + } + + if (width_alignment_factor != -1 && height_alignment_factor != -1) + { + if (avctx->width % width_alignment_factor != 0) + { + crop_right = width_alignment_factor - (avctx->width & (width_alignment_factor - 1)); + } + + if (avctx->height % height_alignment_factor != 0) + { + crop_bottom = height_alignment_factor - (avctx->height & (height_alignment_factor - 1)); + } + + //There is specia processing for crop_bottom equal to 8 in hardware + if (crop_bottom == 8) + { + crop_bottom = 2; + } + } + + if (crop_right != 0 || crop_bottom != 0) + { + sd_crop = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*sd_crop)); + if (!sd_crop) + { + av_log(ctx, AV_LOG_WARNING, "Can't allocate memory for amf av1 encoder crop information\n"); + return AVERROR(ENOMEM); + } + + sd_crop->data = (uint8_t*)av_mallocz(sizeof(uint32_t) * 4); + if (!sd_crop->data) + { + av_log(ctx, AV_LOG_WARNING, "Can't allocate memory for amf av1 encoder crop information\n"); + return AVERROR(ENOMEM); + } + + crop = (uint32_t*)sd_crop->data; + + //top, bottom, left,right + *crop++ = 0; + *crop++ = crop_bottom; + *crop++ = 0; + *crop = crop_right; + + avctx->nb_coded_side_data++; + + avctx->coded_side_data = sd_crop; + avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_FRAME_CROPPING; + avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = sizeof(uint32_t) * 4; + } + return 0; }