From patchwork Mon Sep 4 07:33:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mateusz X-Patchwork-Id: 4972 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.15.201 with SMTP id 70csp2863016jao; Mon, 4 Sep 2017 00:39:32 -0700 (PDT) X-Google-Smtp-Source: ADKCNb4HaCzrle+Gw2fGeZ88szFsCnR4jYblmgRxCb9Q+LFAiShGyG5zQD+vHoTWa7KiKtrunYdk X-Received: by 10.28.188.138 with SMTP id m132mr2399078wmf.163.1504510772365; Mon, 04 Sep 2017 00:39:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1504510772; cv=none; d=google.com; s=arc-20160816; b=0PnS1w5Fly7gqsXec8ZNL4vrmc7tkHGElthSXrVaixnKj245/fd1JcOeclwYoV7//B NaWNxtsx1yPKelQc7YGsCpCgIm8cuztIKGXsusjONo5Nx6XptQ382Z0J1lJm1RTFrMzL 2k9OYG6Q7rb1j73xqSXzwNlhCR0D1WBYKhyHle2cu7c8+2qSCrXz7YZ/8yqk9wFRt+xN 9nTVX0Qg5OdQX9j1/Rz8I1qhAbBed4mI5DKShpwLLshg5AYkdec+1U3DM7Vjhh/5+d79 Ai11VAOKcR1DRpuQtRxosFl5mJcjfh1oMSrhtexjimdnghYDchtGU6wlbFD+j/YR9yos U+9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :content-language:mime-version:user-agent:date:message-id:from:to :dkim-signature:delivered-to:arc-authentication-results; bh=VXCIm5gmHkUKKC/2BDT3OS/c1Tag7jf1tWkKChWXyvY=; b=mYhNu4Lr691P6p5iVhhL5mYJJKSu0KYfIwpWFoIlp35p09jNCyM5twUgnK36cKb8XK IqW6MuNvWNbXKXYnVFupdNd45D3TosdHZBleyy8DLBgRSFaI4kNJElRE/12Et9KHxQve i+7+Ew5QMH9waGVVvqDPkt7XbU2OIxnHyfTb7YZhDBi4Zla3wKrZLPIasYYwXV+nWdww 4tR69e0Hw+143MMYwgPSxYdB+4z4LzCCGxb1I3JYgA3+vhzv5iHMVmjmk3w3ccxEBwRI ZYf91069BzfARPmRkXDtdiZeVZ3mMqb6IfIUFyTSdTyRudQKxFK1N1j96009e4okSrJB zNbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@poczta.onet.pl header.s=2011 header.b=UNS1f0Hs; 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 96si4730042wrk.537.2017.09.04.00.39.31; Mon, 04 Sep 2017 00:39:32 -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=@poczta.onet.pl header.s=2011 header.b=UNS1f0Hs; 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 E66BC68A160; Mon, 4 Sep 2017 10:39:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpo93.poczta.onet.pl (smtpo93.poczta.onet.pl [213.180.149.146]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 34FBD68A144 for ; Mon, 4 Sep 2017 10:39:21 +0300 (EEST) Received: from [192.168.1.2] (adss132.neoplus.adsl.tpnet.pl [79.185.204.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: mateuszb@poczta.onet.pl) by smtp.poczta.onet.pl (Onet) with ESMTPSA id 3xm1vC5dMczlmZbS for ; Mon, 4 Sep 2017 09:39:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=poczta.onet.pl; s=2011; t=1504510760; bh=ZAslh9GbmwY5Xb/32kObj/hy1XUeySwbNRiPWV/E+SQ=; h=To:From:Subject:Date:From; b=UNS1f0Hsp5RhCH5O5yBDsGghum7jZ3AalI4ZIbM2wlTZ5eJRjMQDGHw2J+x1flfTe jmntLJ/IkjtIjxp5+JZZtf0LQ3s9GyrRlrKKW1uL4qhKXOKWTBBjhF81g4Lk9+bFkL rNtq3EhHhP+zmrR9+L7Ip7GBBaYwDgN6kei/6fws= To: FFmpeg development discussions and patches From: Mateusz Message-ID: <8fa31561-5155-6975-738c-7406150ba73e@poczta.onet.pl> Date: Mon, 4 Sep 2017 09:33:34 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 Content-Language: en-US Subject: [FFmpeg-devel] [PATCH] libswscale/swscale_unscaled: fix DITHER_COPY macro X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" If ffmpeg reduces bit-depth to 8-bit or more, it uses DITHER_COPY macro. The problem is DITHER_COPY macro make images darker (on all planes). In x265 project there is issue #365 which is caused by this DITHER_COPY bug. I think it is time to fix -- there are more and more high bit-depth sources. Please review.  libswscale/swscale_unscaled.c | 44 +++++++++++++------------------------------  1 file changed, 13 insertions(+), 31 deletions(-) libswscale/swscale_unscaled.c | 44 +++++++++++++------------------------------ 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c index ef36aec..3b7a5a9 100644 --- a/libswscale/swscale_unscaled.c +++ b/libswscale/swscale_unscaled.c @@ -110,24 +110,6 @@ DECLARE_ALIGNED(8, static const uint8_t, dithers)[8][8][8]={ { 112, 16,104, 8,118, 22,110, 14,}, }}; -static const uint16_t dither_scale[15][16]={ -{ 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,}, -{ 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,}, -{ 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,}, -{ 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,}, -{ 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,}, -{ 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,}, -{ 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,}, -{ 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,}, -{ 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,}, -{ 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,}, -{ 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,}, -{ 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,}, -{ 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,}, -{ 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,}, -{ 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,}, -}; - static void fillPlane(uint8_t *plane, int stride, int width, int height, int y, uint8_t val) @@ -1502,22 +1484,22 @@ static int packedCopyWrapper(SwsContext *c, const uint8_t *src[], } #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\ - uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\ - int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\ + unsigned shift= src_depth-dst_depth, tmp;\ for (i = 0; i < height; i++) {\ - const uint8_t *dither= dithers[src_depth-9][i&7];\ + const uint8_t *dither= dithers[shift-1][i&7];\ for (j = 0; j < length-7; j+=8){\ - dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\ - dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\ - dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\ - dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\ - dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\ - dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\ - dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\ - dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\ + tmp = (bswap(src[j+0]) + dither[0])>>shift; dst[j+0] = dbswap(tmp - (tmp>>dst_depth));\ + tmp = (bswap(src[j+1]) + dither[1])>>shift; dst[j+1] = dbswap(tmp - (tmp>>dst_depth));\ + tmp = (bswap(src[j+2]) + dither[2])>>shift; dst[j+2] = dbswap(tmp - (tmp>>dst_depth));\ + tmp = (bswap(src[j+3]) + dither[3])>>shift; dst[j+3] = dbswap(tmp - (tmp>>dst_depth));\ + tmp = (bswap(src[j+4]) + dither[4])>>shift; dst[j+4] = dbswap(tmp - (tmp>>dst_depth));\ + tmp = (bswap(src[j+5]) + dither[5])>>shift; dst[j+5] = dbswap(tmp - (tmp>>dst_depth));\ + tmp = (bswap(src[j+6]) + dither[6])>>shift; dst[j+6] = dbswap(tmp - (tmp>>dst_depth));\ + tmp = (bswap(src[j+7]) + dither[7])>>shift; dst[j+7] = dbswap(tmp - (tmp>>dst_depth));\ + }\ + for (; j < length; j++){\ + tmp = (bswap(src[j]) + dither[j&7])>>shift; dst[j] = dbswap(tmp - (tmp>>dst_depth));\ }\ - for (; j < length; j++)\ - dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\ dst += dstStride;\ src += srcStride;\ } diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c index ef36aec..3b7a5a9 100644 --- a/libswscale/swscale_unscaled.c +++ b/libswscale/swscale_unscaled.c @@ -110,24 +110,6 @@ DECLARE_ALIGNED(8, static const uint8_t, dithers)[8][8][8]={    { 112, 16,104,  8,118, 22,110, 14,},  }};   -static const uint16_t dither_scale[15][16]={ -{    2,    3,    3,    5,    5,    5,    5,    5,    5,    5,    5,    5,    5,    5,    5,    5,}, -{    2,    3,    7,    7,   13,   13,   25,   25,   25,   25,   25,   25,   25,   25,   25,   25,}, -{    3,    3,    4,   15,   15,   29,   57,   57,   57,  113,  113,  113,  113,  113,  113,  113,}, -{    3,    4,    4,    5,   31,   31,   61,  121,  241,  241,  241,  241,  481,  481,  481,  481,}, -{    3,    4,    5,    5,    6,   63,   63,  125,  249,  497,  993,  993,  993,  993,  993, 1985,}, -{    3,    5,    6,    6,    6,    7,  127,  127,  253,  505, 1009, 2017, 4033, 4033, 4033, 4033,}, -{    3,    5,    6,    7,    7,    7,    8,  255,  255,  509, 1017, 2033, 4065, 8129,16257,16257,}, -{    3,    5,    6,    8,    8,    8,    8,    9,  511,  511, 1021, 2041, 4081, 8161,16321,32641,}, -{    3,    5,    7,    8,    9,    9,    9,    9,   10, 1023, 1023, 2045, 4089, 8177,16353,32705,}, -{    3,    5,    7,    8,   10,   10,   10,   10,   10,   11, 2047, 2047, 4093, 8185,16369,32737,}, -{    3,    5,    7,    8,   10,   11,   11,   11,   11,   11,   12, 4095, 4095, 8189,16377,32753,}, -{    3,    5,    7,    9,   10,   12,   12,   12,   12,   12,   12,   13, 8191, 8191,16381,32761,}, -{    3,    5,    7,    9,   10,   12,   13,   13,   13,   13,   13,   13,   14,16383,16383,32765,}, -{    3,    5,    7,    9,   10,   12,   14,   14,   14,   14,   14,   14,   14,   15,32767,32767,}, -{    3,    5,    7,    9,   11,   12,   14,   15,   15,   15,   15,   15,   15,   15,   16,65535,}, -}; -    static void fillPlane(uint8_t *plane, int stride, int width, int height, int y,                        uint8_t val) @@ -1502,22 +1484,22 @@ static int packedCopyWrapper(SwsContext *c, const uint8_t *src[],  }    #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\ -    uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\ -    int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\ +    unsigned shift= src_depth-dst_depth, tmp;\      for (i = 0; i < height; i++) {\ -        const uint8_t *dither= dithers[src_depth-9][i&7];\ +        const uint8_t *dither= dithers[shift-1][i&7];\          for (j = 0; j < length-7; j+=8){\ -            dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\ -            dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\ -            dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\ -            dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\ -            dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\ -            dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\ -            dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\ -            dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\ +            tmp = (bswap(src[j+0]) + dither[0])>>shift; dst[j+0] = dbswap(tmp - (tmp>>dst_depth));\ +            tmp = (bswap(src[j+1]) + dither[1])>>shift; dst[j+1] = dbswap(tmp - (tmp>>dst_depth));\ +            tmp = (bswap(src[j+2]) + dither[2])>>shift; dst[j+2] = dbswap(tmp - (tmp>>dst_depth));\ +            tmp = (bswap(src[j+3]) + dither[3])>>shift; dst[j+3] = dbswap(tmp - (tmp>>dst_depth));\ +            tmp = (bswap(src[j+4]) + dither[4])>>shift; dst[j+4] = dbswap(tmp - (tmp>>dst_depth));\ +            tmp = (bswap(src[j+5]) + dither[5])>>shift; dst[j+5] = dbswap(tmp - (tmp>>dst_depth));\ +            tmp = (bswap(src[j+6]) + dither[6])>>shift; dst[j+6] = dbswap(tmp - (tmp>>dst_depth));\ +            tmp = (bswap(src[j+7]) + dither[7])>>shift; dst[j+7] = dbswap(tmp - (tmp>>dst_depth));\ +        }\ +        for (; j < length; j++){\ +            tmp = (bswap(src[j]) + dither[j&7])>>shift; dst[j] = dbswap(tmp - (tmp>>dst_depth));\          }\ -        for (; j < length; j++)\ -            dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\          dst += dstStride;\          src += srcStride;\      }