From patchwork Sun Aug 4 05:27:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: WATANABE Osamu X-Patchwork-Id: 50885 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:1f5a:b0:489:2eb3:e4c4 with SMTP id jm26csp749827vqb; Sat, 3 Aug 2024 22:27:53 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUFn1D4WBv6EB2g4zR1s7pwP7cf+A9J6Y9LFBl5fADMQypyntUcz/Yzf3Yo/HqL1VEk158va/rvkuy9L4ECyxiLZVyL/KEjyictKw== X-Google-Smtp-Source: AGHT+IHD3mn7msAUkcEsBEm/4CpLS5M6/bTFLa7rbu0XD1UpbhgwTbXeviEsVHJcAdqdw3KuN1Hc X-Received: by 2002:a17:906:6a08:b0:a7a:a7b8:ada6 with SMTP id a640c23a62f3a-a7dc4ffa91dmr642325566b.39.1722749273188; Sat, 03 Aug 2024 22:27:53 -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 a640c23a62f3a-a7dc9c2777csi293447866b.273.2024.08.03.22.27.52; Sat, 03 Aug 2024 22:27:53 -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=LrKsJZID; 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 5012968D969; Sun, 4 Aug 2024 08:27:31 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from OS0P286CU011.outbound.protection.outlook.com (mail-japanwestazon11020088.outbound.protection.outlook.com [52.101.228.88]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7961A68D664 for ; Sun, 4 Aug 2024 08:27:28 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=B/mTDP4QcwYU5oxo0QiNMKC5rI7Agf/mpbYSNBGgESbBYvKedZvTz2UedxUMUMxCzJfXgnzC9rry6R0xPB3XbCMAHBnVXu4ZOnpZx9+W5al7FvHjiyPIFX+upSTzBj4p+Pftzoi33AROoWtXdm2yq0tNSrrAGr2J+8ESCRFPR2SEOyTFlYBkHIEfmYDZh/WuoInF5LN7c2Kmg2E0gW0QXBZwjk1+UaikgPGb+NvNykAm4J61n6ibHJ/eC5Nl1IPGnFgr9zAMLvp8hf/VDHIE4B0aAG6GxfoLbJrCxAXB0SsOQXE+PESlRK37LdOthOFZZsY3lnnIpLq0BM4XU0Ptjw== 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=pToajkh6llkNQsvoEWPt8lUemZbkHS+6w3nHwA0gbe4=; b=l/+tj21hpF0vz1ClvZmDT9uVP2a5oyQMEN81oX9/DBIrw6BIp0glWhaxI3HVHd6ReWgF+NVncXaYd1jhO6HhFQNyW6/GYwi7H3kBaKrRwPIjCmrAKKOxrGerU+FEO+eCKRl95cLg8sY6jCRbWen4mE0A2pafo5dttCpfj+9EFTSIGVjFdVCapd6DqmpnXXXakbJqQo+8vUvuBl9w9VS9Osk8nw/96DrrRSp7AcOFfs2KhAPzRnsRYOYHLNcC+JBukMek8HxuxRFD+tOqKkX3VkK+jDy43e4vMeRJxNVsZZzgbYlJHjnHX5Ty4BtYK5a0GKyrmX1IFL5XX/MegIsWdA== 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=pToajkh6llkNQsvoEWPt8lUemZbkHS+6w3nHwA0gbe4=; b=LrKsJZIDSp/xtirBlikV+cQIwEZtT450KzsGFke9cMZijHipBtwtj6KDdZyCtFSsq0O4nURy6nqY2/ls0gMQgqAYINzYQjzdTs8wVTitfNX6hsr1nsD/nMREt5P8kr05+B+yYZWgWCS5eJBBD+IKbyM9yVtTrmaXd8IuJ2dBmiU= 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 TY3PR01MB11348.jpnprd01.prod.outlook.com (2603:1096:400:3d0::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.22; Sun, 4 Aug 2024 05:27:13 +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.7828.023; Sun, 4 Aug 2024 05:27:13 +0000 From: Osamu Watanabe To: ffmpeg-devel@ffmpeg.org Date: Sun, 4 Aug 2024 14:27:06 +0900 Message-Id: <20240804052706.292107-3-owatanab@es.takushoku-u.ac.jp> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240804052706.292107-1-owatanab@es.takushoku-u.ac.jp> References: <20240804052706.292107-1-owatanab@es.takushoku-u.ac.jp> X-ClientProxiedBy: TYCPR01CA0149.jpnprd01.prod.outlook.com (2603:1096:400:2b7::9) To OS0PR01MB6001.jpnprd01.prod.outlook.com (2603:1096:604:b7::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: OS0PR01MB6001:EE_|TY3PR01MB11348:EE_ X-MS-Office365-Filtering-Correlation-Id: e4bc84a4-90f8-4142-25d2-08dcb4461846 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|41320700013|366016|1800799024|376014|52116014|38350700014; X-Microsoft-Antispam-Message-Info: dAcHUzJIhnytLGPfb5ocT5GZHUDafaaS0/p3RxEGX/lMULeEMFj/oA1+p1Fbw0tXz3AcFY/jipNiFpMcICF3l6TfCuvIomQQ6dDw699x2pdN5kID+KXpQjKraiAmkXKyEws+lfT3usMjv1vMLjYKWC/+ZxmUu/+uqZcYRPodwzBVPG53wcDNjj0pIEVJdtg0iCGRj6J7YTqFLPfYw8VAbn3YmuxOm8ox5YT/UiioKFTRmMBv3qeXC/+0kjQJ7DnkM86PbE1mRoPQaBeWCQ7yVhyFyGVdQ9Ps+cjajts2xHGosLMpIuQDnP7iI2tF7TC1bTT3ina3U2Qq6s4q07DdIC/sMIf0vjgCncB0yMDlK7EGDqMgeHxwcRFEPoUBUEmhXRCBmp0OScxBqX8Ff4RmC6gGRHagYIxsqtlyNeQ+wMvWX5n7YT5vvv2ZXsUK9FokCOfsqTFWLpvn9sqIRGxK9pJdyx7oTnufsnkYHzL9/efPHpx1kMs1bW4S7iBW3XNeThgfaYWrjeiBOALhrEohqfHoH/qpwE4/zJnH6ahFWvyjfrMIWh3AUJHwb6ysdiC4mdxYnIpkkcLfmp+MtIPEdC3eJk7uKnaB71xhJjNdALNQ7fGDf9dPmp9hRNtfnvIEOto7sl0Idm5aCrp3Zz7G788HRj6A4tfaLraevW2v9SqrYDh92FiLblAIyglURcPyfWKWQBk0zvAtuUKtY9BcIZ1zwU2Aik4vHXHOh7GH2WmnkSik0SEycdalGRd/vVVDEaaggdfzsipZWrRMrvWi8cLDRd5KkkrZoTfxcXvjvYNIrwyyjg5miW9gNSJ5nxvNFzHzPqcPF9OthX3imhDXiJshXzT8EhzzPufOzwCEl5VZdxcaM0ETfcjD7vXuipb/EYX4exFt3jMyqtKR1VfTpuFUMNkDTC0WZEyIyEVKnvnX2lK/9GIyb/P93Re6TJmM9E9ub6/dvKkabYqJNsfChx72oB59k7MtrTWSA5txQgLj3a0pSL7O0XFt1C4I7jASq3oh9d8QJJhvwtA0CStzsHM3fR+6tbXq15GgbXLAf7bKKUj3HEmdtvgOA9KkjRPdNGpbNfJjk4QBK32ZMPKSPnaGRzbVDlB+7RJYwKv/VFrzFt2mlTPDFLVrE3+u76QW+5cUrFVYgpgKbqhQMCsbs4fK5u1yuFdlndxi4ZMyQ9AXwZ3rqTjmg8T/ykryXyrqUtNKNJzATYf/9tH8szy5wXgQc3j1DxIlHlrX0ll5a/LwOJAfzGgzoR2fyiqmUqkVmEii78WgYuIedcvQJMkyH1IPWAb9BMcrvtlK9KqnibHs4i80fYpqLNk95tP/lDck4UMfcH/OkmmqayW2Eqjo/6GYTyU5ekp7Z08ooAiq1RojvvDBS7elA6RQTgJCp45EVl+1gT06GT4g2qb5INbDdg== 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)(41320700013)(366016)(1800799024)(376014)(52116014)(38350700014); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3P74jG5ziQ6Jz+dqeBEkJ42I5ENZdYP82oq48Dx8VKsX6dRqvYtx3NJ13nN5xDVtDLXi7vXlZ5YVtZw8Z5LinqpxBzpgvrKYZksQCOX/xNVnvcQUYYxfDLcjz+WxqZp0Mbl5srRye0QwHqnRCe/3P9KMkEDbuX4kO6iPA6aT/qzwPTmIXe8ObKlYqhrQZ7rAbVydNHKAZP3poGG7pqJvxWKXHW+mdSW8F/xYBer02vXlWnlPANW/+wlN5nhbsppEbB4Ui1SIsGf1Ydmpq7ONbFUXFEZHI7MS7CeSUfbZ8oz0QW6T/zOgPifL2KFh08GAFKhNNPJc4d7W6qDxepZt2LJ7nfXItZqpKInA5h2957jmxyI3aMGlcXk94iAxm4hlYhYXj9GpO94RZf83OEVg7pl2xP9w974kgLTnWG8B/HX8PspPNBJG3E0wKpVtLElgRiN/hlctEtBv0TuLpRYCcCh3YDZDAiSW5rs1cDlaoes6eTKvfTOTfgVnmGaBSigRRW4nHWXfKgFubGAWgYnaw3zzH0y8ZVuLzz/tPmlVB6MtVGQVk/V8mys8i/CuHuyeht4VSDynFoNRGvnPFdLJnOueHgpD4BC2woaaACXp22mb/BVh3fgtf36kYqOvGgbfdpQuCJAh9ishfjOmnmC9gGiu4sFcMEmIQfLBPpcB9obpIXXFZXs47b7JpxzOXqy7jX/xXf8stlqeqNFoigbuAPbiC1CmOAou+2s2NiyjqedewWvCqvAqgsyrat4R2dfGlai1DNBAAGrzyiBIU0I5uOxt+JD2JKUQyjl4VRgDO05xFXsc7BgsNJoKWAwLtjAH4j0QcMu03UiWOYwg/5O0Mm5nbVh6EseciPabGA13PqFxBkxu9LK5cWoXl3e2ln9Z0DKHN41Nhcx2iNYrhNOIjFW2Iexx1nNxzHdsSXx3N1+Ncu0HwAkJMfQpBYwSc1/yrNQUELwc4r0L3OHrqXX47WfZUFxpQQupPzaYIXfOn2a7W8Jxzn/t/sa6OkVMFBJUnIWCAV7xruIRkeV7y/WHj8G5zGfULDr77+4D5mD269IUtqJHnnyPFL5Lohf2x3KkuIgnwx/am3OW9jMNNRUkQ2S4TJsTUEPEqs076SP1TGEZRTldKx6ZiXsMjU5laVIdgitJ7yt1rRpgjolD8+Djw5t2LfQLjDQWYqePZjFg+8q3fYIjNJ1GtMNan2dP7AQ9psV73rc8Ii8SZ38Gwk0GPu+VjggF8o07Aln6qVI57AXpYv0Be3YRQzirgjQwObi0PKfaZ7/dgRFag6Qb2uZak7cA1jl1XKC9tuOyPVj+HkVf7P27AmJ5fM/s0uFh6XB6cf6XLCjbZ7ZSwKxjDUbC+PvWye5EphnMlSeRFwfly/8SuszrT6tuc6o+Yk5Brh8UT2dRTavg18aF2h5LRXBzQd95jiSg8Etu+voZdn2CwzGQw1vM6KKRbErOV1WUnDmzPMBuMeoXEbCE88IBnhGued3rpwRqOm/ml9KqvbbzCS9rhbYUL5+U5aQTMRrk0dYZjWVD4V7MFu/bpzJWblnQ2vITAe2gcu5npA7TpU0qgxtbz92phoobSqJl3MkphkUjne6Fxx9mqe5JecNzdgKBAw== X-OriginatorOrg: es.takushoku-u.ac.jp X-MS-Exchange-CrossTenant-Network-Message-Id: e4bc84a4-90f8-4142-25d2-08dcb4461846 X-MS-Exchange-CrossTenant-AuthSource: OS0PR01MB6001.jpnprd01.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Aug 2024 05:27:13.8461 (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: QnSybbjVfOwpzHNOWiUIwxeOIC4Ud7bkVmmZ+1jflN/g7Y5djBnbO8sE1+y+UX/Ao8EUqF0SKBSJaDg6fTc55zmHok7Cl032d32Klc9guJU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: TY3PR01MB11348 Subject: [FFmpeg-devel] [PATCH v7 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: YALAIDvftST0 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 | 136 ++++++++++++++++++++++--------------- libavcodec/jpeg2000htdec.h | 2 +- 3 files changed, 89 insertions(+), 60 deletions(-) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index c8759b6d51..fe6b542110 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..0296792a6a 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, @@ -1060,6 +1062,18 @@ static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s 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) } } } @@ -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 */