From patchwork Thu May 23 07:08:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 49164 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:542:0:b0:460:55fa:d5ed with SMTP id 63csp844182vqf; Thu, 23 May 2024 00:09:33 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXFOC74BeEQdjzhJwrokp6Rt7DYoRogIpbJCvZDRRepd3Uqb8kNU+9xom6I8pJ19rNMdR3B4yVwxTRVLtHgt+BujersqGhg2PWZQA== X-Google-Smtp-Source: AGHT+IFB7XD/cp+GCWjpYx11exjO8CpaQUGv82VJUtLyFMBsdJ3ZD8CH4wyvcCj/uVB0luNPgTXB X-Received: by 2002:a17:906:1854:b0:a62:2b65:1dd5 with SMTP id a640c23a62f3a-a622b65233cmr233021966b.58.1716448173127; Thu, 23 May 2024 00:09:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1716448173; cv=none; d=google.com; s=arc-20160816; b=Na7JYSpt4lzlBjHWLTOzFN7XgyqQTiFwMuiUGXgxs1sCjQj/p2qgVxUOzkpnaplLxC 3OK9W7enjDOsSmoaFMu7+C1brhYH2ojsHJef28/9Qht4yIHjUGJPY7yxmz7CdF2eqdqY kZWJ8yDqUCi1pW19zu6WkBqEhziLoBiesQsqx1LAtrcEb3YJq8yCVjJ5r2pAd9bOUpK9 PmX7kdVEUUpPvGzVclOF2XwHzKKyUsyg3ivsMQICL6BiUfcWV8YqoKtawF8VuriPda+U mrgSh/GFqCllsSFv+9SZrTUm7moBqvmNGfaOPz+/7kudXy90/uQIG3MO9oI1MBA7qqVE IsOg== 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:date:to:from:message-id :dkim-signature:delivered-to; bh=iiAHY22EUtKvaTYBdJchKgs9ULDi2hm7Mb98ZTqs1p0=; fh=HnHYuZ9XgUo86ZRXTLWWmQxhslYEI9B9taZ5X1DLFfc=; b=X22LeklC240wZ0U2xcNU+ce0Wk3iG9HsIb6SPoLrIEZtiWLXwq9+MOs7mdoZDcehZM 5GkiyVAy9IYWu94VCNwaY6S4mhU2PUhfzs4DUrh4TwBpBE9nSds9miGZHuxcob7OMeko E6GwhY9JDEfrnHc9/cgN5t64uIqW82egSJ4RiayzU175ZMdqCnCMjbv1fdaiX1PxvdRL N+1V1/k1PWp331FQjJP151DWRruApMjs0O1GhEkhp/W0SiB+weo25r+17d87JLnoxIGl YmVKwbyRR+Zo2o5xkEHKdrPXiUSdDQcMJI1AbSsJH4/n84TAP68kO8SX8U6YFe9NV51m xMBg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=X1dyf6f9; 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=NONE dis=NONE) header.from=foxmail.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-a61a82bfc84si379446466b.280.2024.05.23.00.09.32; Thu, 23 May 2024 00:09:33 -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=@foxmail.com header.s=s201512 header.b=X1dyf6f9; 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=NONE dis=NONE) header.from=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 31D9268D3E3; Thu, 23 May 2024 10:09:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-210.mail.qq.com (out203-205-221-210.mail.qq.com [203.205.221.210]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4838668D2B3 for ; Thu, 23 May 2024 10:09:09 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1716448139; bh=5JjFKdHQT8UBsSc7YJEhW9o1xuSaJdfIUKLBMaF5kgM=; h=From:To:Cc:Subject:Date; b=X1dyf6f9ZT2HcS2ciCXz/0sHpV7i6koscF8F8aODq0eePbD37gGYORBzXm8/twLYN qX3hWroTGEGEHGfrbYSCet6smcROdJCDboQhtYOVtzFCCn65IzyFXLoFCgXR7l6wXD 0i1yXV9aj6E2UDWjfUr8bNWDopahUNxrCJrI9kBM= Received: from ZHILIZHAO-MB1.tencent.com ([119.147.10.190]) by newxmesmtplogicsvrszb9-0.qq.com (NewEsmtp) with SMTP id 23A264EF; Thu, 23 May 2024 15:08:58 +0800 X-QQ-mid: xmsmtpt1716448138t08unfzue Message-ID: X-QQ-XMAILINFO: NkBK3x0tNc75cfUNBAogeo+mMzNM4gax/EOv1Uc2zWNaMT1hJ5hhIh5xLGgb7e q90GsUIlPGPCYgVf2geGt6PSX6FdeVXg2A4WsW3LWVSuBf0d417ZOU+4RiyO5xcBFjDnszCjnb4K GPfSjY8VtWG2m6tTlNL87auQuw/kcp1q4ALGOUVI6uCgudhOEsYH7zeleFd4s883A0HD4KHKIIEj 2tPjmBmsZY64H+Ce3r1+YZxp4h4V5zukuooXprVtiFsoAif7vbyj+Bm9AGw7AF1VLYS1XWSbrM5b kKo8t/7yY/Wght7kpobC0UQEbkWzVU2jj/PGgSpK5braA1Q9KWwNwFVRY+GyxJzXEkLo/Iydrb0o Wl5rZve/dWqBU0dqwFFcknlWQimZ9hwMsPGPo1O8Fv+97M8HSbqmQDT/H/WW+EL/2oMlPa/xTZ9K LSajQR1e9A4sYKjMjz0mkSKt75MP9Ag4EZUfSacTTy/T09nb8oeinGh4T/9LJFBBO9tVI27oy+cH nYfbK4ipnjrJAyQNnQ3D4hPhMua9Uccyuon/youQrhWHYFoGZPdiF9shuVqgpiTd9ePRvMUkCHLX kHpuh7TQK9rZFs/zhUJZ1YbnpmFFRK9o1WwtGa4v0rHaU7eR/4EKrdArMuF6BtGygtJwE0J3EZQs uEaZlE86X45qXPGu9k9fOHG1ExgVEhpSEanZP2u42nWRyrnkgb3j7PC8L8FjB1xa0GVBfP4j8lor egGlqurqO7Faq4OAby71n4tGVQv1lO85c52+MnEYdmDlYSDjhKDxzfKRpU/480/cpjL0jbAEapQM AvtIwzsOdFQVbFBXY3ZfHM+eYq+5Rxje3xG82D/BfWMXuoTJHF3bAELV5ozTUlXVQZtkY4IkqGew O+85bi5uwqXi4W8WwOZSNkt278Rij433uH3maQrDuEyY01dv5ff8J84t2Oh3ppsGXtjWGo/yixgr zsrj6EJ1JMGU1p2DVJA8PC1d83Snh088f9ZDca7T2yqSaWT5PvKKDvpJc1phip X-QQ-XMRINFO: MSVp+SPm3vtS1Vd6Y4Mggwc= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 May 2024 15:08:54 +0800 X-OQ-MSGID: <20240523070856.60655-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] avcodec/h265_metadata: Add options to set width/height after crop 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: CKQvSjhzTnoD From: Zhao Zhili It's a common usecase to request a video size after crop. Before this patch, user must know the video size before crop, then set crop_right/crop_bottom accordingly. Since HEVC can have different CTU size, it's not easy to get/deduce the video size before crop. With the new width/height options, there is no such requirement. Signed-off-by: Zhao Zhili --- doc/bitstream_filters.texi | 4 ++ libavcodec/bsf/h265_metadata.c | 101 ++++++++++++++++++++++++++++++++- libavcodec/version.h | 2 +- 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index 3d4dda04fc..c03f04f858 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -456,6 +456,10 @@ will replace the current ones if the stream is already cropped. These fields are set in pixels. Note that some sizes may not be representable if the chroma is subsampled (H.265 section 7.4.3.2.1). +@item width +@item height +Set width and height after crop. + @item level Set the level in the VPS and SPS. See H.265 section A.4 and tables A.6 and A.7. diff --git a/libavcodec/bsf/h265_metadata.c b/libavcodec/bsf/h265_metadata.c index c9e1cc3eed..eba00c20d5 100644 --- a/libavcodec/bsf/h265_metadata.c +++ b/libavcodec/bsf/h265_metadata.c @@ -58,6 +58,8 @@ typedef struct H265MetadataContext { int crop_right; int crop_top; int crop_bottom; + int width; + int height; int level; int level_guess; @@ -187,12 +189,94 @@ static int h265_metadata_update_vps(AVBSFContext *bsf, return 0; } +static int h265_metadata_deduce_crop(AVBSFContext *bsf, const H265RawSPS *sps, + int *crop_left, int *crop_right, + int *crop_top, int *crop_bottom) +{ + const H265MetadataContext *ctx = bsf->priv_data; + int left = ctx->crop_left; + int right = ctx->crop_right; + int top = ctx->crop_top; + int bottom = ctx->crop_bottom; + + if (ctx->width > 0) { + if (ctx->width > sps->pic_width_in_luma_samples) { + av_log(bsf, AV_LOG_ERROR, + "The width option value %d is larger than picture width %d\n", + ctx->width, sps->pic_width_in_luma_samples); + return AVERROR(EINVAL); + } + + if (left < 0) { + if (right > 0) + left = sps->pic_width_in_luma_samples - ctx->width - right; + else + left = 0; + } + + if (right < 0) + right = sps->pic_width_in_luma_samples - ctx->width - left; + + if (left < 0 || right < 0 || (left + right + ctx->width) != + sps->pic_width_in_luma_samples) { + av_log(bsf, AV_LOG_ERROR, + "Invalid value for crop_left %d, crop_right %d, width after " + "crop %d, with picture width %d\n", + ctx->crop_left, ctx->crop_right, ctx->width, + sps->pic_width_in_luma_samples); + return AVERROR(EINVAL); + } + } + + if (ctx->height > 0) { + if (ctx->height > sps->pic_height_in_luma_samples) { + av_log(bsf, AV_LOG_ERROR, + "The height option value %d is larger than picture height %d\n", + ctx->height, sps->pic_height_in_luma_samples); + return AVERROR(EINVAL); + } + + if (top < 0) { + if (bottom > 0) + top = sps->pic_height_in_luma_samples - ctx->height - bottom; + else + top = 0; + } + + if (bottom < 0) + bottom = sps->pic_height_in_luma_samples - ctx->height - top; + + if (top < 0 || bottom < 0 || (top + bottom + ctx->height) != + sps->pic_height_in_luma_samples) { + av_log(bsf, AV_LOG_ERROR, + "Invalid value for crop_top %d, crop_bottom %d, height after " + "crop %d, with picture height %d\n", + ctx->crop_top, ctx->crop_bottom, ctx->height, + sps->pic_height_in_luma_samples); + return AVERROR(EINVAL); + } + } + + *crop_left = left; + *crop_right = right; + *crop_top = top; + *crop_bottom = bottom; + + return 0; +} + static int h265_metadata_update_sps(AVBSFContext *bsf, H265RawSPS *sps) { H265MetadataContext *ctx = bsf->priv_data; int need_vui = 0; int crop_unit_x, crop_unit_y; + /* Use local variables to avoid modifying context fields in case of video + * resolution changed. Crop doesn't work well with resolution change, this + * is the best we can do. + */ + int crop_left, crop_right, crop_top, crop_bottom; + int ret; if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) { int num, den, i; @@ -289,6 +373,11 @@ static int h265_metadata_update_sps(AVBSFContext *bsf, } } + ret = h265_metadata_deduce_crop(bsf, sps, &crop_left, &crop_right, + &crop_top, &crop_bottom); + if (ret < 0) + return ret; + if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) { crop_unit_x = 1; crop_unit_y = 1; @@ -297,14 +386,14 @@ static int h265_metadata_update_sps(AVBSFContext *bsf, crop_unit_y = 1 + (sps->chroma_format_idc < 2); } #define CROP(border, unit) do { \ - if (ctx->crop_ ## border >= 0) { \ - if (ctx->crop_ ## border % unit != 0) { \ + if (crop_ ## border >= 0) { \ + if (crop_ ## border % unit != 0) { \ av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \ "must be a multiple of %d.\n", #border, unit); \ return AVERROR(EINVAL); \ } \ sps->conf_win_ ## border ## _offset = \ - ctx->crop_ ## border / unit; \ + crop_ ## border / unit; \ sps->conformance_window_flag = 1; \ } \ } while (0) @@ -453,6 +542,12 @@ static const AVOption h265_metadata_options[] = { { "crop_bottom", "Set bottom border crop offset", OFFSET(crop_bottom), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS }, + { "width", "Set width after crop", + OFFSET(width), AV_OPT_TYPE_INT, + { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS }, + { "height", "Set height after crop", + OFFSET(height), AV_OPT_TYPE_INT, + { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS }, { "level", "Set level (tables A.6 and A.7)", OFFSET(level), AV_OPT_TYPE_INT, diff --git a/libavcodec/version.h b/libavcodec/version.h index 3d2de546b3..f9b09b4b25 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -30,7 +30,7 @@ #include "version_major.h" #define LIBAVCODEC_VERSION_MINOR 5 -#define LIBAVCODEC_VERSION_MICRO 104 +#define LIBAVCODEC_VERSION_MICRO 105 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \ From patchwork Thu May 23 07:08:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 49165 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:542:0:b0:460:55fa:d5ed with SMTP id 63csp844282vqf; Thu, 23 May 2024 00:09:43 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXqCG/rYkHRh2sQlMI+qM0YfC5qa2SwP3+ooBUZVzk5VAJZqe+TNkdQwGgoiTtFjelxW0+gf5wRQQyGkyPT8lzhfLAeF2+zy7fdtA== X-Google-Smtp-Source: AGHT+IFUIGobs7HZcFEYkeXvxd6DTerLEKwis5Mk9XogBMnXnX4embeSb7uSLKNdBIQYZpsSHsBA X-Received: by 2002:a17:906:7fcb:b0:a5a:89cf:489a with SMTP id a640c23a62f3a-a62281a3776mr289864466b.4.1716448182857; Thu, 23 May 2024 00:09:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1716448182; cv=none; d=google.com; s=arc-20160816; b=d+v4XVvwu2k4cm5iwuPEg+V2+Fqyl8YAeEVY5TXk0o3tQMu1SMEScX/P0/UvjEE9/O q5Msi+9DNPBDz/m9DDtvNquTGYy+NkWZi0EH7LMvG45RDp/bvP1pdmASXD8o0rnPvvn4 sXyiBPzx0tv1ZL3tM44ypOleZ9rrtMf3UTwbucfFlWWBEopMSFQUYdowCw1o2FNjpBxV ggSZISS8d2lvQypgu+AHWdmcgVXFT2MYHYg57EpZ3+wGkUiM42CApp7VYikTmDgOx4kb gySBhqKaLhGWCziCxtwZeXJRyLvvQCldF3NIHOBFi3HmRT6a9yj0bxxlglDRkA9VPKSB cVnA== 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:date :to:from:message-id:dkim-signature:delivered-to; bh=3PYmOrV2Ajrqa07O+Gl5rm5RU/uvc6cNlkJjRmx6dUY=; fh=HnHYuZ9XgUo86ZRXTLWWmQxhslYEI9B9taZ5X1DLFfc=; b=Gm6/hP8FnUE9u2gcmrpZuOHDWPKf6rf2nXxZlaBNxT1lOPXrs2t44StjC3HDqdj837 81Rv1G2EpSWdZAKsq3sLh4OG67F5wrICUmgsCcMzB3HWFtkOjBTNeLeJLmJvHNE+2gt1 t9bELFZ7xnkAQNGPsHCsHbXcANYUBnLtnXxE+2MGJGFHEsCNqYVWGwdzLI/b6d3cn54/ prqFxX7HMRcGUZWEiJBGnzEGPODRweITH/fsP/8kJH8oAMwUWu4OV/SThb7N2NOfw4sK HsrwYobfS3tmcl9adywcExHuvOM+k5i8EBEtDCmjPtQuqLC/nuWOhXzXCChrS1hz/zD6 44KQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=cr6094ZF; 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=NONE dis=NONE) header.from=foxmail.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-a5a17be63a1si1584562266b.709.2024.05.23.00.09.42; Thu, 23 May 2024 00:09:42 -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=@foxmail.com header.s=s201512 header.b=cr6094ZF; 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=NONE dis=NONE) header.from=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 69C9668D431; Thu, 23 May 2024 10:09:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-242.mail.qq.com (out203-205-221-242.mail.qq.com [203.205.221.242]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4F30E68D2D4 for ; Thu, 23 May 2024 10:09:09 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1716448139; bh=c+YdNQHK5gm6BtBFOtF0Qx/yKeRl4L/SMFzhECV9BdY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cr6094ZFe5I3wqotrz44I1xjg/SAluH0MZAmD/NYV3aBYRjs2PbuVcjJWpbknQ68i 2pkvI7Q13RzUnkmT6Fd1IXRLZko7iUt5sWldCZfrz1yiUXM8OYTMHn6LxPrk0p3Gqx lzeea2dQSTBlgMjR7nnCr3mtHsHrj3Qazvzm/IzE= Received: from ZHILIZHAO-MB1.tencent.com ([119.147.10.190]) by newxmesmtplogicsvrszb9-0.qq.com (NewEsmtp) with SMTP id 23A264EF; Thu, 23 May 2024 15:08:58 +0800 X-QQ-mid: xmsmtpt1716448139tecv07pfk Message-ID: X-QQ-XMAILINFO: NQR8mRxMnur91gIxrQVtefSx1FYZrfTcW3mwmUqQODtuhLoGTcTTAqC0lRKQmj sBOU+zP3V/wAFhfnN54y1eeLj2j2tULHxYLYKc72oEqpQb5MFiZ9OyksCkPF6l+AaAyzzdWxHwCC A2LMyBwzVdNEBUM66FBMcpSrtDCYjbJUezuntISB4zliEMJ390rvvkpX1iOgORNZfbki9nqgIa0E Sf1fnQrZqitDD1khh/nVM1koxcMyRMBD4Gg/QNUPHfezWvXPbTW9ekOjCkrIa9plKJOtgW7isQrI vZzkno0byFhZEuqVUaoDdE2WkP6eNaYv4ri0TU1RAIF8xM/iaj3tPEC40FEfUAOh7f3nlImdmGYr uPjutGTJhoJEs591V3EH9d5i9G6RXaVITeCJbJ5ODx8XCnZj6x1aQCqTYGcme26XDKvaqFMPYpda 6am6ClFhzkjVFMy35rveJbfPj81KHkDFNzLt/Kko451mDbhV862B8iBdqQPelLO89FgKwZ1Z5EpS NIsCx5mTjOcXbxCCCFFJ/LFq+K8zQMG+a/uo5mO2nk+9pWvGOConb7Cu8mkM5TW5heoPjJreshs4 gMlPdlo+9jVKGQur5ZC/5OGBjxxpqX6fUn+VM2XNzCLfaRtVTMjSuDF2o+ZCMwi/vyqZglLj1jIf 4ogRmNEylKLejXD2eNdoJFxsAe+HhUR5PuWs6WdLHwB9aiyEXOe81accV22VEicxuBdLYeECZY3r 4xfgq3lCuZFJ6KKl5gdBlfNoG+obLc8KJJHP581yg2LcCHM5hYHFVeVGYPiNVivQy51ORKFq8gVc hPUgW+/rteZtbYLPHTBDzzBnbNuP4qbperZni0xKgOD+Me/BaoHnVB1ihSosczUT5brJsM3EMk4+ jOqp+Esg/5B+BvqcahQ5nLDSsZcyWqXu9tizwgt7CDoI/oDQqVCHbnQDr4rfeNacqFFXYhD3zf6E c5mPohKS8Ckdve87FIkCAE04OVcBpzs6j2YEafNzdMTHw2zw4HSg== X-QQ-XMRINFO: NI4Ajvh11aEj8Xl/2s1/T8w= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 May 2024 15:08:55 +0800 X-OQ-MSGID: <20240523070856.60655-2-quinkblack@foxmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240523070856.60655-1-quinkblack@foxmail.com> References: <20240523070856.60655-1-quinkblack@foxmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] avcodec/mediacodecenc: workaround the alignment requirement only for H.264 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: jjGPzaoMzl2M From: Zhao Zhili There is no bsf for other codecs to modify crop info except H.265. For H.265, the assumption that FFALIGN(width, 16)xFFALIGN(height, 16) is the video resolution can be wrong, since the encoder can use CTU larger than 16x16. In that case, use FFALIGN(width, 16) - width as crop_right is incorrect. So disable the workaround for H.265 now. --- libavcodec/mediacodecenc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c index bbf570e7be..bfff149039 100644 --- a/libavcodec/mediacodecenc.c +++ b/libavcodec/mediacodecenc.c @@ -234,7 +234,8 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) ff_AMediaFormat_setString(format, "mime", codec_mime); // Workaround the alignment requirement of mediacodec. We can't do it // silently for AV_PIX_FMT_MEDIACODEC. - if (avctx->pix_fmt != AV_PIX_FMT_MEDIACODEC) { + if (avctx->pix_fmt != AV_PIX_FMT_MEDIACODEC && + avctx->codec_id == AV_CODEC_ID_H264) { s->width = FFALIGN(avctx->width, 16); s->height = FFALIGN(avctx->height, 16); } else { From patchwork Thu May 23 07:08:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 49163 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:542:0:b0:460:55fa:d5ed with SMTP id 63csp844091vqf; Thu, 23 May 2024 00:09:22 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVUYXTlnLe84fBpBa0JIhcJuXyd293Q/aQndB4r4917jt4obMPFfVDpmgd7rRyDwGvCeO+v43w0m+eIwJFtj66axQ+fKTL9Q0BB6g== X-Google-Smtp-Source: AGHT+IE+IFX4tO9qHELM4Xg6xkQhABIEIdzDQTZyMuRnk7dopQoxkK6Cst7tO4/n60uH0cnkCqgn X-Received: by 2002:ac2:514d:0:b0:51d:a87e:27e6 with SMTP id 2adb3069b0e04-526bfcf733fmr2488330e87.67.1716448162598; Thu, 23 May 2024 00:09:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1716448162; cv=none; d=google.com; s=arc-20160816; b=kqqYJJivyd/7PRXWfxHXpxJrEpJjCD2UxIrfwrAf/7slgl06wRFk4VwnTIMJ/2nThu 8Cj4L9U6z83GpggKmHcGcP4+waN6ht8NmPMzYqEXslRu6F3L9A3AxlW8mQQyoaq3AWFY TfZpEIM6cfiGRpWimqKXhlDTiy2MMbs6JQJCo84etdugRvNQC9xjcntH7l7boOTdWSyU N45dXaspDQ6Cj+T1xUzHmw1uNgHjJ79QWGLXn+xe16i7I3xehHjsEAEVCJ7Swch7vVSm NJlapNGfxq9kh4ghIrYtGvgPHp1NYz9rPr9CtBFX3oljvkwRWq3SMW1sfMT34PKRLBsR 69kg== 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:date :to:from:message-id:dkim-signature:delivered-to; bh=l47nq6sn2Bk0JbWWSdFEU2g619j2IVh3SzvabQAz5P8=; fh=HnHYuZ9XgUo86ZRXTLWWmQxhslYEI9B9taZ5X1DLFfc=; b=EryF3nckqs1+WoUE7xK1sEvAYZyy6MHmrzaHqHJZQ+oJKDirQu0f6w54bEWnF36TQG Xk2ODDPK54UbhHCpw49DanEkGTqYQ7W39WVISbjDnu/IKIu5v/Mh9KOvBVwYb3GMJ+j8 iPP2YJ9wDCsRPk/Zp55fwi2Lbqn4dpBD4of1kNM3iBbb9GgopX/ofP+nHTuq/O5z2tuo IxNhHh/udF8lsOarRbRFktXlV4+X0NNDCEv4DAX7Shz1ygWQgmH1S9ItNqecxXCeMJCI di0ao1ilrroHZgXntgg5TukuqafzVcNkUhKDL8B+SdOS2+eHY+iAkiHLTNBz72f+ubeh BV9w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=UnrZ0O6T; 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=NONE dis=NONE) header.from=foxmail.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-57847578a83si352179a12.381.2024.05.23.00.09.21; Thu, 23 May 2024 00:09:22 -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=@foxmail.com header.s=s201512 header.b=UnrZ0O6T; 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=NONE dis=NONE) header.from=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D3FEF68D355; Thu, 23 May 2024 10:09:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-233.mail.qq.com (out203-205-221-233.mail.qq.com [203.205.221.233]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 279F768C6D5 for ; Thu, 23 May 2024 10:09:09 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1716448140; bh=ZpSptXMFaDGdLQ+LXwfr8McxpY2O89uo8GF5nNsuOY0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=UnrZ0O6TAE6mAu61jtjbMLgBXyQ/STivTSaPCaeXTK73+id8Wah619Nx2CxEDAQEH OkXCnctfBQsPHrY59AN9LWW6LXPxqsal7TnAnWTeba4FHT/cW5ZeElQUjz5Wi2Y3Lm TGUH+xGydZ1CK+qcVkDoW+fSyA3XS9Kah2VR8goA= Received: from ZHILIZHAO-MB1.tencent.com ([119.147.10.190]) by newxmesmtplogicsvrszb9-0.qq.com (NewEsmtp) with SMTP id 23A264EF; Thu, 23 May 2024 15:08:58 +0800 X-QQ-mid: xmsmtpt1716448139tl7eyn7wx Message-ID: X-QQ-XMAILINFO: OJV4F5WcWfWa5ojQZ4MX+trWwYSPOHWfYr24fJWKdxw0253SrorlSsitHXFLyI 9eAvrH+v9elLBfe/V2+w2AiSBYdrSidXwayEFgO5GAHtwcFg9/ZZBf7C0OowSebyEOUzzmKdrdEV Y3XSDlqLHz85SNT3KpeRx+cPQhruRi7JV/Fhzk5a8vFk6ZhNBOYGOAS7zHkimhRAS1rfla6RTBQN 22sw3TRoLBTIyMwxq0gP4RnPAGTEAxat8WzfAiv1Hl4gXkBCmWmpZ95AerAc9tMgeS9UyjJPukkd 6D7i1EhTk2HFnP6G8PThridbsJyUy4OsvY8LrK8ZDlvFdZtfPlcjYDYrhYHc8N1gm7+mz0zYf+JA jPF+Im/y65mkIZi33HQYi3jBImlU76V7OHUQIQKlAbFPbPbQyvF7hF/e9OzBJrQp39ABlzzwGpty qy/sg2G2jCAl3aBZKtclEzIkPqx9ljaqvHNQn7OxLgLMdV4s0Voz3KycLC78kM3U/UDYT+2/AlCe sDgP1ZcKHFogYbyF9pqeyhl+9oND0cJzw9nIku0BWp0rUMjaZOMVsM6nK+BxgZbdUWutnL1oZ0Xp 9kSofc8zh8ebMqqpb8MJe+1mbOwcqP7qe2zd4gc6l1mvXhXJgHX33haNJdSIlaqVcd+ZYZoJBONU Cgu55NYXY4Bqh5GDTC59qZMEbssA58Uk9j9JYE7D+/oTdBWNo4SBeikP9ZFvceBR+yTLWnIjO9j9 buACBM9EH2kzCAZWwDjaCwKGG+7rdh+DWfYJ1sm9K9vZ3tSU5e3FitRxHaaNi4cgvszeDHYLapSN fwn2INwprjW84AFCMQkScIijMJ7WuzFNVmVcLrt0cIDJ7yW//Hc8R/5xlaxF0muVABACW4cAaIEk AqAoBrMQP3cHte6EDeRJsg61Q+MeGek8GfEbg9qbXgobn3U8KilsoBCPE8190WG/2UNIlWqmmALk YSyWgEbvHgFvMXmVY/iJeS38ZQ5lp67dS5SYalLluGc2mtD+4u8dgPrYrPLuERnuS3E0R39jNHbc i7RzXVvr99unG1KV7C X-QQ-XMRINFO: OWPUhxQsoeAVDbp3OJHYyFg= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 May 2024 15:08:56 +0800 X-OQ-MSGID: <20240523070856.60655-3-quinkblack@foxmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240523070856.60655-1-quinkblack@foxmail.com> References: <20240523070856.60655-1-quinkblack@foxmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] avcodec/mediacodecenc: workaround the alignment requirement for H.265 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: alDRTUQnmg4u From: Zhao Zhili Signed-off-by: Zhao Zhili --- libavcodec/mediacodecenc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c index bfff149039..a42270551b 100644 --- a/libavcodec/mediacodecenc.c +++ b/libavcodec/mediacodecenc.c @@ -154,8 +154,12 @@ static int mediacodec_init_bsf(AVCodecContext *avctx) ret = snprintf(str, sizeof(str), "h264_metadata=crop_right=%d:crop_bottom=%d", crop_right, crop_bottom); else if (avctx->codec_id == AV_CODEC_ID_HEVC) - ret = snprintf(str, sizeof(str), "hevc_metadata=crop_right=%d:crop_bottom=%d", - crop_right, crop_bottom); + /* Encoder can use CTU size larger than 16x16, so the real crop + * margin can be larger than crop_right/crop_bottom. Let bsf figure + * out the real crop margin. + */ + ret = snprintf(str, sizeof(str), "hevc_metadata=width=%d:height=%d", + avctx->width, avctx->height); if (ret >= sizeof(str)) return AVERROR_BUFFER_TOO_SMALL; } @@ -235,7 +239,8 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) // Workaround the alignment requirement of mediacodec. We can't do it // silently for AV_PIX_FMT_MEDIACODEC. if (avctx->pix_fmt != AV_PIX_FMT_MEDIACODEC && - avctx->codec_id == AV_CODEC_ID_H264) { + (avctx->codec_id == AV_CODEC_ID_H264 || + avctx->codec_id == AV_CODEC_ID_HEVC)) { s->width = FFALIGN(avctx->width, 16); s->height = FFALIGN(avctx->height, 16); } else {