From patchwork Tue Sep 10 04:58:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: WATANABE Osamu X-Patchwork-Id: 51483 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:41b1:b0:48e:c0f8:d0de with SMTP id le49csp180149vqb; Mon, 9 Sep 2024 21:59:15 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW+/m7j8xVsGHAGEQEcSYHgZ8m1jHqwQyZd22yuSlp5Oy1eZpaABtzctoOHK+vNpyqlowJOLE2g7g1FRllqsZaa@gmail.com X-Google-Smtp-Source: AGHT+IGV9hLsV9OuZUmizxfcAhez0qtBAoLYASYGObb+pfPhjo1e3I7ZmsSHffL0jns4CHr9eOfz X-Received: by 2002:a05:6402:51d4:b0:5c3:c530:e99a with SMTP id 4fb4d7f45d1cf-5c3dc795d36mr14031787a12.15.1725944355364; Mon, 09 Sep 2024 21:59:15 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 4fb4d7f45d1cf-5c3ebd46c86si4876589a12.159.2024.09.09.21.59.14; Mon, 09 Sep 2024 21:59:15 -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=@takushoku.onmicrosoft.com header.s=selector2-takushoku-onmicrosoft-com header.b=SKktRvjG; arc=fail (body hash mismatch); 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 58C6968D996; Tue, 10 Sep 2024 07:59:10 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from OS0P286CU011.outbound.protection.outlook.com (mail-japanwestazon11020125.outbound.protection.outlook.com [52.101.228.125]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B543A68DE84 for ; Tue, 10 Sep 2024 07:59:01 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=EvLWI8TyAZ6qCD/YDiHxKQqZc259s1KCIYsRcswo0AVX7D1EyZ2cI6ObXM+S8oVweY7TkOxc78SxKpm/nSxqK1/uDQ/x4RvtFFOG1QjgxvXnCRW+9/vkHvwMVQeFTNOCt3FLpCR1WWsvbvqXAKRiv/xHBfiSyvQluIi/TyhBtlIyYsj802BWMIM/aK/qPqDhg401x064OaobXCpWvBDYFVhqRtFTY8Spml3cI3rKlk4S0oAIf5e1OR++eNmFaveJ7FYu/fbeLGVxcJk7ckk8UZLXBj+Ej1+x4kYz8fRp4ZvU1V1MDhE7wX1jmTqRi+spUXmApjiIYpWed9gWhS2N/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=h9z1X20IDfnTip3Q+UcIeDjZwhmHWfhnqiHU2zxWd8k=; b=rpelsE1FyUk9LswiRT4XanPOtyaagcwiz3o1B26PyV4rZgSDGCPTZRx23ChWwCpHGlTUN428iHnrEGc/+xzRT+8ylFYTq8x4e7Rj38sAydk+DhXZeiAgGCry1Y9klUkCZtsYLArm7K5UwonfytZ6qLz8eQSts2FgQySMIIUd+lyWgjLP+MZ47wJhYJp8F9KVhM5zeyBrzbU9jM3CyN8smxsaCokoxqw0Zt+sIdjdQ204SqUsHQep6RH8S5UuRINJ7pj2CF8P1prElMLdx7tJLbclv8RyvGerRJU2hmkZMouuNSCg3OsJMq+DQ+oEP+nVg0QJkU+ylrAT50CGOKh0Bw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=es.takushoku-u.ac.jp; dmarc=pass action=none header.from=es.takushoku-u.ac.jp; dkim=pass header.d=es.takushoku-u.ac.jp; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=takushoku.onmicrosoft.com; s=selector2-takushoku-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=h9z1X20IDfnTip3Q+UcIeDjZwhmHWfhnqiHU2zxWd8k=; b=SKktRvjGTCtZckET9VxqJ+iRSUjTSQ/BWtTlxiimWRoUJ3p3v47kycifn78lWqabP4+UdDjDswmSPVj5TTprw79rW/Nxurq54qTJ/O++edxKiTUBumOcRaCCRiG/UGV4tqzAykBBqOt4aO4LdmtF9QBa043eLYs6RqOoZH6WWpY= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=es.takushoku-u.ac.jp; Received: from OS0PR01MB6001.jpnprd01.prod.outlook.com (2603:1096:604:b7::12) by TYRPR01MB12758.jpnprd01.prod.outlook.com (2603:1096:405:1ba::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.24; Tue, 10 Sep 2024 04:58:56 +0000 Received: from OS0PR01MB6001.jpnprd01.prod.outlook.com ([fe80::181c:c1f4:9b58:f6cc]) by OS0PR01MB6001.jpnprd01.prod.outlook.com ([fe80::181c:c1f4:9b58:f6cc%3]) with mapi id 15.20.7939.022; Tue, 10 Sep 2024 04:58:56 +0000 From: Osamu Watanabe To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Sep 2024 13:58:34 +0900 Message-ID: <20240910045835.3205671-1-owatanab@es.takushoku-u.ac.jp> X-Mailer: git-send-email 2.43.0 X-ClientProxiedBy: TYCP301CA0039.JPNP301.PROD.OUTLOOK.COM (2603:1096:400:380::16) To OS0PR01MB6001.jpnprd01.prod.outlook.com (2603:1096:604:b7::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: OS0PR01MB6001:EE_|TYRPR01MB12758:EE_ X-MS-Office365-Filtering-Correlation-Id: 84f360c4-ae87-43d6-e852-08dcd1554601 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|41320700013|1800799024|52116014|376014|38350700014; X-Microsoft-Antispam-Message-Info: fC/oRJ/GH65sOOeh4A1v3lrrGsg8WacyFdq/5TJR0/St8gWfTp/IDnc958Wy88zVuIa+Cv5sotzJl1BkuIiyidSEpC0SGQwTsX2Lo+0N3sYJGHe5Z00grNcXvndoxhysAjtcsPBKTWFdTkXDrefArkoB5Xk2N+1o/rUiaNKTxIlVvWYTnOiI9BSL7aFE+WnCFSBPUNtw79h9GQavay/ajpxmF8VE/pztcgjZAZMgWs1w29FF4O3ezIqY+c573oSnjMSLLGeBQh2ckJ7UiDwldLJCrV75ZovjL9RALnVeoXpCqGsM5u2EjH+45exkKoXdQ+HeXO4l7t8VydFHpsoDjfc5td2RgL37QExRgODTqhm352xiNDIlif0dpTmp9NpztAPgxRGBvdumMPLwjPm8r7FBNRAv1ToGFAvCWyNX2XaavRvMLLViHJ1u6NqLzEbtPhOki6ydHDMCUUi74cD9DJngn4pkHtcJG3UE7ms3zInXNty0xQmj2SASIRSVwpAO7jgl8nT0pLSHl9n7g1BVkr95Z99THfNIFh/OE+kkivvUXx03Zo8GxmD2jFRnNgqvFazOc8GqIqN30avrPzoGjSyHm89XaGe9fl60zdzLlQwscdIBWgDxkTva4JkslJAzG1rEN+34ZAa4z/h/A0fECEKVXoNeHajhDvKolON2BsvyFdp2Lr+jk+NLzacyiwn1o603DFH3MX/pwFjK3cyJ8zA37KuYOSlKd+1LAa/34Ft+pGqbGE2IPzWIYqT5KyVMqCS0P2OlJgR35qC4kBSFVtjJYIvbAT5YacQ34thdWcm+UmRVoUVWBbUmkeeLga9uF1FUUoJf5nXJlxo3w7lDB5I4gL4+sTGr/eKq+i5mOJH/ScWFWNIWOXIzjisju4XCMw+GZnShe8ho9qpzsnZnsgtaLu/88sMqYU9YxMrNZdGRFkyeAC5cnLPPuRihW3nkdA/wZjsRm7DJ0O5/pzLCZxmG6dnYCJCjSZZLzyXlbYYIlAFrlHBXVom9JLlIJ1ckouy8HW4UGdxXCKKrFNgKDRxl1eC27/OruA0jr7NdBDA+GMz5zr30XCmgmp/J1BhhJncF/R4e6xiZNMpr1biikClcoGmX/9YeIIlgC94dg17runXoT4AUAL0jvpPhBqkVumMJz8DZCrwMdlBj8LwVMrpU5yKmjai1/RSpr/NWZJKYRTYYeqMs2pkzo1W73y7Wixtr4CPdaJ6SCLve445YGNQzEx3lv3h3g0OFxeOX1RkQ9COhs3F7AXGtLRSRpTzlNJTi7uGC3sxKp2XsigxfA2uCO/AWaZ1+3QiPXupAHrZagNRIfWQY4wgr7rPBIV5/tp6LTI7fjTHvp07iuNvCiSqJe/StRtrk4H/dtGXyA5Q= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:OS0PR01MB6001.jpnprd01.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(41320700013)(1800799024)(52116014)(376014)(38350700014); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: sPc7vHpxfhox4VW1Nco7Uxni7JBeVtk5TWLvIj5geGbX+k4EPEY2T3DMBSMFX27Kv5Of06MqFTo0dffeqi26lwwhczbxSfkCtk84XpQ59u80UPVs6r98uahHIwNz90I2siSII4itwz8iEbuxuZZY7AnZscn7ZYM9zPg3roq/eVhvBEBbhWDOa8k5Q8xX0WSgjkANUWHtQzsnp33ArxiNad2PGNoTz9ar4miWd46hSWJ5zCkJsg6z5ATvoYIK8dBWw0s3i43Dy5WObalneXfrVMDlrZnkRM3rX8kSQEw5qlHo9NbNuU2tBxENvRMs3TqO0ZDjL9JYf44mfUG57Zf3XWuuAFdB1NCapnx9+nsra85PZFd/vi83EFILREDOj1C8fFoH4mpyz+S5IAIb7dNK/hNYBalTv7ue/9X+fQFMFT9hlQaFu1/pEijgsKcsS8iRe89YAl3EtvQojQ8MZxUwWGBX05YfrkMWB2Gnhp9DJagccQF67xXV8NM50pR0a3aGTpchjZLj+nULRrhqcSM6D0ZgHnOLRhMZh4SAmDQnSAwxcniGhlWWWRqj/wzIpBP78zi3gYkvJcJpM604G65auZgK4DJFSZR6R5n8uqYTQ4Y25MpGenAi0qh40K5ROus9bjFYU7BwlYHtezKc4KEEzIZreweZ3652QZivvU7/E79ScXjcWcTkqzcN7UpCel6DEQ4s3ebiIUOj3Wh0v92LaPgNRcxcEo3aiRnErCR0TBDlguCErI3fHdb9nYzuNNBXBzu/4p/ms15rlR77OFZNQ/Cohm9zBG9c68OdxYzXaTSLzaP47ZDOYG+xctXUhpcyNYesHNaYiuw53JJx93SI3fWVJZzQhD//UlRedEuSz1HKZozymYHMN/+2d15+uv6+yBDMvgRFu9omngGmVk3p0/+Vn/LMic3hdmW1k9hyIYbQOMKoh7wJ3gWYwbDLa0Ab+LQDQnN55rBmzt60B/ZRpEkQ42fM4q5AkeWeeUYibka8We2YXgUKqj5ELFvp+ymVfBOw43ZGZPC9HWpFk2UYvcHB++goQF5qqTIEYAzOorPxrRo2tsYSGYg8OYa6eMVlA8/tTBOM2yxY9BAXufWGKGRwynRkqx3FNS2C4i4b3p4x2+VzbIeWPFsJbEPHe8oETB+jHNHoqZ2EU3OcqSLu9j6UafNA1FhtToZWYZme1rhh15PoQ/k1todibTZkycKckJzgmw6a9t+2wU0OHGxBgV5BMSkwRibIkmAoaSHARYlMrUQEcYrb28z2MBnAiyiz6paWCwYSH6Olm6u6LWfS07tPpcpw1meWq9QAcRTX95FrowhubJrXzs5oJxHf31zwwzp5/+IKuNnhgQ/I97jIdJorhQt1BbPFHAwy44J7UBqM34Wm2hr7200uPkCnUglA/gt7mbd7cuTeMRWpzuFolKGnki0gEUPYiUys6FSvzk0eV5/+wChS6QUs2jt5VRaxGuA60kq9mb9tDKTUFLaJVK5egYiqw72M/ErWHeZtz5ym78lcAhNgXFjxF+a11AjXz4wWNf0v+nl5cbrMXBcVuxmZg32ppbb5VkvQl0VE41ZapH/Or17CO6pOUM3Rncna16Cm0TwTtM5LoFLVUfP+9g== X-OriginatorOrg: es.takushoku-u.ac.jp X-MS-Exchange-CrossTenant-Network-Message-Id: 84f360c4-ae87-43d6-e852-08dcd1554601 X-MS-Exchange-CrossTenant-AuthSource: OS0PR01MB6001.jpnprd01.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Sep 2024 04:58:56.7584 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 853333e5-13b1-4738-ae04-bfb589cf2665 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: NrxBY0vY/bL/ns34rxR5LfsxwTUdA1UldUOcuWv9pjkI3jg28Q5FZZ/FOS8l6vETfLRodIssm90bkba57YM81omyHe1VBq+lKbvJwDCZ8b4= X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYRPR01MB12758 Subject: [FFmpeg-devel] [PATCH v4 1/2] avcodec/jpeg2000dec: Fix FF_DWT97_INT to pass the conformance testing defined in ISO/IEC 15444-4 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: Osamu Watanabe , pal@sandflow.com Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: iSmSaRctxANV This commit fixes the problem described below on the integer version of the inverse 9-7 DWT processing (FF_DWT97_INT, https://trac.ffmpeg.org/ticket/10123), which is activated with `-flags +bitexact`. I went through the code path for 9-7 transform (integer) and improved precision to match conformance codestream. As a result, the encoded codestream size is slightly larger for a given Q value. For example, `-flags +bitexact -i lena.pnm -q: 20 -format j2k -y tmp.j2c` gives 13K (HEAD) and 19K (with this patch)` - Problem - The tests for the following codestreams were failed with `-flags +bitexact`. - p0_04.j2k, p0_05.j2k, p0_09.j2k, p1_02.j2k, p1_03.j2k, p1_06.j2k. - ds0_ht_04_b11.j2k, ds0_ht_04_b12.j2k, ds0_ht_05_b11.j2k, ds0_ht_05_b12.j2k, ds0_ht_09_b11.j2k, ds1_ht_02_b11.j2k, ds1_ht_02_b12.j2k, ds1_ht_03_b11.j2k, ds1_ht_03_b12.j2k, ds1_ht_06_b11.j2k. - These failure comes from the insufficient precision of the fixed-point implementation of the 9-7 DWT. Signed-off-by: Osamu Watanabe --- libavcodec/jpeg2000.c | 6 ++---- libavcodec/jpeg2000dec.c | 2 +- libavcodec/jpeg2000dwt.c | 43 +++++++++++++++++++++++----------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index d6ffb02319..f1a7d55ae1 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -260,9 +260,7 @@ static void init_band_stepsize(AVCodecContext *avctx, band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4; break; } - if (codsty->transform == FF_DWT97) { - band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2); - } + band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2); } if (band->f_stepsize > (INT_MAX >> 15)) { @@ -270,7 +268,7 @@ static void init_band_stepsize(AVCodecContext *avctx, av_log(avctx, AV_LOG_ERROR, "stepsize out of range\n"); } - band->i_stepsize = band->f_stepsize * (1 << 15); + band->i_stepsize = lrint(band->f_stepsize * (1 << 15) + 0.5f); /* FIXME: In OpenJPEG code stepsize = stepsize * 0.5. Why? * If not set output of entropic decoder is not correct. */ diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 2e09b279dc..f68e41ed6a 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -2136,7 +2136,7 @@ static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk, int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x]; int *src = t1->data + j*t1->stride; for (i = 0; i < w; ++i) - datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16; + datap[i] = (int32_t)(src[i] * (int64_t)band->i_stepsize + (1 << 14)) >> 15; } } diff --git a/libavcodec/jpeg2000dwt.c b/libavcodec/jpeg2000dwt.c index 34e33553f7..d24f15d39b 100644 --- a/libavcodec/jpeg2000dwt.c +++ b/libavcodec/jpeg2000dwt.c @@ -39,12 +39,12 @@ /* Lifting parameters in integer format. * Computed as param = (float param) * (1 << 16) */ -#define I_LFTG_ALPHA 103949ll -#define I_LFTG_BETA 3472ll -#define I_LFTG_GAMMA 57862ll -#define I_LFTG_DELTA 29066ll -#define I_LFTG_K 80621ll -#define I_LFTG_X 53274ll +#define I_LFTG_ALPHA_PRIME 38413ll // = 103949 - 65536, (= alpha - 1.0) +#define I_LFTG_BETA 3472ll +#define I_LFTG_GAMMA 57862ll +#define I_LFTG_DELTA 29066ll +#define I_LFTG_K 80621ll +#define I_LFTG_X 53274ll #define I_PRESHIFT 8 static inline void extend53(int *p, int i0, int i1) @@ -234,8 +234,11 @@ static void sd_1d97_int(int *p, int i0, int i1) extend97_int(p, i0, i1); i0++; i1++; - for (i = (i0>>1) - 2; i < (i1>>1) + 1; i++) - p[2 * i + 1] -= (I_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16; + for (i = (i0>>1) - 2; i < (i1>>1) + 1; i++) { + const int64_t sum = p[2 * i] + p[2 * i + 2]; + p[2 * i + 1] -= sum; + p[2 * i + 1] -= (I_LFTG_ALPHA_PRIME * sum + (1 << 15)) >> 16; + } for (i = (i0>>1) - 1; i < (i1>>1) + 1; i++) p[2 * i] -= (I_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16; for (i = (i0>>1) - 1; i < (i1>>1); i++) @@ -276,7 +279,7 @@ static void dwt_encode97_int(DWTContext *s, int *t) // copy back and deinterleave for (i = mv; i < lv; i+=2, j++) - t[w*j + lp] = ((l[i] * I_LFTG_X) + (1 << 15)) >> 16; + t[w*j + lp] = l[i]; for (i = 1-mv; i < lv; i+=2, j++) t[w*j + lp] = l[i]; } @@ -293,7 +296,7 @@ static void dwt_encode97_int(DWTContext *s, int *t) // copy back and deinterleave for (i = mh; i < lh; i+=2, j++) - t[w*lp + j] = ((l[i] * I_LFTG_X) + (1 << 15)) >> 16; + t[w*lp + j] = l[i]; for (i = 1-mh; i < lh; i+=2, j++) t[w*lp + j] = l[i]; } @@ -301,7 +304,7 @@ static void dwt_encode97_int(DWTContext *s, int *t) } for (i = 0; i < w * h; i++) - t[i] = (t[i] + ((1<>1)) >> I_PRESHIFT; + t[i] = (t[i] + ((1<<(I_PRESHIFT))>>1)) >> (I_PRESHIFT); } static void sr_1d53(unsigned *p, int i0, int i1) @@ -471,8 +474,11 @@ static void sr_1d97_int(int32_t *p, int i0, int i1) for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++) p[2 * i] += (I_LFTG_BETA * (p[2 * i - 1] + (int64_t)p[2 * i + 1]) + (1 << 15)) >> 16; /* step 6 */ - for (i = (i0 >> 1); i < (i1 >> 1); i++) - p[2 * i + 1] += (I_LFTG_ALPHA * (p[2 * i] + (int64_t)p[2 * i + 2]) + (1 << 15)) >> 16; + for (i = (i0 >> 1); i < (i1 >> 1); i++) { + const int64_t sum = p[2 * i] + (int64_t) p[2 * i + 2]; + p[2 * i + 1] += sum; + p[2 * i + 1] += (I_LFTG_ALPHA_PRIME * sum + (1 << 15)) >> 16; + } } static void dwt_decode97_int(DWTContext *s, int32_t *t) @@ -500,9 +506,9 @@ static void dwt_decode97_int(DWTContext *s, int32_t *t) l = line + mh; for (lp = 0; lp < lv; lp++) { int i, j = 0; - // rescale with interleaving + // interleaving for (i = mh; i < lh; i += 2, j++) - l[i] = ((data[w * lp + j] * I_LFTG_K) + (1 << 15)) >> 16; + l[i] = data[w * lp + j]; for (i = 1 - mh; i < lh; i += 2, j++) l[i] = data[w * lp + j]; @@ -516,9 +522,9 @@ static void dwt_decode97_int(DWTContext *s, int32_t *t) l = line + mv; for (lp = 0; lp < lh; lp++) { int i, j = 0; - // rescale with interleaving + // interleaving for (i = mv; i < lv; i += 2, j++) - l[i] = ((data[w * j + lp] * I_LFTG_K) + (1 << 15)) >> 16; + l[i] = data[w * j + lp]; for (i = 1 - mv; i < lv; i += 2, j++) l[i] = data[w * j + lp]; @@ -530,7 +536,8 @@ static void dwt_decode97_int(DWTContext *s, int32_t *t) } for (i = 0; i < w * h; i++) - data[i] = (data[i] + ((1LL<>1)) >> I_PRESHIFT; + // We shift down by `I_PRESHIFT + 1` because the input coefficients `datap[]` were shifted down to 1 bit above from the binary point. + data[i] = (int32_t)(data[i] + ((1LL<<(I_PRESHIFT + 1))>>1)) >> (I_PRESHIFT + 1); } int ff_jpeg2000_dwt_init(DWTContext *s, int border[2][2],