From patchwork Tue Nov 1 19:05:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Drew Dunne X-Patchwork-Id: 39103 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp349199pzb; Tue, 1 Nov 2022 12:05:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7NNOgsq/xPeN7Y6XTMjsqgGOtYuw82dOXoM2WGYpdE80vRPcGPp/DRCwgnhhkYonq0xTl/ X-Received: by 2002:aa7:cdcf:0:b0:463:56ff:4cf8 with SMTP id h15-20020aa7cdcf000000b0046356ff4cf8mr12925610edw.215.1667329531011; Tue, 01 Nov 2022 12:05:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667329531; cv=none; d=google.com; s=arc-20160816; b=jBJJm6QcScrT70V/b9FgzdCMtY83XXCEHpMBkk3fVrhXSPT/6ft9FJIpjkspD6v0Eq p9x0a+lahvWX+gJkqMIvTBmA9RMpEKCMQ2RKLAdWnELOQ+wcESWUEBcve+eInrPgD1s1 MeVFNi+auyFHfyM4380IK396jsEH9jCp5JLU6dVLDNPh9d8zMJHl/qGnsGeXR8tLVDY9 GMMXIag/r/9S6dKMZ1j6IDTlVWMnN2H5KvGUWsnWSNfAkCWRpqkDohLTLyggxSOlDSBc 9/sQ4vCJ1fFClzy86/reJrbuP6dxMtZAq7q5diuwy5WKJM6CfSwus0V6qbuDnBDTIyQd ZAfA== 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:to:from:message-id:references :mime-version:in-reply-to:date:dkim-signature:delivered-to; bh=utpFmd38WMkk4djvNo7ZYG8xOlaInvJgAwZXOiqn6gU=; b=DKw9sO4Mx+95X3+VSoQYX8YRi8Kw6BxW0kMxHjc6VIoOFLDVVfK9WCR9dm2W85a8R4 zcroug4HlfET9LKxZQzI0daJV+AYWlNZPMvg+R1tycziqrLljn4dst15gabC2I21LVAD JxYbG272adY94V/fSmpyA9FjDHEFRKxYaFUTtbZlH9KZrLZOW9SJnCtHW5FO6MHaBmd4 V2j2bOHIzYD8PGz6JDFENL5nI8JJfXSwhsWQXzPEj4KFtlWsmpSkrvjbxU4nSyYkLBAf 9hZCt4QHbJDugLvCAopaTYnbUJ7HEXzAoYBbvA3mxLvapaWVf8kabKCTESGYJt2sL3pV Z7lQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20210112 header.b=sF9qDZAW; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id qw18-20020a1709066a1200b00791a67e4c04si14693245ejc.31.2022.11.01.12.05.29; Tue, 01 Nov 2022 12:05:31 -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=@google.com header.s=20210112 header.b=sF9qDZAW; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8BB3468BE5C; Tue, 1 Nov 2022 21:05:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BA5EF68BE09 for ; Tue, 1 Nov 2022 21:05:19 +0200 (EET) Received: by mail-yb1-f201.google.com with SMTP id 130-20020a251188000000b006be249d0a70so13825966ybr.2 for ; Tue, 01 Nov 2022 12:05:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hBFNRYBzGpjwc1Hri4DGIRTHgQ5He4PzMN+Hghq8J7w=; b=sF9qDZAWs39VxI0h0QitEu6PzIIl2MKkg0ET+dk/ElGXP4BsDURrV/+CXK+QuJm87k cy1oBHaTUgHTPCCxIOghT9zZ+NeI5Gq282YBJ/ZzlSwKI7+FwD1wDlIpYET5tJ7iD4MR JC6BJbF77L7sJUBpSRSnxgJsg2/iAzJy136T4TtP+guYYhD78LZZp//aHqDelwOb2dNA QcdqnTv4EwecQ9O7PP4MoPdKJU6kRs4iKl+YL2B4SxaFOnmJ0wfl5+DlD8QPXGnii1iG NGCQx16px1uePy22AYXbdjsiii1S8S+kwsiPnVcASSSXUPhdY56TzWO/I6ThYyJl7nbf 4ecA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hBFNRYBzGpjwc1Hri4DGIRTHgQ5He4PzMN+Hghq8J7w=; b=ZAxaEj7jOeJ8Y6+qgnUM5thG2PlYLWj8UeGwyoJF+y/yet2Q8/xrDcY4ZIeLirPveL xYO5khofLzUKm8/Kcb0WMRQLuQV16xnWm/puWjuY7R+EnvViG/ttk9pTzLWb8uaR9mGJ oaVKLx8IaZb+1Lr/Jz+lncL9suFsFtKJ0ai1jZ9VTk9lJiPN7KmMScFMHnbiqJzPIPyz BZT3PdBTjdK+ORJVNldIq+GCtZ3y7sOGX+duivgCL2MojYP6jpVzJA4UV0lNGFf6WQ8r wA1TsJXvAnwCCILSqudXqokdda40ihk4AsCjVpLQitjfUwO5us5rUOXCtDJ3RdiwFsew 2gnQ== X-Gm-Message-State: ACrzQf2Zt6HmCz+py952H2sJmAYUswTNalUjJ56//wXKhX070L3Ou8Px d/rrDqTO+EIz6RA938TwZqGucaRG/H9jAVjVIhPfKP6bv7Jash/JWzA2GkDrDjXtKjsiDLCiXus deUL/ar3Q1JUjj4G52oaxmLWjvh42ex2f6DA/wv8wBUoiQSBw5gwlcOuFhiT9fNpFgg== X-Received: from zaffrezebra.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2d40]) (user=asdunne job=sendgmr) by 2002:a05:690c:82:b0:368:4707:a61a with SMTP id be2-20020a05690c008200b003684707a61amr173255ywb.344.1667329518169; Tue, 01 Nov 2022 12:05:18 -0700 (PDT) Date: Tue, 1 Nov 2022 19:05:07 +0000 In-Reply-To: <20221026161747.3672520-1-asdunne@google.com> Mime-Version: 1.0 References: <20221026161747.3672520-1-asdunne@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221101190507.3229714-1-asdunne@google.com> From: Drew Dunne To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] sws_scale: Use av_sat_add32 in yuv2rgba64 template 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: Drew Dunne Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: lMaSd0xNCUU3 Avoid a possible integer overflow in the yuv2rgba64 templates by using av_sat_add32 when combing the R, G, B components with Y. On certain inputs, this addition can overflow to a negative, is then clipped to a power of two and shifted down 14. This results in a much different value in the output than had it been saturated instead. I will attach an example input YUV in a follow up and some images that show the artifacts resulting from this overflow. --- libswscale/output.c | 96 ++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/libswscale/output.c b/libswscale/output.c index 0e1c1225a0..8c8f62682a 100644 --- a/libswscale/output.c +++ b/libswscale/output.c @@ -1109,20 +1109,20 @@ yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter, B = U * c->yuv2rgb_u2b_coeff; // 8 bits: 30 - 22 = 8 bits, 16 bits: 30 bits - 14 = 16 bits - output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14); - output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14); - output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14); + output_pixel(&dest[0], av_clip_uintp2(av_sat_add32(R_B, Y1), 30) >> 14); + output_pixel(&dest[1], av_clip_uintp2(av_sat_add32( G, Y1), 30) >> 14); + output_pixel(&dest[2], av_clip_uintp2(av_sat_add32(B_R, Y1), 30) >> 14); if (eightbytes) { output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14); - output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14); - output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14); - output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14); + output_pixel(&dest[4], av_clip_uintp2(av_sat_add32(R_B, Y2), 30) >> 14); + output_pixel(&dest[5], av_clip_uintp2(av_sat_add32( G, Y2), 30) >> 14); + output_pixel(&dest[6], av_clip_uintp2(av_sat_add32(B_R, Y2), 30) >> 14); output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14); dest += 8; } else { - output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14); - output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14); - output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14); + output_pixel(&dest[3], av_clip_uintp2(av_sat_add32(R_B, Y2), 30) >> 14); + output_pixel(&dest[4], av_clip_uintp2(av_sat_add32( G, Y2), 30) >> 14); + output_pixel(&dest[5], av_clip_uintp2(av_sat_add32(B_R, Y2), 30) >> 14); dest += 6; } } @@ -1175,20 +1175,20 @@ yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2], A2 += 1 << 13; } - output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14); - output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14); - output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14); + output_pixel(&dest[0], av_clip_uintp2(av_sat_add32(R_B, Y1), 30) >> 14); + output_pixel(&dest[1], av_clip_uintp2(av_sat_add32( G, Y1), 30) >> 14); + output_pixel(&dest[2], av_clip_uintp2(av_sat_add32(B_R, Y1), 30) >> 14); if (eightbytes) { output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14); - output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14); - output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14); - output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14); + output_pixel(&dest[4], av_clip_uintp2(av_sat_add32(R_B, Y2), 30) >> 14); + output_pixel(&dest[5], av_clip_uintp2(av_sat_add32( G, Y2), 30) >> 14); + output_pixel(&dest[6], av_clip_uintp2(av_sat_add32(B_R, Y2), 30) >> 14); output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14); dest += 8; } else { - output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14); - output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14); - output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14); + output_pixel(&dest[3], av_clip_uintp2(av_sat_add32(R_B, Y2), 30) >> 14); + output_pixel(&dest[4], av_clip_uintp2(av_sat_add32( G, Y2), 30) >> 14); + output_pixel(&dest[5], av_clip_uintp2(av_sat_add32(B_R, Y2), 30) >> 14); dest += 6; } } @@ -1232,20 +1232,20 @@ yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0, G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff; B = U * c->yuv2rgb_u2b_coeff; - output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14); - output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14); - output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14); + output_pixel(&dest[0], av_clip_uintp2(av_sat_add32(R_B, Y1), 30) >> 14); + output_pixel(&dest[1], av_clip_uintp2(av_sat_add32( G, Y1), 30) >> 14); + output_pixel(&dest[2], av_clip_uintp2(av_sat_add32(B_R, Y1), 30) >> 14); if (eightbytes) { output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14); - output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14); - output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14); - output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14); + output_pixel(&dest[4], av_clip_uintp2(av_sat_add32(R_B, Y2), 30) >> 14); + output_pixel(&dest[5], av_clip_uintp2(av_sat_add32( G, Y2), 30) >> 14); + output_pixel(&dest[6], av_clip_uintp2(av_sat_add32(B_R, Y2), 30) >> 14); output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14); dest += 8; } else { - output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14); - output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14); - output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14); + output_pixel(&dest[3], av_clip_uintp2(av_sat_add32(R_B, Y2), 30) >> 14); + output_pixel(&dest[4], av_clip_uintp2(av_sat_add32( G, Y2), 30) >> 14); + output_pixel(&dest[5], av_clip_uintp2(av_sat_add32(B_R, Y2), 30) >> 14); dest += 6; } } @@ -1278,20 +1278,20 @@ yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0, G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff; B = U * c->yuv2rgb_u2b_coeff; - output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14); - output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14); - output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14); + output_pixel(&dest[0], av_clip_uintp2(av_sat_add32(R_B, Y1), 30) >> 14); + output_pixel(&dest[1], av_clip_uintp2(av_sat_add32( G, Y1), 30) >> 14); + output_pixel(&dest[2], av_clip_uintp2(av_sat_add32(B_R, Y1), 30) >> 14); if (eightbytes) { output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14); - output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14); - output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14); - output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14); + output_pixel(&dest[4], av_clip_uintp2(av_sat_add32(R_B, Y2), 30) >> 14); + output_pixel(&dest[5], av_clip_uintp2(av_sat_add32( G, Y2), 30) >> 14); + output_pixel(&dest[6], av_clip_uintp2(av_sat_add32(B_R, Y2), 30) >> 14); output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14); dest += 8; } else { - output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14); - output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14); - output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14); + output_pixel(&dest[3], av_clip_uintp2(av_sat_add32(R_B, Y2), 30) >> 14); + output_pixel(&dest[4], av_clip_uintp2(av_sat_add32( G, Y2), 30) >> 14); + output_pixel(&dest[5], av_clip_uintp2(av_sat_add32(B_R, Y2), 30) >> 14); dest += 6; } } @@ -1351,9 +1351,9 @@ yuv2rgba64_full_X_c_template(SwsContext *c, const int16_t *lumFilter, B = U * c->yuv2rgb_u2b_coeff; // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit - output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14); - output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14); - output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14); + output_pixel(&dest[0], av_clip_uintp2(av_sat_add32(R_B, Y), 30) >> 14); + output_pixel(&dest[1], av_clip_uintp2(av_sat_add32( G, Y), 30) >> 14); + output_pixel(&dest[2], av_clip_uintp2(av_sat_add32(B_R, Y), 30) >> 14); if (eightbytes) { output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14); dest += 4; @@ -1404,9 +1404,9 @@ yuv2rgba64_full_2_c_template(SwsContext *c, const int32_t *buf[2], A += 1 << 13; } - output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14); - output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14); - output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14); + output_pixel(&dest[0], av_clip_uintp2(av_sat_add32(R_B, Y), 30) >> 14); + output_pixel(&dest[1], av_clip_uintp2(av_sat_add32( G, Y), 30) >> 14); + output_pixel(&dest[2], av_clip_uintp2(av_sat_add32(B_R, Y), 30) >> 14); if (eightbytes) { output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14); dest += 4; @@ -1448,9 +1448,9 @@ yuv2rgba64_full_1_c_template(SwsContext *c, const int32_t *buf0, G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff; B = U * c->yuv2rgb_u2b_coeff; - output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14); - output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14); - output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14); + output_pixel(&dest[0], av_clip_uintp2(av_sat_add32(R_B, Y), 30) >> 14); + output_pixel(&dest[1], av_clip_uintp2(av_sat_add32( G, Y), 30) >> 14); + output_pixel(&dest[2], av_clip_uintp2(av_sat_add32(B_R, Y), 30) >> 14); if (eightbytes) { output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14); dest += 4; @@ -1481,9 +1481,9 @@ yuv2rgba64_full_1_c_template(SwsContext *c, const int32_t *buf0, G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff; B = U * c->yuv2rgb_u2b_coeff; - output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14); - output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14); - output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14); + output_pixel(&dest[0], av_clip_uintp2(av_sat_add32(R_B, Y), 30) >> 14); + output_pixel(&dest[1], av_clip_uintp2(av_sat_add32( G, Y), 30) >> 14); + output_pixel(&dest[2], av_clip_uintp2(av_sat_add32(B_R, Y), 30) >> 14); if (eightbytes) { output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14); dest += 4;