From patchwork Sat Sep 17 21:27:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Langdale X-Patchwork-Id: 37996 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp392391pzh; Sat, 17 Sep 2022 14:28:07 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5EJelzDM9wQGJIgzqoL74FvH5C8VuIkeX5e9fxvGaomB2Bnt9oBVnC26ooJ1PRuJNwLONX X-Received: by 2002:aa7:d054:0:b0:450:f6b9:bc2e with SMTP id n20-20020aa7d054000000b00450f6b9bc2emr9061684edo.413.1663450087333; Sat, 17 Sep 2022 14:28:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663450087; cv=none; d=google.com; s=arc-20160816; b=jjIYW2tEDp36sUp3s+S6a6tB36uG7j1sOJ5FvxrcmJH++STGZFS4bqcZ0Hwsa0Dnz+ WN82A+YaZQivp0NcPEpcXscO2UaLP83vpgoTMji1JdElmfeX6s86wnY+otHmQN4x8Syu SXghZfe49it7CO7GdX9EJqUK7VCWnu8i7XjfoTbU6u75ZJw9SSvAxzL31C/beWeGKNXF m1rKVs1zg3pt8dKna+BjFTu8SjvXTouYUku67qOHHOSnFlHmSi1XSTTArilzhn5C2hJ8 tQkSbjI/7kguIEGz/ME6nFQSx8Stly23Socs4CXUkrS5kGQRzkkXIWz2a86dydjXVs43 2Uxw== 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=lqPbAXlUmGzzhIA/DXcfiVFz3R1+xsR3I4nNRrFEiUY=; b=iyReICnVSMcIAgEcDtReLjBgmy5r3cKt9tdrLzSv6GWN+66e5XXiQKMmWRsVqVTvJX kJ6YEQZheDM4fIN7N5BXvrv4iE+jMiC9ogBpEC0LSWXbvv5zrN/XD5baUpat5BSX/S/d GGJd5ZO1MBUfjbXACKk+90LXomqkmoqub8iXB3bTaaq1DQeOpQvl4GlWCXk5EV32GN19 VH0NxEz5atfcQg68jjcV4fWRAy4ipfz+79WqSy3rWT91HhkPYh7ux7rHP1H7tIpsvfTA KydYRUDBw5NTLX76OAPlUqe55v28GJOr5o1uFa9iZe4y6geBOJGkNZQEgTLx8kVgf4BF QlaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@overt.org header.s=mail header.b=tz+tTw09; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=overt.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id dp22-20020a170906c15600b00730870cb4b6si5681398ejc.621.2022.09.17.14.28.06; Sat, 17 Sep 2022 14:28:07 -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=@overt.org header.s=mail header.b=tz+tTw09; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=overt.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1777C68BB15; Sun, 18 Sep 2022 00:28:04 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.overt.org (mail.overt.org [157.230.92.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 90A5568B9C9 for ; Sun, 18 Sep 2022 00:27:57 +0300 (EEST) Received: from authenticated-user (mail.overt.org [157.230.92.47]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by mail.overt.org (Postfix) with ESMTPSA id B48D93FB20; Sat, 17 Sep 2022 16:27:55 -0500 (CDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=overt.org; s=mail; t=1663450076; bh=yEXSUqPihyealtT+OPh784cbXyNruqAAymbDnXUy0u8=; h=From:To:Cc:Subject:Date:From; b=tz+tTw09usTIGCZOhXV2jMxgVEIx77+gVLpr7eQ6ktcKub0vWAZ3sYFYfi+4+U6DD RLPgQu+y+iw++9gjU0ck6khKDsHDnJmlYJ2TQGH3eOZaJIkzO5x8E8vUV1x1nJmlQ4 7skAR/V7cFHh5BTXZK8mKCCxDkr2ark5ciAzaLDCbs2cLEABoHenINMUDelHOKYIn/ K1/c7wcw1rccKtwHg6BNhmtELngcntgA571gqwD44ObbR11FoR52KWeFnprZU44ig+ PZc+z/Q0D+a8zp0VXMkDVWfarAJsn/MwIjfX1BL1QReQIoDF/9gg03KUexSwZ4nKI3 098wR77t7TIig== From: Philip Langdale To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Sep 2022 14:27:49 -0700 Message-Id: <20220917212749.7026-1-philipl@overt.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5] lavu/pixdesc: favour formats where depth and subsampling exactly match 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: Philip Langdale Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: +4LCIbWnW6mz Since introducing the various packed formats used by VAAPI (and p012), we've noticed that there's actually a gap in how av_find_best_pix_fmt_of_2 works. It doesn't actually assign any value to having the same bit depth as the source format, when comparing against formats with a higher bit depth. This usually doesn't matter, because av_get_padded_bits_per_pixel() will account for it. However, as many of these formats use padding internally, we find that av_get_padded_bits_per_pixel() actually returns the same value for the 10 bit, 12 bit, 16 bit flavours, etc. In these tied situations, we end up just picking the first of the two provided formats, even if the second one should be preferred because it matches the actual bit depth. This bug already existed if you tried to compare yuv420p10 against p016 and p010, for example, but it simply hadn't come up before so we never noticed. But now, we actually got a situation in the VAAPI VP9 decoder where it offers both p010 and p012 because Profile 3 could be either depth and ends up picking p012 for 10 bit content due to the ordering of the testing. In addition, in the process of testing the fix, I realised we have the same gap when it comes to chroma subsampling - we do not favour a format that has exactly the same subsampling vs one with less subsampling when all else is equal. To fix this, I'm introducing a small score penalty if the bit depth or subsampling doesn't exactly match the source format. This will break the tie in favour of the format with the exact match, but not offset any of the other scoring penalties we already have. I have added a set of tests around these formats which will fail without this fix. v2: Rework penalty system to scale penalty based on how different the two formats are, and add new loss categories for them. v3: Remove leftover bits of v1. Remove bit depth penalty scaling to avoid the value being too large in extreme cases (1 bit vs 16 bit). v4: Reduce subsampling penalty to avoid situations where a loss of bit dpeth will have a smaller penalty than having greater than needed chroma resolution. v5: Update fate reference. Signed-off-by: Philip Langdale --- libavutil/pixdesc.c | 31 +++++++- libavutil/pixdesc.h | 15 ++-- libavutil/tests/pixfmt_best.c | 129 +++++++++++++++++++++++++++++----- tests/ref/fate/pixfmt_best | 2 +- 4 files changed, 150 insertions(+), 27 deletions(-) diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c index d7c6ebfdc4..b472a94f60 100644 --- a/libavutil/pixdesc.c +++ b/libavutil/pixdesc.c @@ -3013,9 +3013,16 @@ static int get_pix_fmt_score(enum AVPixelFormat dst_pix_fmt, for (i = 0; i < nb_components; i++) { int depth_minus1 = (dst_pix_fmt == AV_PIX_FMT_PAL8) ? 7/nb_components : (dst_desc->comp[i].depth - 1); - if (src_desc->comp[i].depth - 1 > depth_minus1 && (consider & FF_LOSS_DEPTH)) { + int depth_delta = src_desc->comp[i].depth - 1 - depth_minus1; + if (depth_delta > 0 && (consider & FF_LOSS_DEPTH)) { loss |= FF_LOSS_DEPTH; score -= 65536 >> depth_minus1; + } else if (depth_delta < 0 && (consider & FF_LOSS_EXCESS_DEPTH)) { + // Favour formats where bit depth exactly matches. If all other + // scoring is equal, we'd rather use the bit depth that most closely + // matches the source. + loss |= FF_LOSS_EXCESS_DEPTH; + score += depth_delta; } } @@ -3035,6 +3042,28 @@ static int get_pix_fmt_score(enum AVPixelFormat dst_pix_fmt, } } + if (consider & FF_LOSS_EXCESS_RESOLUTION) { + // Favour formats where chroma subsampling exactly matches. If all other + // scoring is equal, we'd rather use the subsampling that most closely + // matches the source. + if (dst_desc->log2_chroma_w < src_desc->log2_chroma_w) { + loss |= FF_LOSS_EXCESS_RESOLUTION; + score -= 1 << (src_desc->log2_chroma_w - dst_desc->log2_chroma_w); + } + + if (dst_desc->log2_chroma_h < src_desc->log2_chroma_h) { + loss |= FF_LOSS_EXCESS_RESOLUTION; + score -= 1 << (src_desc->log2_chroma_h - dst_desc->log2_chroma_h); + } + + // don't favour 411 over 420, because 420 has much better support on the + // decoder side. + if (dst_desc->log2_chroma_w == 1 && src_desc->log2_chroma_w == 2 && + dst_desc->log2_chroma_h == 1 && src_desc->log2_chroma_h == 2) { + score += 4; + } + } + if(consider & FF_LOSS_COLORSPACE) switch(dst_color) { case FF_COLOR_RGB: diff --git a/libavutil/pixdesc.h b/libavutil/pixdesc.h index f8a195ffcd..48d9300bfe 100644 --- a/libavutil/pixdesc.h +++ b/libavutil/pixdesc.h @@ -357,12 +357,15 @@ void av_write_image_line(const uint16_t *src, uint8_t *data[4], */ enum AVPixelFormat av_pix_fmt_swap_endianness(enum AVPixelFormat pix_fmt); -#define FF_LOSS_RESOLUTION 0x0001 /**< loss due to resolution change */ -#define FF_LOSS_DEPTH 0x0002 /**< loss due to color depth change */ -#define FF_LOSS_COLORSPACE 0x0004 /**< loss due to color space conversion */ -#define FF_LOSS_ALPHA 0x0008 /**< loss of alpha bits */ -#define FF_LOSS_COLORQUANT 0x0010 /**< loss due to color quantization */ -#define FF_LOSS_CHROMA 0x0020 /**< loss of chroma (e.g. RGB to gray conversion) */ +#define FF_LOSS_RESOLUTION 0x0001 /**< loss due to resolution change */ +#define FF_LOSS_DEPTH 0x0002 /**< loss due to color depth change */ +#define FF_LOSS_COLORSPACE 0x0004 /**< loss due to color space conversion */ +#define FF_LOSS_ALPHA 0x0008 /**< loss of alpha bits */ +#define FF_LOSS_COLORQUANT 0x0010 /**< loss due to color quantization */ +#define FF_LOSS_CHROMA 0x0020 /**< loss of chroma (e.g. RGB to gray conversion) */ +#define FF_LOSS_EXCESS_RESOLUTION 0x0040 /**< loss due to unneeded extra resolution */ +#define FF_LOSS_EXCESS_DEPTH 0x0080 /**< loss due to unneeded extra color depth */ + /** * Compute what kind of losses will occur when converting from one specific diff --git a/libavutil/tests/pixfmt_best.c b/libavutil/tests/pixfmt_best.c index 0542af494f..d71b3cc2e8 100644 --- a/libavutil/tests/pixfmt_best.c +++ b/libavutil/tests/pixfmt_best.c @@ -39,32 +39,74 @@ static const enum AVPixelFormat pixfmt_list[] = { AV_PIX_FMT_VAAPI, }; -static enum AVPixelFormat find_best(enum AVPixelFormat pixfmt) +static const enum AVPixelFormat semiplanar_list[] = { + AV_PIX_FMT_P016, + AV_PIX_FMT_P012, + AV_PIX_FMT_P010, + AV_PIX_FMT_NV12, +}; + +static const enum AVPixelFormat packed_list[] = { + AV_PIX_FMT_XV36, + AV_PIX_FMT_XV30, + AV_PIX_FMT_VUYX, + AV_PIX_FMT_Y212, + AV_PIX_FMT_Y210, + AV_PIX_FMT_YUYV422, +}; + +static const enum AVPixelFormat subsampled_list[] = { + AV_PIX_FMT_YUV411P, + AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUV422P, + AV_PIX_FMT_YUV444P, +}; + +static const enum AVPixelFormat depthchroma_list[] = { + AV_PIX_FMT_YUV420P14, + AV_PIX_FMT_YUV422P14, + AV_PIX_FMT_YUV444P16, +}; + +typedef enum AVPixelFormat (*find_best_t)(enum AVPixelFormat pixfmt); + +#define find_best_wrapper(name, list) \ +static enum AVPixelFormat find_best_ ## name (enum AVPixelFormat pixfmt) \ +{ \ + enum AVPixelFormat best = AV_PIX_FMT_NONE; \ + int i; \ + for (i = 0; i < FF_ARRAY_ELEMS(list); i++) \ + best = av_find_best_pix_fmt_of_2(best, list[i], \ + pixfmt, 0, NULL); \ + return best; \ +} + +find_best_wrapper(base, pixfmt_list) +find_best_wrapper(seminplanar, semiplanar_list) +find_best_wrapper(packed, packed_list) +find_best_wrapper(subsampled, subsampled_list) +find_best_wrapper(depthchroma, depthchroma_list) + +static void test(enum AVPixelFormat input, enum AVPixelFormat expected, + int *pass, int *fail, find_best_t find_best_fn) { - enum AVPixelFormat best = AV_PIX_FMT_NONE; - int i; - for (i = 0; i < FF_ARRAY_ELEMS(pixfmt_list); i++) - best = av_find_best_pix_fmt_of_2(best, pixfmt_list[i], - pixfmt, 0, NULL); - return best; + enum AVPixelFormat output = find_best_fn(input); + if (output != expected) { + printf("Matching %s: got %s, expected %s\n", + av_get_pix_fmt_name(input), + av_get_pix_fmt_name(output), + av_get_pix_fmt_name(expected)); + ++(*fail); + } else + ++(*pass); } int main(void) { - enum AVPixelFormat output; int i, pass = 0, fail = 0; -#define TEST(input, expected) do { \ - output = find_best(input); \ - if (output != expected) { \ - printf("Matching %s: got %s, expected %s\n", \ - av_get_pix_fmt_name(input), \ - av_get_pix_fmt_name(output), \ - av_get_pix_fmt_name(expected)); \ - ++fail; \ - } else \ - ++pass; \ - } while (0) +#define TEST(input, expected) \ + test(input, expected, &pass, &fail, find_best_base) // Same formats. for (i = 0; i < FF_ARRAY_ELEMS(pixfmt_list); i++) @@ -137,6 +179,55 @@ int main(void) // Opaque formats are least unlike each other. TEST(AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_VDPAU); +#define TEST_SEMIPLANAR(input, expected) \ + test(input, expected, &pass, &fail, find_best_seminplanar) + + // Same formats. + for (i = 0; i < FF_ARRAY_ELEMS(semiplanar_list); i++) + TEST_SEMIPLANAR(semiplanar_list[i], semiplanar_list[i]); + + // Formats containing the same data in different layouts. + TEST_SEMIPLANAR(AV_PIX_FMT_YUV420P, AV_PIX_FMT_NV12); + TEST_SEMIPLANAR(AV_PIX_FMT_YUV420P10, AV_PIX_FMT_P010); + TEST_SEMIPLANAR(AV_PIX_FMT_YUV420P12, AV_PIX_FMT_P012); + TEST_SEMIPLANAR(AV_PIX_FMT_YUV420P16, AV_PIX_FMT_P016); + TEST_SEMIPLANAR(AV_PIX_FMT_YUV420P9, AV_PIX_FMT_P010); + +#define TEST_PACKED(input, expected) \ + test(input, expected, &pass, &fail, find_best_packed) + + // Same formats. + for (i = 0; i < FF_ARRAY_ELEMS(packed_list); i++) + TEST_PACKED(packed_list[i], packed_list[i]); + + // Formats containing the same data in different layouts. + TEST_PACKED(AV_PIX_FMT_YUV444P, AV_PIX_FMT_VUYX); + TEST_PACKED(AV_PIX_FMT_YUV444P10, AV_PIX_FMT_XV30); + TEST_PACKED(AV_PIX_FMT_YUV444P12, AV_PIX_FMT_XV36); + TEST_PACKED(AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUYV422); + TEST_PACKED(AV_PIX_FMT_YUV422P10, AV_PIX_FMT_Y210); + TEST_PACKED(AV_PIX_FMT_YUV422P12, AV_PIX_FMT_Y212); + +#define TEST_SUBSAMPLED(input, expected) \ + test(input, expected, &pass, &fail, find_best_subsampled) + + // Same formats. + for (i = 0; i < FF_ARRAY_ELEMS(subsampled_list); i++) + TEST_SUBSAMPLED(subsampled_list[i], subsampled_list[i]); + + TEST_SUBSAMPLED(AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV420P); + +#define TEST_DEPTH_CHROMA(input, expected) \ + test(input, expected, &pass, &fail, find_best_depthchroma) + + // Same formats. + for (i = 0; i < FF_ARRAY_ELEMS(depthchroma_list); i++) + TEST_DEPTH_CHROMA(depthchroma_list[i], depthchroma_list[i]); + + TEST_DEPTH_CHROMA(AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV444P16); + TEST_DEPTH_CHROMA(AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16); + + printf("%d tests passed, %d tests failed.\n", pass, fail); return !!fail; } diff --git a/tests/ref/fate/pixfmt_best b/tests/ref/fate/pixfmt_best index 783c5fe640..1bdc0e83f0 100644 --- a/tests/ref/fate/pixfmt_best +++ b/tests/ref/fate/pixfmt_best @@ -1 +1 @@ -75 tests passed, 0 tests failed. +106 tests passed, 0 tests failed.