From patchwork Wed Aug 7 03:48:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Osamu Watanabe X-Patchwork-Id: 50922 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:d7b2:0:b0:489:2eb3:e4c4 with SMTP id dc18csp129332vqb; Tue, 6 Aug 2024 20:49:29 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWEVZnL10uxM0ZV4mqjgns/3VjXWku8iCDJ0dVDVYVKwOTJ/jMX5dDW6wuoNQ1TRjTWPK5d7YMgbxBIJi7ilngqGnsI76JoqtsIDg== X-Google-Smtp-Source: AGHT+IHV04tmlKrJFzBoxSF0B/dEyEuXoe+hDOiiWyWhH0+pSP/YWJHbkSGmH4MXf8m7NQJyJNkL X-Received: by 2002:a05:6512:3d89:b0:52e:987f:cfe4 with SMTP id 2adb3069b0e04-530bb396e7amr10862366e87.30.1723002569337; Tue, 06 Aug 2024 20:49:29 -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-5b83c523c10si6217927a12.483.2024.08.06.20.49.28; Tue, 06 Aug 2024 20:49:29 -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=QZ2K4Ehy; 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 4D02968DB10; Wed, 7 Aug 2024 06:49:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from OS0P286CU011.outbound.protection.outlook.com (mail-japanwestazon11020077.outbound.protection.outlook.com [52.101.228.77]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A240168DACE for ; Wed, 7 Aug 2024 06:49:07 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=w763YiQTAkd/hMuC87Hj1K6jULEuD7mjy+ASrdHYwRL4MA47K7NkiItwgJDRMo4O9X/PkcoRqlSIBklFg4Kwvr6Kq1A76NljgTgaKrrt9o+vvDs2TYONGmDzAarfEl4oars/f+CtrCkZ7RSavD+8TX3wyg1lDbZjYbvUxo23AwTHXbFY9XiA5dG1EC9wFGyQcbLtIHpPuAb/1ZNYFUfTsIpeRpYMnQhgLoncEaqxsOk4JtpSc7fb0XBmfdJA7GJFZOUczUsK9pAc8atnti0QLrpleeUhMCg7S8HVvpWMV4F2S1IDSK96UErru0Tj+5LFuqvhNUtp2l5CPN6fYh7Ixw== 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=b5GKOCswhUPdRF+yJ52yNcb4wkqwleTxv15PMnMqhtw=; b=sSyu3Zco59CL5y/4OJN+vSI0fC2jSpx+vf1t6d+/nVXtuOs/rrpjWQLCxSvLYTVJBWAsuLCTO1O6FQNZHod+oYDh9Gl38w9HQ0bBuDdZtRkaliYbnu3CInaqTO7Pn83XRONteUKrg2dr4hyQYMaQAGnH9VIARkFrP7mZQYAP0xMKbrEMEhrYerykKqxO69JRCp2zYgaokGB0PxqiXgA2vRh9/8dAvSrWCtiaRNtztK06LXyronOe8cSebhLRTASdF4GUd1SgZrTDB0FPJMCfS/zkLm+PuF6Q4oFuvHTKIk0Qe+Jicnm35xx5PdHy6b+88tESabvKwuZ+BNx9q2Lw+g== 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=b5GKOCswhUPdRF+yJ52yNcb4wkqwleTxv15PMnMqhtw=; b=QZ2K4EhyG/A9tb3PEFRLney5sS9vJ/ndV3aMyytDRTtXrO1tZyisKHiRHMDkZDjTWofqeXhAuJuwHDhS9pOqNhT76G3CwIS/etPKbz5loFuZFX6omYwLOLQ20PQGXjp2oxliopWGRt2AZjv7IqaY3IDDkTnBkS90qjpevZHAlDg= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=es.takushoku-u.ac.jp; Received: from TYCPR01MB6016.jpnprd01.prod.outlook.com (2603:1096:400:4c::12) by OSCPR01MB12849.jpnprd01.prod.outlook.com (2603:1096:604:337::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.13; Wed, 7 Aug 2024 03:48:53 +0000 Received: from TYCPR01MB6016.jpnprd01.prod.outlook.com ([fe80::32fe:be82:2878:fc7c]) by TYCPR01MB6016.jpnprd01.prod.outlook.com ([fe80::32fe:be82:2878:fc7c%7]) with mapi id 15.20.7849.008; Wed, 7 Aug 2024 03:48:53 +0000 From: Osamu Watanabe To: ffmpeg-devel@ffmpeg.org Date: Wed, 7 Aug 2024 12:48:45 +0900 Message-Id: <20240807034845.363526-3-owatanab@es.takushoku-u.ac.jp> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240807034845.363526-1-owatanab@es.takushoku-u.ac.jp> References: <20240807034845.363526-1-owatanab@es.takushoku-u.ac.jp> X-ClientProxiedBy: TY2PR02CA0035.apcprd02.prod.outlook.com (2603:1096:404:a6::23) To TYCPR01MB6016.jpnprd01.prod.outlook.com (2603:1096:400:4c::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYCPR01MB6016:EE_|OSCPR01MB12849:EE_ X-MS-Office365-Filtering-Correlation-Id: fda12fa3-ded3-4201-5c2e-08dcb693da5c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|41320700013|1800799024|376014|52116014|38350700014; X-Microsoft-Antispam-Message-Info: vRO8fNp5r5M/p11Xk2+tkFmoWFdL3B25icmxoQ+aSA5fhRQ0FpwDHVHZB07JgJe6X5Rdbn1vDkVxhuqnL9lRKNR5LMeUvAMAmRTJj/VZ8ob8lSLcP04QXn9OsMyhH51Afnn3R5HZ0u//VWnc2XHSma9GA67xnZKh5miM4TqIqvoS/qr6slQ9NyIRPPixZWezK9UgFi1jxOJW8pWOXFEuHkS74DDbe25rlsYvFpy8fLXFlhZTbrtSp8+arcSwsn4FM/bOrKMaU9ysBd/xQDw074OBCk/1Pl35AXdZ+YRwtYgQ5nEOJNpT3C5yrRR8Jq6A8bfXA7ZqWSEdgsmf637REwPicHzyVSEhTB8ly0OAwuK3YWapOAYDV3oA3V5Top+nFs0Q1fhUEabNMliz/5PWiK3LSjChBtWOFl7XcVQR5SpHAQrqRb1iJT/utJ6Q5L42AdjGysFN56uX7nWAA//z0Ou4Q4zkNGZvO+Wqq82OzV2TZuuNNKgk9yvU/WU94xhpR0UFfOpr0FGVUJDEldl7q24urRqd/t4gyeBE7IiLQWImtGxhoCm64mnRmTMThV/Ngjly4WnkXEU6IZOQzy6CSdpONq6uLVJpoSllwKTPUHOZTGPSgAKSBRhl7kQNYcmIvz5fBtMnKvXrd8r3pYwoYCKmOuSxLyF+Mb2+SLahDn+mzi5h9YmzL0CIKYttU/AuosgppwulUT4yudf5w9FhosAT8T/iX6OZnYik5Ylw08ffTxH/EnYg0mgRDHXxJbGtQFkvJlSjb/mePwl+2cjvumDWUU9lvF1XJckJQ6YZ5lakXdrq5JMCLlPewJMD58FazxytWJkeoa2oQsbmoapfVoeJ2mGNpKPa1YckLb7N0ML0RT3mqtvW3+07QIZbgAjo9CTsUD2rh5JZPwOEMkzoiKRLhny+MNJP8Hm0KpX1Lr9GzWN4S2CqWNKSSkr9o25Ux8OCLwnSGrKomFmdGMIP+AtRSRkVmRnQTcD1mRTxVZN7RLOx3eON2n+mSZd/SgMui6wamDqCR7YRCpEmLXgRO1jlrViCzdemGS/8ZpkOvrW5xUfpeaW2YNU97aLTgmak5946D/DxML7jUX/wEVWb4IrUHrUB7lVes4KRAuQHDuWpXSrXh+BCqid25R0xXrI274+3lFwmdoQcMg1DpZOw0qe6o603RAnI+ZQ/OaEAIXfAAnHTS/ARe2zLRzFFc2Y4Qu/miaIBv1jzZb89t8JQvMAGW03j5V8b4LP8lyjGV4o/X0GN5xbQi393IRsphw9IEyQYZ5x/Z0pMXpXjaR6vtxfuoZWDPUgDUVRF63ykuY/b8xYhULjRgw/L6AGt26+XWzWv98t0Uqf3ylEVA0Pzpp+CMIa/fX7H/NkP10SMq3FE2e+i43GevtQ0ZJZY9sV4nKHcW7B5qZqw6RyGinDvWA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:TYCPR01MB6016.jpnprd01.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(41320700013)(1800799024)(376014)(52116014)(38350700014); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 812Ccq7ugjYoXJA0w7OO9uhspkDDQRBilV1k+37pxQTCH6BBhnFx3RxvbAkIjXMJ5oM2pz1hPkiDFHyJ8GcnqhHPzu1LaJ5ZifNmMB1uz1PqAROK+gHLeiQ1FtIrGTkX+hldCfIIT8CmwmLyJUPHighYvHXc/1TDab2E7KtCwZMCsSxWrpGzoYcgKoj056vzjUrH1xRL8wVwdit8dnDfp/I3+peAlAkAMB7nK0ColDVArMz7hyS1jEFgGiDIk5IW6UPByBLfmSwmP0hBi5w10a+rlJfHs5MLvl9xqkRDt0rCck1cUs10IbBUmFSwDMAmx2MM3qMYrwfX/ZVcraOeuA70DCsuNbAY/RwU+v5rxMtCB3uTuZVGEIa2bWFcZmJVAO/XNvbnn2bilQs+te0yn1WtQvIA1uvqLYm4t8KG5zC5Y0qh2i7XnTz4DPVgHBx55eaqEQrgt1duTBDAKcfwlF6ByEF91biQwr5d2Nlrb+2VHV/KMI1jryHdMPQakn/GykLUW6Xu3ryHTAI0j4hS0vclImGzl16ViDWhOEeBeu+yCPy4gnDRpGDC0XuFcnI4ffzOc+rCjVj2D8I02p3tlH9YKt2VGO3c3yCEeekyHFNJA4O+T9nruoe/7X58xtoLx0S8C65uyRC9CQjSMvtfwVdtwYIccNlTMeYTy+FU4nk1ZOzYr2tRsB964dgz2+pPi0qXxVTLqk/AjW+PVgJaEwb4FobIjHQH3fygsAswSzPdqSroIH1FbSF4N7e7Lx6ObNBj3PttN0P+ORh9yUPOIGyIdoOU1tGMx/H/M4laPqo1QOEm4K9vYzjlB874F8Hd8zuXHVmrgVzCKi9aElPlH8JNGRdrKqq8nr9PqCThxsQehLcUAyBkL7QcLQcwLrT9Z+Xl8LmI4NpvhMXIWHtSG1DHqznWUe3NDdX9A0JlXdtFL+NX3FdqFmUlWNxZJAFAeegjdFO7NQkJiyIgOqgxulF/SNyemb+bEJq6S6BNMZXqjSkdABCZojgRFdzCD/SijwNKzuhIggA+g59aSB6aHJTNFKoqawE9cToc8I3FqXxTp+LQjYoZgJ0YM1BiZM9AaAyYRIpMTf9uVtMMgDBg0DvMkH1Y241i1OagP6qkY6xglaYEAEL8xpDZ9VYmjg68ZkwU+h2H8yePUV7ykgNUyozz6l5b2ZZ2cq2+DKInDvx9M2vZXvsJJbYPLWRQpZUOE0m68G7JWE2PmRgXWreUkQ8XpQnHN0DTUviFZDYjeZNrf/7N0zFYj4eGqbXTAWue1b2NsZhV4XBWsuZOe3O+bjZiOdfEhrkhYnrsILZsM0IEQrat7gLBDWcse48BDPDpyL3sjnagLe06gN2ZgzCrxRD209ezl5U2KQB0a6VQWuJQVrp+9qHFcabmubuoDS22zk2uD9uNfjVz09lTZ/uKZ1CKV4WyKsT80gXv0WY9UyMOdfCOte0Mo/py0CcM7f4ZQ2miY9CP3ApnD3lQAP4AD1WDyqKWJ0JP7CBwSTOIpBnuLoNgIiVUnB0sHW+/ZqidAdcJV8fuOObaZ34LdAoqjFugFeDrbiwAh2S1l5fTeYX3Um3+cMn8gP7ErfBGSlCwJyWh8ot8dn9PFIvNq4QRbg== X-OriginatorOrg: es.takushoku-u.ac.jp X-MS-Exchange-CrossTenant-Network-Message-Id: fda12fa3-ded3-4201-5c2e-08dcb693da5c X-MS-Exchange-CrossTenant-AuthSource: TYCPR01MB6016.jpnprd01.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Aug 2024 03:48:53.0580 (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: atkn5X3Oz63cxjeO7utY4oOrzSOiAAH3VM523NCP6FQOaxoT5tb8s7buWK+xDc+O1QCkMwU8ADawOpy9mWyjnpcqdxVB60JiEDDThoHJbDs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: OSCPR01MB12849 Subject: [FFmpeg-devel] [PATCH v8 3/3] avcodec/jpeg2000dec: Fix HT decoding 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: pySFwzbv77A8 This commit fixes wrong treatment of MAGBP value in Ccap15 and bugs in HT block decoding. Signed-off-by: Osamu Watanabe --- libavcodec/jpeg2000dec.c | 11 +-- libavcodec/jpeg2000htdec.c | 144 ++++++++++++++++++++++--------------- libavcodec/jpeg2000htdec.h | 2 +- 3 files changed, 93 insertions(+), 64 deletions(-) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index d13eedfaf7..2e09b279dc 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -391,6 +391,9 @@ static int get_siz(Jpeg2000DecoderContext *s) } else if (ncomponents == 1 && s->precision == 8) { s->avctx->pix_fmt = AV_PIX_FMT_GRAY8; i = 0; + } else if (ncomponents == 1 && s->precision == 12) { + s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE; + i = 0; } } @@ -2204,7 +2207,7 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile Jpeg2000Band *band = rlevel->band + bandno; int cblkno = 0, bandpos; /* See Rec. ITU-T T.800, Equation E-2 */ - int magp = quantsty->expn[subbandno] + quantsty->nguardbits - 1; + int M_b = quantsty->expn[subbandno] + quantsty->nguardbits - 1; bandpos = bandno + (reslevelno > 0); @@ -2212,8 +2215,8 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile band->coord[1][0] == band->coord[1][1]) continue; - if ((codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F) && magp >= 31) { - avpriv_request_sample(s->avctx, "JPEG2000_CTSY_HTJ2K_F and magp >= 31"); + if ((codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F) && M_b >= 31) { + avpriv_request_sample(s->avctx, "JPEG2000_CTSY_HTJ2K_F and M_b >= 31"); return AVERROR_PATCHWELCOME; } @@ -2234,7 +2237,7 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk, cblk->coord[0][1] - cblk->coord[0][0], cblk->coord[1][1] - cblk->coord[1][0], - magp, comp->roi_shift); + M_b, comp->roi_shift); else ret = decode_cblk(s, codsty, &t1, cblk, cblk->coord[0][1] - cblk->coord[0][0], diff --git a/libavcodec/jpeg2000htdec.c b/libavcodec/jpeg2000htdec.c index 9b473e11d3..c47c8d61fe 100644 --- a/libavcodec/jpeg2000htdec.c +++ b/libavcodec/jpeg2000htdec.c @@ -122,7 +122,7 @@ static void jpeg2000_init_mel(StateVars *s, uint32_t Pcup) static void jpeg2000_init_mag_ref(StateVars *s, uint32_t Lref) { - s->pos = Lref - 2; + s->pos = Lref - 1; s->bits = 0; s->last = 0xFF; s->tmp = 0; @@ -145,9 +145,10 @@ static void jpeg2000_init_mel_decoder(MelDecoderState *mel_state) static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *array) { uint64_t tmp = 0; - int32_t position = buffer->pos - 4; uint32_t new_bits = 32; + buffer->last = array[buffer->pos + 1]; + if (buffer->bits_left >= 32) return 0; // enough data, no need to pull in more bits @@ -157,9 +158,24 @@ static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *ar * the bottom most bits. */ - for(int i = FFMAX(0, position + 1); i <= buffer->pos + 1; i++) - tmp = 256*tmp + array[i]; - + if (buffer->pos >= 3) { // Common case; we have at least 4 bytes available + tmp = array[buffer->pos - 3]; + tmp = (tmp << 8) | array[buffer->pos - 2]; + tmp = (tmp << 8) | array[buffer->pos - 1]; + tmp = (tmp << 8) | array[buffer->pos]; + tmp = (tmp << 8) | buffer->last; // For stuffing bit detection + buffer->pos -= 4; + } else { + if (buffer->pos >= 2) + tmp = array[buffer->pos - 2]; + if (buffer->pos >= 1) + tmp = (tmp << 8) | array[buffer->pos - 1]; + if (buffer->pos >= 0) + tmp = (tmp << 8) | array[buffer->pos]; + buffer->pos = 0; + tmp = (tmp << 8) | buffer->last; // For stuffing bit detection + } + // Now remove any stuffing bits, shifting things down as we go if ((tmp & 0x7FFF000000) > 0x7F8F000000) { tmp &= 0x7FFFFFFFFF; new_bits--; @@ -176,13 +192,11 @@ static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *ar tmp = (tmp & 0x0000007FFF) + ((tmp & 0xFFFFFF0000) >> 1); new_bits--; } - - tmp >>= 8; // Remove temporary byte loaded + tmp >>= 8; // Shifts away the extra byte we imported /* Add bits to the MSB of the bit buffer */ buffer->bit_buf |= tmp << buffer->bits_left; buffer->bits_left += new_bits; - buffer->pos = FFMAX(0, position); return 0; } @@ -406,6 +420,7 @@ static void recover_mag_sgn(StateVars *mag_sgn, uint8_t pos, uint16_t q, int32_t E[n] = 32 - ff_clz(v[pos][i] | 1); mu_n[n] = (v[pos][i] >> 1) + 1; mu_n[n] <<= pLSB; + mu_n[n] |= (1 << (pLSB - 1)); // Add 0.5 (reconstruction parameter = 1/2) mu_n[n] |= ((uint32_t) (v[pos][i] & 1)) << 31; // sign bit. } } @@ -414,7 +429,7 @@ static void recover_mag_sgn(StateVars *mag_sgn, uint8_t pos, uint16_t q, int32_t static int jpeg2000_import_bit(StateVars *stream, const uint8_t *array, uint32_t length) { int cond = stream->pos < length; - int pos = FFMIN(stream->pos, length); + int pos = FFMIN(stream->pos, length - 1); if (stream->bits == 0) { stream->bits = (stream->tmp == 0xFF) ? 7 : 8; stream->pos += cond; @@ -426,14 +441,22 @@ static int jpeg2000_import_bit(StateVars *stream, const uint8_t *array, uint32_t static int jpeg2000_peek_bit(StateVars *stream, const uint8_t *array, uint32_t length) { + uint8_t bit; + if (stream->bits == 0) { - int cond = stream->pos < length; - int pos = FFMIN(stream->pos, length); - stream->bits = (stream->tmp == 0xFF) ? 7 : 8; - stream->pos += cond; - stream->tmp = cond ? array[pos] : 0xFF; + stream->bits = (stream->last == 0xFF) ? 7 : 8; + if (stream->pos < length) { + stream->tmp = array[stream->pos]; + stream->pos++; + } else { + stream->tmp = 0; + } + stream->last = stream->tmp; } - return (stream->tmp >> stream->bits) & 1; + bit = stream->tmp & 1; + stream->tmp >>= 1; + stream->bits--; + return bit; } static int jpeg2000_decode_mel_sym(MelDecoderState *mel_state, @@ -994,39 +1017,18 @@ static void jpeg2000_calc_mbr(uint8_t *mbr, const uint16_t i, const uint16_t j, const uint32_t mbr_info, uint8_t causal_cond, uint8_t *block_states, int stride) { - int local_mbr = 0; - - local_mbr |= jpeg2000_get_state(i - 1, j - 1, stride, HT_SHIFT_SIGMA, block_states); - local_mbr |= jpeg2000_get_state(i - 1, j + 0, stride, HT_SHIFT_SIGMA, block_states); - local_mbr |= jpeg2000_get_state(i - 1, j + 1, stride, HT_SHIFT_SIGMA, block_states); - - local_mbr |= jpeg2000_get_state(i + 0, j - 1, stride, HT_SHIFT_SIGMA, block_states); - local_mbr |= jpeg2000_get_state(i + 0, j + 1, stride, HT_SHIFT_SIGMA, block_states); - - local_mbr |= jpeg2000_get_state(i + 1, j - 1, stride, HT_SHIFT_SIGMA, block_states) * causal_cond; - local_mbr |= jpeg2000_get_state(i + 1, j + 0, stride, HT_SHIFT_SIGMA, block_states) * causal_cond; - local_mbr |= jpeg2000_get_state(i + 1, j + 1, stride, HT_SHIFT_SIGMA, block_states) * causal_cond; - - local_mbr |= jpeg2000_get_state(i - 1, j - 1, stride, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i - 1, j - 1, stride, HT_SHIFT_SCAN, block_states); - local_mbr |= jpeg2000_get_state(i - 1, j + 0, stride, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i - 1, j - 1, stride, HT_SHIFT_SCAN, block_states); - local_mbr |= jpeg2000_get_state(i - 1, j + 1, stride, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i - 1, j + 1, stride, HT_SHIFT_SCAN, block_states); - - local_mbr |= jpeg2000_get_state(i + 0, j - 1, stride, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 0, j - 1, stride, HT_SHIFT_SCAN, block_states); - local_mbr |= jpeg2000_get_state(i + 0, j + 1, stride, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 0, j + 1, stride, HT_SHIFT_SCAN, block_states); - - local_mbr |= jpeg2000_get_state(i + 1, j - 1, stride, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 1, j - 1, stride, HT_SHIFT_SCAN, block_states) * causal_cond; - local_mbr |= jpeg2000_get_state(i + 1, j + 0, stride, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 1, j + 0, stride, HT_SHIFT_SCAN, block_states) * causal_cond; - local_mbr |= jpeg2000_get_state(i + 1, j + 1, stride, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 1, j + 1, stride, HT_SHIFT_SCAN, block_states) * causal_cond; - - *mbr |= local_mbr; + uint8_t *state_p0 = block_states + i * stride + j; + uint8_t *state_p1 = block_states + (i + 1) * stride + j; + uint8_t *state_p2 = block_states + (i + 2) * stride + j; + + uint8_t mbr0 = state_p0[0] | state_p0[1] | state_p0[2]; + uint8_t mbr1 = state_p1[0] | state_p1[2]; + uint8_t mbr2 = state_p2[0] | state_p2[1] | state_p2[2]; + *mbr = mbr0 | mbr1 | (mbr2 & causal_cond); + *mbr |= (mbr0 >> HT_SHIFT_REF) & (mbr0 >> HT_SHIFT_SCAN); + *mbr |= (mbr1 >> HT_SHIFT_REF) & (mbr1 >> HT_SHIFT_SCAN); + *mbr |= (mbr2 >> HT_SHIFT_REF) & (mbr2 >> HT_SHIFT_SCAN) & causal_cond; + *mbr &= 1; } static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s, @@ -1051,15 +1053,27 @@ static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s modify_state = block_states[(i + 1) * stride + (j + 1)]; modify_state |= 1 << HT_SHIFT_SCAN; if (mbr != 0) { - modify_state |= 1 << HT_SHIFT_REF_IND; + modify_state |= 1 << HT_SHIFT_REF_IND; bit = jpeg2000_peek_bit(sig_prop, magref_segment, magref_length); - modify_state |= bit << HT_SHIFT_REF; - *sp |= bit << pLSB; + modify_state |= bit << HT_SHIFT_REF; + *sp |= bit << pLSB; *sp |= bit << (pLSB - 1); // Add 0.5 (reconstruction parameter = 1/2) } jpeg2000_modify_state(i, j, stride, modify_state, block_states); } } + // decode sign + for (int j = j_s; j < j_s + width; j++) { + for (int i = i_s; i < i_s + height; i++) { + uint8_t bit; + int32_t *sp = &sample_buf[j + (i * (stride))]; + uint8_t *state_p = block_states + (i + 1) * stride + (j + 1); + if ((state_p[0] >> HT_SHIFT_REF) & 1) { + bit = jpeg2000_peek_bit(sig_prop, magref_segment, magref_length); + *sp |= (int32_t)bit << 31; + } + } + } } /** @@ -1130,7 +1144,8 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int uint16_t height = 4; uint16_t i_start = 0; int32_t *sp; - + int32_t bit; + int32_t tmp; jpeg2000_init_mag_ref(&mag_ref, magref_length); for (int n1 = 0; n1 < num_v_stripe; n1++) { @@ -1141,9 +1156,13 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int * Rec. ITU-T T.814, Figure 7. */ sp = &sample_buf[j + i * stride]; - if (jpeg2000_get_state(i, j, width, HT_SHIFT_SIGMA, block_states) != 0) { - jpeg2000_modify_state(i, j, width, 1 << HT_SHIFT_REF_IND, block_states); - *sp |= jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length) << pLSB; + if (jpeg2000_get_state(i, j, stride, HT_SHIFT_SIGMA, block_states) != 0) { + jpeg2000_modify_state(i, j, stride, 1 << HT_SHIFT_REF_IND, block_states); + bit = jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length); + tmp = 0xFFFFFFFE | (uint32_t)bit; + tmp <<= pLSB; + sp[0] &= tmp; + sp[0] |= 1 << (pLSB - 1); // Add 0.5 (reconstruction parameter = 1/2) } } } @@ -1153,9 +1172,13 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int for (int j = 0; j < width; j++) { for (int i = i_start; i < i_start + height; i++) { sp = &sample_buf[j + i * stride]; - if (jpeg2000_get_state(i, j, width, HT_SHIFT_SIGMA, block_states) != 0) { - jpeg2000_modify_state(i, j, width, 1 << HT_SHIFT_REF_IND, block_states); - *sp |= jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length) << pLSB; + if (jpeg2000_get_state(i, j, stride, HT_SHIFT_SIGMA, block_states) != 0) { + jpeg2000_modify_state(i, j, stride, 1 << HT_SHIFT_REF_IND, block_states); + bit = jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length); + tmp = 0xFFFFFFFE | (uint32_t)bit; + tmp <<= pLSB; + sp[0] &= tmp; + sp[0] |= 1 << (pLSB - 1); // Add 0.5 (reconstruction parameter = 1/2) } } } @@ -1197,7 +1220,7 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c int32_t n, val; // Post-processing const uint32_t mask = UINT32_MAX >> (M_b + 1); // bit mask for ROI detection - + uint8_t num_rempass; const int quad_buf_width = width + 4; @@ -1233,6 +1256,9 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c } Dcup = cblk->data; Dref = cblk->data + Lcup; // Dref comes after the refinement segment + + cblk->data[cblk->length] = 0xFF; // an extra byte for refinement segment (buffer->last) + S_blk = p0 + cblk->zbp; cblk->zbp = S_blk - 1; pLSB = 30 - S_blk; diff --git a/libavcodec/jpeg2000htdec.h b/libavcodec/jpeg2000htdec.h index 572d095c92..8d6919a0de 100644 --- a/libavcodec/jpeg2000htdec.h +++ b/libavcodec/jpeg2000htdec.h @@ -29,6 +29,6 @@ int ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width, - int height, int magp, uint8_t roi_shift); + int height, int M_b, uint8_t roi_shift); #endif /* AVCODEC_JPEG2000HTDEC_H */