From patchwork Thu Jul 25 12:06:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Araz X-Patchwork-Id: 50726 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:cc0a:0:b0:482:c625:d099 with SMTP id h10csp495526vqv; Thu, 25 Jul 2024 05:06:35 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVceQTjYWr/7ki2jpEi/MQ62AZisjJqwdxd368AyKyTnhlCGajdfNBzNzxW57nNOPxstOEpTFl5fnvhUW1iE32onolcor726wyt6A== X-Google-Smtp-Source: AGHT+IHUtdVLDHVlu7V5TZJF5+45Fa/z5Om1Gxop9EoRbhJaTVCDoNW3rC2Bop/P/u6M1D6q5gWc X-Received: by 2002:a50:9eae:0:b0:5a0:d754:5ec5 with SMTP id 4fb4d7f45d1cf-5ac2c1c432cmr1637203a12.26.1721909195005; Thu, 25 Jul 2024 05:06:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721909194; cv=none; d=google.com; s=arc-20160816; b=Z1dGwGkRAbvBSjXL4CgqlxU3Epxj7ieko1ih+euMpaAqFIH5lRu0e7HA7cdz87q59Q kjggY0Oq9/oz0VT94vN7FtrXBmuAaL5dipX8YwN/kiWUsgXtB+VTaDaTrj1rQveFb3WP JorEGfLuSKq6olWJq32mi7jKswNlaGgsmPNJIz3VOaD4WE/Uc64SoOhtDgOMGj3EDpLt R5VhcTDJVZvfQ+EVSpKj6efHXe3FpUXAvhXwi94QBwdgWGcDjFgkybOCkKzSJT44C4aG 2ecUUzXMBDzmHKfbQ05ObxOFdqr1KzyxVxh93nbhG3uMi94R4bLF/n4M0RZTuIbgP+Cm 8X0A== 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=fCUWR+2jFmiHfPygOmjhXm/EGzTRjyCdWwDfpxCaBYE=; fh=OWLPUf18s9dlUQuxhl6xLimy4NybloePU6lVbhxT1Ck=; b=A7JJDwPPnJYagSIZOWgHtuqwHgU84d0wJxOpJFspzx7gwpfcWz5sMjvxbENqVwiKNb zrgL+lzqYyTkU/vtfba2JLZoyhVi5WBthAK3zMHP+WOysZUslRXJElh6WHK/pWbsI1lw 3Z0mlR9x2ulj2FLM/Aovj9bysiERxFYBWMy7k7aFgxRxQz/fz1X4ROiPIyQarK1Sz22i 9WWZ0PF7msveV/DVsH23pXl4mHrww4XU7s3X1Y2Cq8+7Yuq5XXTwDUtXWUbj3TFSamAd dvLCDbTPNWVJ1afumjmYvkdVq+LoXu61yudf5DNTTKX0K/D+sPq1zPYBye+Xxb2VUZQA wSIg==; 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=OwBmfXPE; 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 4fb4d7f45d1cf-5ac6339f393si918178a12.32.2024.07.25.05.06.34; Thu, 25 Jul 2024 05:06:34 -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=OwBmfXPE; 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 8E13868D41F; Thu, 25 Jul 2024 15:06:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8B9A368D076 for ; Thu, 25 Jul 2024 15:06:23 +0300 (EEST) Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4280812ca01so2921095e9.1 for ; Thu, 25 Jul 2024 05:06:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721909182; x=1722513982; 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=gn8nnGojG2njzwHfZMLSIDg/+DAKdI0qys4utrCvbOU=; b=OwBmfXPE4mGdIhPkW9ttE9XIbz8D3Sk4VdYAVam5KRXcxxB1qA6WVp8jCt3edXGN9Z Fw/ueFdxXD5gxYT24wta/0zm2KivcF5rXAhcfh55RtndeEyPZIdmxgfpgM3XwXoFvpD2 Z/HogylJ+enWinKP1H+vzcvVTyLM1WSui46tk1RuFnilWu9wCW7NwB/nYHjlzTiQCfJj aS5vdxrdLbR2e4+Svhqh0bAN6cii2iSDk1Y8yz/cEYCPnRIuOAN0XUbFp7N+RRcmGdBG WAnpOw3P52kyP7bmfM8A5eLnvLf5PXeul4QAT4m1wOcl7l4+PsbdvPrXMH3Zt/BMBVJm ckXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721909182; x=1722513982; 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=gn8nnGojG2njzwHfZMLSIDg/+DAKdI0qys4utrCvbOU=; b=TJilSOOk5MWW+x35INLJZctbnRSL58EOiy9f2GLlBNMkFUSwsLcZiBNAtRH+F+dKuW vGIKrvb1z4EcmcmR5XvK2OJctssCmwB42uXBtDKvJ4yfvoglew6UGowUgOlx6nXLFtid a+DcjV8IvOehLj0Ouf7Czwi2FvJj0RE0kdoIMIdBSh5YSYo+JNzSFQ5VHAjMRGNf4Oy7 M0weujvs9nz5F+ujQxsRglyc11sUq9Cfm0jy1KtNRJC/qbICDbHUEIuBof19utJq6xeb ElKPkjz9VHqPwwG65dAUbICRgFQQ7gDp4wUuygtJBWetUmLqcDH57ckdWR0PiOWfhBbh /CdQ== X-Gm-Message-State: AOJu0Ywh1L9UHbJuuJFHztU2l4MuhRlVuuOJfNA5ZaTFcPjU7vH1NxZA AVAAUh44Rn4aIBi/+WEpiSbGtJ6O+ooDSePnm4f2wk2SO72XvT4H+wkJy3xT2bo= X-Received: by 2002:a05:600c:1547:b0:426:64a2:5362 with SMTP id 5b1f17b1804b1-4280546e17bmr15306755e9.8.1721909182337; Thu, 25 Jul 2024 05:06:22 -0700 (PDT) Received: from aiusubov.amd.com ([87.116.132.31]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427f9413547sm74370335e9.44.2024.07.25.05.06.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jul 2024 05:06:22 -0700 (PDT) From: Araz Iusubov X-Google-Original-From: Araz Iusubov To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Jul 2024 14:06:16 +0200 Message-ID: <20240725120616.785-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: Scz0jj+TSyEV --- libavcodec/amfenc.c | 6 ++++ libavcodec/amfenc.h | 3 ++ libavcodec/amfenc_av1.c | 64 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index 061859f85c..93925854e0 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -402,6 +402,12 @@ int av_cold ff_amf_encode_close(AVCodecContext *avctx) dlclose(ctx->library); ctx->library = NULL; } + + if (ctx->crop) + { + av_freep(ctx->crop); + } + ctx->trace = NULL; ctx->debug = NULL; ctx->factory = NULL; diff --git a/libavcodec/amfenc.h b/libavcodec/amfenc.h index 2dbd378ef8..abcf465063 100644 --- a/libavcodec/amfenc.h +++ b/libavcodec/amfenc.h @@ -81,6 +81,9 @@ typedef struct AmfContext { int log_to_dbg; + //handle crop + uint32_t *crop; + // Static options, have to be set before Init() call int usage; int profile; diff --git a/libavcodec/amfenc_av1.c b/libavcodec/amfenc_av1.c index d40c71cb33..e9bc26a770 100644 --- a/libavcodec/amfenc_av1.c +++ b/libavcodec/amfenc_av1.c @@ -19,9 +19,13 @@ #include "libavutil/internal.h" #include "libavutil/mem.h" #include "libavutil/opt.h" +#include "libavutil/intreadwrite.h" #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 +171,12 @@ 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_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,59 @@ 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) { + ctx->crop = av_mallocz(sizeof(uint32_t) * 4); + if (!ctx->crop) { + av_log(ctx, AV_LOG_WARNING, "Can't allocate memory for amf av1 encoder crop information\n"); + return AVERROR(ENOMEM); + } + + 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"); + av_freep(ctx->crop); + return AVERROR(ENOMEM); + } + + avctx->coded_side_data = sd_crop; + avctx->nb_coded_side_data++; + + //top, bottom, left,right + AV_WL32(ctx->crop + 0, 0); + AV_WL32(ctx->crop + 1, crop_bottom); + AV_WL32(ctx->crop + 2, 0); + AV_WL32(ctx->crop + 3, crop_right); + + 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].data = (uint8_t*)ctx->crop; + avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = sizeof(uint32_t) * 4; + } + return 0; }