From patchwork Sun Aug 4 05:27:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Osamu Watanabe X-Patchwork-Id: 50884 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:1f5a:b0:489:2eb3:e4c4 with SMTP id jm26csp749801vqb; Sat, 3 Aug 2024 22:27:44 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUXHwiMRXorhozaqOkJv9jiaFE7nUX6+++El8SYgUz1JjvZycEQJd1PQCUaWF7Bf5o3BaNGmAF8X6dgyEs+dJFC63w+7aSAdsS6pg== X-Google-Smtp-Source: AGHT+IFcNHyGrxBB/GKUZ8N/FSgzI/M8aD+VMNduuShj6OTUBle5p32mzmGaRL1gZWjZJ6mHa79c X-Received: by 2002:a05:6512:3a93:b0:52e:ff72:38f0 with SMTP id 2adb3069b0e04-530bb36bb71mr6153736e87.1.1722749263795; Sat, 03 Aug 2024 22:27:43 -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 2adb3069b0e04-530bba46790si1532062e87.612.2024.08.03.22.27.43; Sat, 03 Aug 2024 22:27:43 -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=oEvI97JR; 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 31F9768D655; Sun, 4 Aug 2024 08:27:29 +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 49CF568D655 for ; Sun, 4 Aug 2024 08:27:22 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=UJvx6ozdeVx9lTPayRQFc5ocY2V8N8yBzbXUGtwtP/58ufT9n57KT+pVrm3kXO6QN/Ya6t85AnFnfCRDb1AXEI0zc1WZgBNgR0lD5UIAa2ZrGLh8+NuMMI7+XiDuNfv6Q0xsZctHiGfgdrcntvUuM4+sLWOQol4Whuw1kXembpfRFKQIqIW/bpy2+ZMN3F7G/Lw9UmJfvHRf5TC52g9ZV88r5dXNjJgLpW4inmBjdt6gtWtHXgFUXmut8wXLftUw3hJcRuKxuJYdL2EWRAVO9wwzNh0o12CMOPGM7zT3p4jNjk6m7v153v+FUEO1TJpB4Q0iyqqweyiiSFzwCVGfIg== 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=HajqVhOoB1GkuxFRf6os8UEg3gGESwvZn278CIfEZxw=; b=T1tHCNmIDKtKQ1SnZWoHmihUpZAGv5xPCV/bYcWD4F32Gv5nC7p5U+AEBiVdLeiXD9jqAGeh8pRZvIF7Udp4Z2ewmXJXd1N2yhsKMOzUdGo38v/qYgZjANpf8Rj3C2udSiKCClPF/oUn/ZfYgGfP3KOZ7xZcPNcsKiMV4cKjAFYCVO6ew5HBMgF7mWGOzAAmCrYdkIunCS3V5rrLmC40AeiqPgBCchiU5BH+yVwTq9N0hmpzmlNgj/eYpYK3oxq7brwLUYrH0Q0UxJ2gvDGl4vC38jH64B7wc88xVt6puZX6WFqzFE4Kz/vfBksoZEPOfWq+ZByYmqCly7NMXq93JQ== 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=HajqVhOoB1GkuxFRf6os8UEg3gGESwvZn278CIfEZxw=; b=oEvI97JRoEJmoWi4xH7CVrgtqMv24H1LAvcNS6HBZzkj+5p0C0PuFLMp6TmAlQkxEnMGAPj4cws3ZAtXxMTXhhrfMZmM9h42H3F+x1J9cOHBM9BEK19KeffreYxJA8ufN0dQqd1CyrEeh7j8UMsCf1XNJv831zPLf/v2wCDtBUI= 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:12 +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:12 +0000 From: Osamu Watanabe To: ffmpeg-devel@ffmpeg.org Date: Sun, 4 Aug 2024 14:27:05 +0900 Message-Id: <20240804052706.292107-2-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: TYCP286CA0032.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:29d::10) 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: 8706bae0-4e0f-413d-185c-08dcb446173f 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: PBt5+mp5kyTjo/y4OFWbDO7B0gOylz9islPe/H+nF0pVe2lO85Q3mT149JafUKm37rJK1rwJ9ivrpXKyK1K8DOi0bJJGGPlUo1hDjcNW9ubOYxJqwTgenNZWyILMsIkcWiaefQP416vhsgHQwR1BBe77sFYcf4Xeg90Y3b8zBac3cPxfsJaJrbFb4/XDrksIPRAykZpV/x3Nx5SEPimknkBB5Wpg8u+8G7sSTvz3g6bXXRYm0FnxXPu1mB29UCw2homQwTMYDgka9BW1cl3oqtMgT5/GwE3CUHVNX1BcEPnVIs/yZK1amgxQqpKvgmhz5dMgCnlqkYxOSn7pAYSJUYf/CFdlbYYczrwSyAA+QlcUhRVL4bP98xjx1piGXQXh2vykIQiQRB7fwX+Ca8inReN/N6QA47JhuIjDxBxemYyUYpP9AIfG7OvH0iwV53I1DV4b3qvE33hsTe8zF/5RP0k2tSJ+RHdKXDc5oEaYkWz7xqKJq8sfsIW66QJVpaI/XiAY7Vnwd04skXZpn5WT2BfJnBaOWBjgvkx9em6UCM4Uq3ZqnBSHTlnhe2sWUIJnqKKH46NHu9NWjRV+ghfxBMpLweJFbOIK4XL06Cp5xVReBOZEtw7ngaSMxjOwNw7HtXMERQVQxgPJ4tWjSwo5CCeoBUvfZuLST2eM5XHk1vPntIb4oUP68jMSA5HTVSkqNLiwWUfcCkRIov7NqWap9jhngHNORPMzHhlh/SfKT2d2T5eXl3Xc+qyGYVqGZScU/ZGHB7SSvTXF3yc7gGnWdf4FepOr0kh+jsuYe02Fq7zPANQAnBfDV9Tobe7sqmiT6GwBZQsz55qJAQuxhRsDDvNaRIy1SbhsIprb2NY72VbOYN4BQu+BfdeM04lryj7ERbKjku2HO0MIighEILZ7QZvZMueHe3YLYqXqGSd0uL3bjd0xDHB03iMaMMFoffo9tVMJh26t3k9+7agTYE6HDD2o7csJfIV3Y1UgdxB0VqCP9UJcLpYNl47dNVJyirpb4Dsn/oehto2t/0ugQzKDd59Bx+DBolTm7u6XziRjY0l3HoZNhWLU6/o8heX6q/7QB65vdBI2sASBRnhFpwOVSPpP341c+NtBxe3xd9tPaNkIhvn2x+vz9iPGJyXC6dI/OsdTz3ioNd0r9uFCy9X8QYNf7ZiD3UzqdYXN/rBlHkLJ2X+8xlaklQZ7JHi0F1fJuOvnu2ziVyaHWwUCIOns6v+UdNmq7vt0iBYE8MiOT1GhVgLFpg0jDegVIgTwoNcY7ixPTAk2OfYz4FSOpsoMf6XiAxHKSKtQL/mChzbOKxVSvBaQ1ZgcRe6njMAHABYzDetC1PwOJpfaOwDnahx18Mar1bprjec1hamsX0fRkA8z0vzsUkBNO1h1Sp9VvV7vgR6+cPwaZILIEUWxK0DrIg== 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: 2t/mKfe5MVa0/tkjX+nTCeP9Ij0P+eLg5xzZSCglHzd8HsCJ4Jk2eNE0hpc3KU8gMOVsC6lEdKWOrN0j55u7E1p+hnZUJqTtqdwWDSJkQ0KwtURzYKmzDrT0bCNXlYwrWad+1MIGfb88CgF+PE6P/aB7LMWoP94b1TinQZAS9ilUU7R0vt7zEAOwWMxPvuGwJvr/Aeiei54cIul0l8hc/iFE4FWA8iX2FhldrKP4JmhJpHd7+2Pu8ZTiNDQd4TNrQurYIRgRcCcYigMCqI5wNXJWaa31es3ROT9AbycGLE/qnn+yyjOES+e6oKBjhodmdMrTIE4CVvV01Cm2UXls6R7/jpCAOWkSQYEX9s8X2WvSu1/pT635uxBVkYk4l2jctptSKYzJtWKZ7yb43jN9FGBIx5AnMf6QfhzTEeUhFac/8On4Av04lLsmAWPqJpKjPGQM1494b07QjJbLiq0nXLrL87uw9/nCqtL4GBMSfcSgMH9/itEpgxJf5k7YZhLmYKtCk5gcbqqUVIrqRccom02qQCc0GpSDn/4D/phrKIul2ek4uGnioYdfcN+WVmqb34937LhQOD9E0zKlTPVp8V9Jvy39s+L3UcNRA7vWkBU0o7cNRd61iITt11I5ruL9KLdkLC/pWjD84K8Vli+TVXH08cqjzXxw69KRWUzZwzlNpLfUmObxACCO9Pi8wn844vbN1rVBGX+MiuWSqTaA9ALTI+ndFl0BDBoCGgy+WXCEheY+OgbaufwWqLbCxqLVvGgA9zEfUMujpE1ctYdvkLooskeU7SjZtMJkE1+we3/mDjuY4+YLmq4nn9T5HhB2RmHFedkSAQdKjpaz7trpTuv4aMj0nQwLlSd5G5AI1ZXCth2/ttzr3jLuknclxlSKZtUwESelw5VQPBx10fGebkQnVvDRcVIr89cyk/ycG5qaifoVfdzE/N1ZrFAeoX76FI//USCoCO9BNnBA0TxRqP0EWtnfY01sSMWkH2b/Yy3l3eboPikqNtWf+VyrceRZryAfLUQVMMi6MUeQSaoiUNgD2AXPEhL4o5pLQBOMA1uwwxWajK+snjTD2PQQ+fYFd+Vc1AuEvzg7DiSjmSbgwEb7KdxPsJMixrCSM7WIJakW/dEYLrP3PHbfgL2ERNLsWbtuStjOYSFv9Jwb9uCeRtzKLTRmh7QW0C+d9LJpZgw59fXneJtXBgtcrjNcSvkOpJXTphgznHrHfR6qjxuJdXwArtvxrpPHYsHrulDXbcgvCaXJIxXl3USy4CeLWfrM/cANsFDLSeEEw0GJ41In/PyElyrWooIPp8wWIIdN2nIa5aX5oL8H/tinyu+5VoRxUbG+OgmdhYWXGLN5qbcOOtVrLb7gswhirzZolwaaFRpgYiAPkWLHTM7YthV8NB941/I2hV8klfbZuXYPRUBJ6eBBV3NVYYIxMwkKuzwTp5oQOb71C7gx41Kd7c4SKBnLdbqywZPmOAGwt3E1OZtnBAuIlmymh0UfcbCbb5M+0cNhWHz/XOrts3iwSDhQQINCNKk8i8d5rB+qpR+N5Ego4X1by12vU49fskmnHYbhXazWA3LY3XuXb7UKWF9COg+uw/4Q3reE3SpWbQNrQiLl/w== X-OriginatorOrg: es.takushoku-u.ac.jp X-MS-Exchange-CrossTenant-Network-Message-Id: 8706bae0-4e0f-413d-185c-08dcb446173f 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:12.1381 (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: g6EDZjJVd4oF5d06grqn80waVuYxWUsz56Vzf+cpHdqbpHMkN4WhZas3A/kDytb+A9AhWJp3KcYXY4VaoioZkANiZedPQj9u0OuxO8Xr0jE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: TY3PR01MB11348 Subject: [FFmpeg-devel] [PATCH v7 2/3] avcodec/jpeg2000dec: Add support for placeholder passes 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: fN+wZa6iB9ql This commit adds support for placeholder pass parsing. Signed-off-by: Osamu Watanabe --- libavcodec/jpeg2000.h | 2 + libavcodec/jpeg2000dec.c | 352 +++++++++++++++++++++++++++++-------- libavcodec/jpeg2000htdec.c | 90 +++++----- 3 files changed, 326 insertions(+), 118 deletions(-) diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index 4bdc38df7c..93221d90ca 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -200,6 +200,8 @@ typedef struct Jpeg2000Cblk { /* specific to HT code-blocks */ int zbp; int pass_lengths[2]; + uint8_t modes; // copy of SPcod/SPcoc field to parse HT-MIXED mode + uint8_t ht_plhd; // are we looking for HT placeholder passes? } Jpeg2000Cblk; // code block typedef struct Jpeg2000Prec { diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 44db131d77..c8759b6d51 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -54,6 +54,15 @@ #define HAD_COC 0x01 #define HAD_QCC 0x02 +// Values of flag for placeholder passes +enum HT_PLHD_STATUS { + HT_PLHD_OFF, + HT_PLHD_ON +}; + +#define HT_MIXED 0x80 // bit 7 of SPcod/SPcoc + + /* get_bits functions for JPEG2000 packet bitstream * It is a get_bit function with a bit-stuffing routine. If the value of the * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB. @@ -1160,100 +1169,293 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int incl, newpasses, llen; void *tmp; - if (cblk->npasses) - incl = get_bits(s, 1); - else + if (!cblk->incl) { + incl = 0; + cblk->modes = codsty->cblk_style; + if (cblk->modes >= JPEG2000_CTSY_HTJ2K_F) + cblk->ht_plhd = HT_PLHD_ON; + if (layno > 0) + incl = tag_tree_decode(s, prec->cblkincl + cblkno, 0 + 1) == 0; incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno; - if (!incl) - continue; - else if (incl < 0) - return incl; - - if (!cblk->npasses) { - int zbp = tag_tree_decode(s, prec->zerobits + cblkno, 100); - int v = expn[bandno] + numgbits - 1 - zbp; - if (v < 0 || v > 30) { - av_log(s->avctx, AV_LOG_ERROR, - "nonzerobits %d invalid or unsupported\n", v); - return AVERROR_INVALIDDATA; + if (incl) { + int zbp = tag_tree_decode(s, prec->zerobits + cblkno, 100); + int v = expn[bandno] + numgbits - 1 - (zbp - tile->comp->roi_shift); + if (v < 0 || v > 30) { + av_log(s->avctx, AV_LOG_ERROR, + "nonzerobits %d invalid or unsupported\n", v); + return AVERROR_INVALIDDATA; + } + cblk->incl = 1; + cblk->nonzerobits = v; + cblk->zbp = zbp; + cblk->lblock = 3; } - cblk->zbp = zbp; - cblk->nonzerobits = v; - } - if ((newpasses = getnpasses(s)) < 0) - return newpasses; - av_assert2(newpasses > 0); - if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) { - avpriv_request_sample(s->avctx, "Too many passes"); - return AVERROR_PATCHWELCOME; - } - if ((llen = getlblockinc(s)) < 0) - return llen; - if (cblk->lblock + llen + av_log2(newpasses) > 16) { - avpriv_request_sample(s->avctx, - "Block with length beyond 16 bits"); - return AVERROR_PATCHWELCOME; + } else { + incl = get_bits(s, 1); } - cblk->lblock += llen; - - cblk->nb_lengthinc = 0; - cblk->nb_terminationsinc = 0; - av_free(cblk->lengthinc); - cblk->lengthinc = av_calloc(newpasses, sizeof(*cblk->lengthinc)); - if (!cblk->lengthinc) - return AVERROR(ENOMEM); - tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start)); - if (!tmp) - return AVERROR(ENOMEM); - cblk->data_start = tmp; - do { - int newpasses1 = 0; + if (incl) { + uint8_t bypass_term_threshold = 0; + uint8_t bits_to_read = 0; + uint32_t segment_bytes = 0; + int32_t segment_passes = 0; + uint8_t next_segment_passes = 0; + int32_t href_passes, pass_bound; + uint32_t tmp_length = 0; + int32_t newpasses_copy, npasses_copy; + + if ((newpasses = getnpasses(s)) <= 0) + return newpasses; + if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) { + avpriv_request_sample(s->avctx, "Too many passes"); + return AVERROR_PATCHWELCOME; + } + if ((llen = getlblockinc(s)) < 0) + return llen; + if (cblk->lblock + llen + av_log2(newpasses) > 16) { + avpriv_request_sample(s->avctx, + "Block with length beyond 16 bits"); + return AVERROR_PATCHWELCOME; + } + cblk->nb_lengthinc = 0; + cblk->nb_terminationsinc = 0; + av_free(cblk->lengthinc); + cblk->lengthinc = av_calloc(newpasses, sizeof(*cblk->lengthinc)); + if (!cblk->lengthinc) + return AVERROR(ENOMEM); + tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, + sizeof(*cblk->data_start)); + if (!tmp) + return AVERROR(ENOMEM); + cblk->data_start = tmp; + cblk->lblock += llen; + + // Count number of necessary terminations for non HT code block + newpasses_copy = newpasses; + npasses_copy = cblk->npasses; + if (!(cblk->modes & JPEG2000_CTSY_HTJ2K_F)) { + do { + int newpasses1 = 0; + + while (newpasses1 < newpasses_copy) { + newpasses1++; + if (needs_termination(codsty->cblk_style, npasses_copy + newpasses1 - 1)) { + cblk->nb_terminationsinc++; + break; + } + } + npasses_copy += newpasses1; + newpasses_copy -= newpasses1; + } while (newpasses_copy); + } - while (newpasses1 < newpasses) { - newpasses1 ++; - if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) { - cblk->nb_terminationsinc ++; - break; + if (cblk->ht_plhd) { + href_passes = (cblk->npasses + newpasses - 1) % 3; + segment_passes = newpasses - href_passes; + pass_bound = 2; + bits_to_read = cblk->lblock; + if (segment_passes < 1) { + // No possible HT Cleanup pass here; may have placeholder passes + // or an original J2K block bit-stream (in MIXED mode). + segment_passes = newpasses; + while (pass_bound <= segment_passes) { + bits_to_read++; + pass_bound += pass_bound; + } + segment_bytes = get_bits(s, bits_to_read); + if (segment_bytes) { + if (cblk->modes & HT_MIXED) { + cblk->ht_plhd = HT_PLHD_OFF; + cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F)); + } + else { + av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n"); + } + } + } else { + while (pass_bound <= segment_passes) { + bits_to_read++; + pass_bound += pass_bound; + } + segment_bytes = get_bits(s, bits_to_read); + if (segment_bytes) { + // No more placeholder passes + if (!(cblk->modes & HT_MIXED)) { + // Must be the first HT Cleanup pass + if (segment_bytes < 2) + av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n"); + next_segment_passes = 2; + cblk->ht_plhd = HT_PLHD_OFF; + // Write length information for HT CleanUp segment + cblk->pass_lengths[0] = segment_bytes; + } else if (cblk->lblock > 3 && segment_bytes > 1 + && (segment_bytes >> (bits_to_read - 1)) == 0) { + // Must be the first HT Cleanup pass, since length MSB is 0 + next_segment_passes = 2; + cblk->ht_plhd = HT_PLHD_OFF; + // Write length information for HT CleanUp segment + cblk->pass_lengths[0] = segment_bytes; + } else { + // Must have an original (non-HT) block coding pass + cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F)); + cblk->ht_plhd = HT_PLHD_OFF; + segment_passes = newpasses; + while (pass_bound <= segment_passes) { + bits_to_read++; + pass_bound += pass_bound; + segment_bytes <<= 1; + segment_bytes += get_bits(s, 1); + } + } + } else { + // Probably parsing placeholder passes, but we need to read an + // extra length bit to verify this, since prior to the first + // HT Cleanup pass, the number of length bits read for a + // contributing code-block is dependent on the number of passes + // being included, as if it were a non-HT code-block. + segment_passes = newpasses; + if (pass_bound <= segment_passes) { + while (1) { + bits_to_read++; + pass_bound += pass_bound; + segment_bytes <<= 1; + segment_bytes += get_bits(s, 1); + if (pass_bound > segment_passes) + break; + } + if (segment_bytes) { + if (cblk->modes & HT_MIXED) { + cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F)); + cblk->ht_plhd = HT_PLHD_OFF; + } else { + av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n"); + } + } + } + } + } + } else if (cblk->modes & JPEG2000_CTSY_HTJ2K_F) { + // Quality layer commences with a non-initial HT coding pass + if(bits_to_read != 0) + av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n"); + segment_passes = cblk->npasses % 3; + if (segment_passes == 0) { + // newpasses is a HT Cleanup pass; next segment has refinement passes + segment_passes = 1; + next_segment_passes = 2; + if (segment_bytes == 1) + av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n"); + } else { + // newpasses = 1 means npasses is HT SigProp; 2 means newpasses is + // HT MagRef pass + segment_passes = newpasses > 1 ? 3 - segment_passes : 1; + next_segment_passes = 1; + bits_to_read = av_log2(segment_passes); + } + bits_to_read = (uint8_t) (bits_to_read + cblk->lblock); + segment_bytes = get_bits(s, bits_to_read); + // Write length information for HT Refinment segment + cblk->pass_lengths[1] += segment_bytes; + } else if (!(cblk->modes & (JPEG2000_CBLK_TERMALL | JPEG2000_CBLK_BYPASS))) { + // Common case for non-HT code-blocks; we have only one segment + bits_to_read = (uint8_t) cblk->lblock + av_log2((uint8_t) newpasses); + segment_bytes = get_bits(s, bits_to_read); + segment_passes = newpasses; + } else if (cblk->modes & JPEG2000_CBLK_TERMALL) { + // RESTART MODE + bits_to_read = cblk->lblock; + segment_bytes = get_bits(s, bits_to_read); + segment_passes = 1; + next_segment_passes = 1; + } else { + // BYPASS MODE + bypass_term_threshold = 10; + if(bits_to_read != 0) + av_log(s->avctx, AV_LOG_WARNING, "Length information for a codeblock is invalid\n"); + if (cblk->npasses < bypass_term_threshold) { + // May have from 1 to 10 uninterrupted passes before 1st RAW SigProp + segment_passes = bypass_term_threshold - cblk->npasses; + if (segment_passes > newpasses) + segment_passes = newpasses; + while ((2 << bits_to_read) <= segment_passes) + bits_to_read++; + next_segment_passes = 2; + } else if ((cblk->npasses - bypass_term_threshold) % 3 < 2) { + // 0 means newpasses is a RAW SigProp; 1 means newpasses is a RAW MagRef pass + segment_passes = newpasses > 1 ? 2 - (cblk->npasses - bypass_term_threshold) % 3 : 1; + bits_to_read = av_log2(segment_passes); + next_segment_passes = 1; + } else { + // newpasses is an isolated Cleanup pass that precedes a RAW SigProp pass + segment_passes = 1; + next_segment_passes = 2; } + bits_to_read = (uint8_t) (bits_to_read + cblk->lblock); + segment_bytes = get_bits(s, bits_to_read); } - - if (newpasses > 1 && (codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F)) { - // Retrieve pass lengths for each pass - int href_passes = (cblk->npasses + newpasses - 1) % 3; - int eb = av_log2(newpasses - href_passes); - int extra_bit = newpasses > 2 ? 1 : 0; - if ((ret = get_bits(s, llen + eb + 3)) < 0) - return ret; - cblk->pass_lengths[0] = ret; - if ((ret = get_bits(s, llen + 3 + extra_bit)) < 0) - return ret; - cblk->pass_lengths[1] = ret; - ret = cblk->pass_lengths[0] + cblk->pass_lengths[1]; + // Update cblk->npasses and write length information + cblk->npasses = (uint8_t) (cblk->npasses + segment_passes); + cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes; + + if ((cblk->modes & JPEG2000_CTSY_HTJ2K_F) && cblk->ht_plhd == HT_PLHD_OFF) { + newpasses -= (uint8_t) segment_passes; + while (newpasses > 0) { + segment_passes = newpasses > 1 ? next_segment_passes : 1; + next_segment_passes = (uint8_t) (3 - next_segment_passes); + bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes)); + segment_bytes = get_bits(s, bits_to_read); + newpasses -= (uint8_t) (segment_passes); + // This is a FAST Refinement pass + // Write length information for HT Refinement segment + cblk->pass_lengths[1] += segment_bytes; + // Update cblk->npasses and write length information + cblk->npasses = (uint8_t) (cblk->npasses + segment_passes); + cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes; + } } else { - if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0) - return ret; - cblk->pass_lengths[0] = ret; + newpasses -= (uint8_t) (segment_passes); + while (newpasses > 0) { + if (bypass_term_threshold != 0) { + segment_passes = newpasses > 1 ? next_segment_passes : 1; + next_segment_passes = (uint8_t) (3 - next_segment_passes); + bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes)); + } else { + if ((cblk->modes & JPEG2000_CBLK_TERMALL) == 0) + av_log(s->avctx, AV_LOG_WARNING, "Corrupted packet header is found.\n"); + segment_passes = 1; + bits_to_read = cblk->lblock; + } + segment_bytes = get_bits(s, bits_to_read); + newpasses -= (uint8_t) (segment_passes); + + // Update cblk->npasses and write length information + cblk->npasses = (uint8_t) (cblk->npasses + segment_passes); + cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes; + } } - if (ret > cblk->data_allocated) { - size_t new_size = FFMAX(2*cblk->data_allocated, ret); + + for (int i = 0; i < cblk->nb_lengthinc; ++i) + tmp_length = (tmp_length < cblk->lengthinc[i]) ? cblk->lengthinc[i] : tmp_length; + + if (tmp_length > cblk->data_allocated) { + size_t new_size = FFMAX(2 * cblk->data_allocated, tmp_length); void *new = av_realloc(cblk->data, new_size); if (new) { cblk->data = new; cblk->data_allocated = new_size; } } - if (ret > cblk->data_allocated) { + if (tmp_length > cblk->data_allocated) { avpriv_request_sample(s->avctx, "Block with lengthinc greater than %"SIZE_SPECIFIER"", cblk->data_allocated); return AVERROR_PATCHWELCOME; } - cblk->lengthinc[cblk->nb_lengthinc++] = ret; - cblk->npasses += newpasses1; - newpasses -= newpasses1; - } while(newpasses); + } else { + // This codeblock has no contribution to the current packet + continue; + } } } jpeg2000_flush(s); @@ -1788,7 +1990,7 @@ static int decode_cblk(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *cod Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width, int height, int bandpos, uint8_t roi_shift) { - int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift; + int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1; int pass_cnt = 0; int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC; int term_cnt = 0; @@ -2028,7 +2230,7 @@ static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile Jpeg2000Cblk *cblk = prec->cblk + cblkno; - if (codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F) + if (cblk->modes & JPEG2000_CTSY_HTJ2K_F) 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], diff --git a/libavcodec/jpeg2000htdec.c b/libavcodec/jpeg2000htdec.c index eba0936089..9b473e11d3 100644 --- a/libavcodec/jpeg2000htdec.c +++ b/libavcodec/jpeg2000htdec.c @@ -1032,25 +1032,31 @@ static void jpeg2000_calc_mbr(uint8_t *mbr, const uint16_t i, const uint16_t j, static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s, int width, int height, int stride, int pLSB, int32_t *sample_buf, uint8_t *block_states, - uint8_t *magref_segment, uint32_t magref_length) + uint8_t *magref_segment, uint32_t magref_length, + uint8_t is_causal) { for (int j = j_s; j < j_s + width; j++) { uint32_t mbr_info = 0; for (int i = i_s; i < i_s + height; i++) { - int modify_state, cond; + int modify_state; uint8_t bit; - uint8_t causal_cond = i != (i_s + height - 1); + uint8_t causal_cond = (is_causal == 0) || (i != (i_s + height - 1)); int32_t *sp = &sample_buf[j + (i * (stride))]; uint8_t mbr = 0; - if (jpeg2000_get_state(i, j, stride - 2, HT_SHIFT_SIGMA, block_states) == 0) + if (jpeg2000_get_state(i, j, stride, HT_SHIFT_SIGMA, block_states) == 0) jpeg2000_calc_mbr(&mbr, i, j, mbr_info & 0x1EF, causal_cond, block_states, stride); mbr_info >>= 3; - cond = mbr != 0; - bit = jpeg2000_peek_bit(sig_prop, magref_segment, magref_length); - *sp |= (bit * cond) << pLSB; - sig_prop->bits -= cond; - modify_state = (((1 << HT_SHIFT_REF_IND) | (1 << HT_SHIFT_REF)) * cond) | 1 << HT_SHIFT_SCAN; + + 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; + bit = jpeg2000_peek_bit(sig_prop, magref_segment, magref_length); + 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); } } @@ -1074,6 +1080,7 @@ static void jpeg2000_decode_sigprop_segment(Jpeg2000Cblk *cblk, uint16_t width, int last_width; uint16_t i = 0, j = 0; + uint8_t is_causal = cblk->modes & JPEG2000_CBLK_VSC; jpeg2000_init_zero(&sp_dec); @@ -1082,14 +1089,14 @@ static void jpeg2000_decode_sigprop_segment(Jpeg2000Cblk *cblk, uint16_t width, for (int n2 = 0; n2 < num_h_stripe; n2++) { jpeg2000_process_stripes_block(&sp_dec, i, j, b_width, b_height, stride, pLSB, sample_buf, block_states, magref_segment, - magref_length); + magref_length, is_causal); j += 4; } last_width = width % 4; if (last_width) jpeg2000_process_stripes_block(&sp_dec, i, j, last_width, b_height, stride, pLSB, sample_buf, block_states, magref_segment, - magref_length); + magref_length, is_causal); i += 4; } @@ -1099,20 +1106,20 @@ static void jpeg2000_decode_sigprop_segment(Jpeg2000Cblk *cblk, uint16_t width, for (int n2 = 0; n2 < num_h_stripe; n2++) { jpeg2000_process_stripes_block(&sp_dec, i, j, b_width, b_height, stride, pLSB, sample_buf, block_states, magref_segment, - magref_length); + magref_length, is_causal); j += 4; } last_width = width % 4; if (last_width) jpeg2000_process_stripes_block(&sp_dec, i, j, last_width, b_height, stride, pLSB, sample_buf, block_states, magref_segment, - magref_length); + magref_length, is_causal); } /** * See procedure decodeSigPropMag at Rec. ITU-T T.814, 7.5. */ -static int +static void jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int stride, uint8_t *magref_segment,uint32_t magref_length, uint8_t pLSB, int32_t *sample_buf, uint8_t *block_states) @@ -1152,15 +1159,14 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, const int } } } - return 1; } 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 width, int height, int M_b, uint8_t roi_shift) { - uint8_t p0 = 0; // Number of placeholder passes + uint8_t p0 = 0; // 3 * p0 = Number of placeholder passes uint32_t Lcup; // Length of HT cleanup segment uint32_t Lref; // Length of Refinement segment uint32_t Scup; // HT cleanup segment suffix length @@ -1174,7 +1180,7 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c int z_blk; // Number of ht coding pass - uint8_t empty_passes; + uint8_t num_plhd_passes; // Number of placeholder passes StateVars mag_sgn; // Magnitude and Sign StateVars mel; // Adaptive run-length coding @@ -1190,8 +1196,9 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c uint8_t *block_states = NULL; int32_t n, val; // Post-processing - - int32_t M_b = magp; + 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; const int quad_buf_height = height + 4; @@ -1201,22 +1208,17 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c av_assert0(width * height <= 4096); av_assert0(width * height > 0); - if (roi_shift) - avpriv_report_missing_feature(s->avctx, "ROI shift"); - memset(t1->data, 0, t1->stride * height * sizeof(*t1->data)); memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags)); if (cblk->npasses == 0) return 0; - if (cblk->npasses > 3) - p0 = 0; - else if (cblk->length == 0) - p0 = 1; - - empty_passes = p0 * 3; - z_blk = cblk->npasses - empty_passes; + num_rempass = cblk->npasses % 3; // Number of remainder passes + num_plhd_passes = num_rempass ? cblk->npasses - num_rempass : cblk->npasses - 3; + av_assert0(num_plhd_passes % 3 == 0); + p0 = num_plhd_passes / 3; + z_blk = cblk->npasses - num_plhd_passes; if (z_blk <= 0) return 0; // No passes within this set, continue @@ -1232,6 +1234,7 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c Dcup = cblk->data; Dref = cblk->data + Lcup; // Dref comes after the refinement segment S_blk = p0 + cblk->zbp; + cblk->zbp = S_blk - 1; pLSB = 30 - S_blk; Scup = (Dcup[Lcup - 1] << 4) + (Dcup[Lcup - 2] & 0x0F); @@ -1277,31 +1280,32 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c goto free; } - if (cblk->npasses > 1) + if (z_blk > 1) jpeg2000_decode_sigprop_segment(cblk, width, height, quad_buf_width, Dref, Lref, pLSB - 1, sample_buf, block_states); - if (cblk->npasses > 2) { - - if (Lref < 2){ - av_log(s->avctx,AV_LOG_ERROR,"Invalid magnitude refinement length\n"); - ret = AVERROR_INVALIDDATA; - goto free; - } - if ((ret = jpeg2000_decode_magref_segment(width, height, quad_buf_width, Dref, Lref, - pLSB - 1, sample_buf, block_states)) < 0) - goto free; - } + if (z_blk > 2) + jpeg2000_decode_magref_segment(width, height, quad_buf_width, Dref, Lref, + pLSB - 1, sample_buf, block_states); pLSB = 31 - M_b; /* Reconstruct the sample values */ for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { + int32_t sign; + n = x + (y * t1->stride); val = sample_buf[x + (y * quad_buf_width)]; + sign = val & INT32_MIN; + val &= INT32_MAX; + /* ROI shift, if necessary */ + if (roi_shift && (((uint32_t)val & ~mask) == 0)) + val <<= roi_shift; /* Convert sign-magnitude to two's complement. */ - val = val >> 31 ? 0x80000000 - val : val; + if (sign) + val = -val; + /* Shift down to 1 bit upper from decimal point for reconstruction value (= 0.5) */ val >>= (pLSB - 1); t1->data[n] = val; }