From patchwork Wed Apr 22 18:34:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 19177 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2B18044A0A7 for ; Wed, 22 Apr 2020 21:34:57 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EF80668BD33; Wed, 22 Apr 2020 21:34:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 57F8968BC3E for ; Wed, 22 Apr 2020 21:34:50 +0300 (EEST) Received: by mail-pl1-f175.google.com with SMTP id c21so455458plz.4 for ; Wed, 22 Apr 2020 11:34:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=m0giBtBloujR4mOPQsEvaNhp8adjhc3gVjSTbckriDs=; b=Tp+3nGl29MhKb95/5YNHFi+/z+F4/S+T+BO3ZNZINQfAtD9q71L1/4nrnROxtOPaXC oq1AKeKvLGa/lg4r3Jze1RBP5hpkLGSc1b8UB/BJ7eSFXA8fWFsy3ek96h3xK9m+4+B5 peDVMDItPWqBBoGMZMY8S7YzMOv53CzZr2YX7oaskWEivMstE9ShtNfNRVkpRa/iVxEY ydfYu9mVnDFNVMZJ8ivnsocZ7PInc6krEiB/82sX8Kl32pu6S3THETp8hgSoq2SMzHQc OZ5XAGyObEH8nMos9potkd1htJ9a3b34O6JIsj2f/ZqViBgwz7TaobNBmI474cHrHVTB NGGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=m0giBtBloujR4mOPQsEvaNhp8adjhc3gVjSTbckriDs=; b=SXSn5VJBShqc0VqeLRBUWBkSM5nGUxjWfX50zbz4MReBtWjjcSHfrnj20LrGeOUmSF g2PgYlJv4ybCbBgkppti/SNAjsYEjZdKTINwtrrifDqwyx7Zk054CQEN7UJ8fOX1LRnn c87uwMesP/uUDp5eiuErLqRMqhOyH9E7vdldvrDyPBtQVh8lBe2/j14Qto5TjXsRe+bd lUT43bDef3IaWYspFDsK7kMIrrm5UgZmHQODzV/jGNyEVnRAUeftW8wXzd7DDYg2HWhT T+rXVsThXLGB9IyBO4YFgRu0ub0GnZxw0YMzehKqR1RGDofFirjmPYahfcOv48Tow6mh 7CDA== X-Gm-Message-State: AGi0PuZryifR+9XQSKL3DD1dm2c9Zr7n76vU5rmwbp1LyV4t2iPMCNjJ sQtlQbAPLOM7YtWuolHGhIUqMWrTY+Q= X-Google-Smtp-Source: APiQypITDrszYJoYez0WnUG4BNpSAegCAsgELAvmiOXAm+yLENxQQEd/5vI6Bav5qocLWYomDJBJqg== X-Received: by 2002:a17:90a:cb8c:: with SMTP id a12mr46038pju.153.1587580488054; Wed, 22 Apr 2020 11:34:48 -0700 (PDT) Received: from localhost.localdomain ([122.166.129.53]) by smtp.gmail.com with ESMTPSA id x128sm184238pfd.109.2020.04.22.11.34.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Apr 2020 11:34:47 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Apr 2020 00:04:39 +0530 Message-Id: <20200422183440.11141-1-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH v2 1/2] libavcodec/jpeg2000dec.c: Support for CRG marker 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 Cc: Gautam Ramakrishnan MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Gautam Ramakrishnan This patch adds support to skip the CRG marker. The CRG marker, is an informational marker. Allows samples such as p0_03.j2k to be decoded. --- libavcodec/jpeg2000dec.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index af6dcee228..5a7d9e7882 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -798,6 +798,15 @@ static int get_sot(Jpeg2000DecoderContext *s, int n) return 0; } +static int read_crg(Jpeg2000DecoderContext *s, int n) +{ + if (s->ncomponents*4 != n - 2) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n"); + return AVERROR_INVALIDDATA; + } + bytestream2_skip(&s->g, n - 2); + return 0; +} /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1 * Used to know the number of tile parts and lengths. * There may be multiple TLMs in the header. @@ -2061,6 +2070,9 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s) // the comment is ignored bytestream2_skip(&s->g, len - 2); break; + case JPEG2000_CRG: + ret = read_crg(s, len); + break; case JPEG2000_TLM: // Tile-part lengths ret = get_tlm(s, len); From patchwork Wed Apr 22 18:34:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 19178 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 279E644A0A7 for ; Wed, 22 Apr 2020 21:34:59 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EED3868BD49; Wed, 22 Apr 2020 21:34:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AFAF468BD47 for ; Wed, 22 Apr 2020 21:34:52 +0300 (EEST) Received: by mail-pl1-f171.google.com with SMTP id w3so1278013plz.5 for ; Wed, 22 Apr 2020 11:34:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6+h69pz0GH/q/8BvIBa5R2OXQu46dRpctr7OoTCOogs=; b=XoBmq6LQRN5UFF5lx34E9ZfpvH2n+KHRjaf0v94bZHdH41D0cZj02bjGr3WbgqmwxA ytZZaT+MK7DMSmpr5TchtG+RaD7WGnMUrN0UgCcOBOHUU8EmCmxxp9dDNkY2gl7VmeWk 5PIQh1a4AN14UtWHtb+3ZtaP3EOqZ3u41fb6B20aY48GpxF5poSF8EiQeDCei/rJp6s1 UyWdkP2TRLSJaTdhoyeIHo+pxzkZol8Y+EXX/U+aoNwjmiMuwpx7pAvvXSFrZOfZVdks Lc3IqRp1dtAp4GeIlsC57j33+m8nKKlxQl/VXKguFynmfrdAFngedliGmTngUy8bfSds ComA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6+h69pz0GH/q/8BvIBa5R2OXQu46dRpctr7OoTCOogs=; b=dDkcl7Dmym0q9XJO3YWHDezLzBhUTmtCkd2gvmckMi85uKtK130lq+kSb8U5/NDMsF grDPElZ0bSVCNYLGFuSjol7MPzTlWjW+Yuf13ikBiVFkOUyGlj5fZ/RrNmTvuUIeOtlt Kw6P80KJ30KBNcl0VsM1VxRM362rtvyUDgJz/IZfgC10RrWMlLcu+FybiwMnxO94+S2V BRYKX3n5EoKnuNa8YVy6oRZa44B1W769+0vqAm5psGp9CI9FCFbiytcgVNIvLJwwuZmU tPHKZcM4MP2dCc1TsJNUYrS/Ub7yHUFuki2apjHiN2nAl9rQRGYGy4h4tWCu/K0+aNIo gDjA== X-Gm-Message-State: AGi0PuYS0wbKZ5FhdD0tkuXMs1zdJczdB1LuESt1xfa9hTbZiClp7RYh IdQXgeFbzpjMzPlrQy5cIZi6Y4W1mw4= X-Google-Smtp-Source: APiQypKnumioA0uSrqBYxWkOPBSvqnYjGQZyp8SdzXsLV/M1f/U3vrHiB5qRp7awT8V4aIBiYnqELA== X-Received: by 2002:a17:90a:ad02:: with SMTP id r2mr61844pjq.63.1587580490677; Wed, 22 Apr 2020 11:34:50 -0700 (PDT) Received: from localhost.localdomain ([122.166.129.53]) by smtp.gmail.com with ESMTPSA id x128sm184238pfd.109.2020.04.22.11.34.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Apr 2020 11:34:50 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Apr 2020 00:04:40 +0530 Message-Id: <20200422183440.11141-2-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200422183440.11141-1-gautamramk@gmail.com> References: <20200422183440.11141-1-gautamramk@gmail.com> Subject: [FFmpeg-devel] [PATCH v2 2/2] libavcodec/jpeg2000dec.c: ROI marker support 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 Cc: Gautam Ramakrishnan MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Gautam Ramakrishnan This patch adds support for decoding images with a Region of Interest. Allows decoding samples such as p0_03.j2k. This patch should fix ticket #4681. --- libavcodec/jpeg2000.h | 1 + libavcodec/jpeg2000dec.c | 67 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index 7b78c0193e..0f82716981 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -210,6 +210,7 @@ typedef struct Jpeg2000Component { int *i_data; int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers + uint8_t roi_shift; // ROI scaling value for the component } Jpeg2000Component; /* misc tools */ diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 5a7d9e7882..bcc6cc628b 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -117,6 +117,7 @@ typedef struct Jpeg2000DecoderContext { Jpeg2000CodingStyle codsty[4]; Jpeg2000QuantStyle qntsty[4]; Jpeg2000POC poc; + uint8_t roi_shift[4]; int bit_index; @@ -598,6 +599,31 @@ static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, return 0; } +static int get_rgn(Jpeg2000DecoderContext *s, int n) +{ + uint16_t compno; + compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g): + bytestream2_get_be16u(&s->g); + if (bytestream2_get_byte(&s->g)) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n"); + return AVERROR_INVALIDDATA; // SRgn field value is 0 + } + // SPrgn field + // Currently compno cannot be greater than 4. + // However, future implementation should support compno up to 65536 + if (compno < s->ncomponents) { + if (s->curtileno == -1) + s->roi_shift[compno] = bytestream2_get_byte(&s->g); + else { + if (s->tile[s->curtileno].tp_idx != 0) + return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile + s->tile[s->curtileno].comp[compno].roi_shift = bytestream2_get_byte(&s->g); + } + return 0; + } + return AVERROR_INVALIDDATA; +} + /* Get common part for QCD and QCC segments. */ static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q) { @@ -947,6 +973,9 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno) comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor); comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor); + if (!comp->roi_shift) + comp->roi_shift = s->roi_shift[compno]; + if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno], s->avctx)) @@ -1615,9 +1644,9 @@ static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, - int width, int height, int bandpos) + int width, int height, int bandpos, uint8_t roi_shift) { - int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1; + int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift; int pass_cnt = 0; int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC; int term_cnt = 0; @@ -1691,6 +1720,19 @@ static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, return 1; } +static inline int roi_shift_param(Jpeg2000Component *comp, + int quan_parameter) +{ + uint8_t roi_shift; + int val; + roi_shift = comp->roi_shift; + val = (quan_parameter < 0)?-quan_parameter:quan_parameter; + + if (val > (1 << roi_shift)) + return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift); + return quan_parameter; +} + /* TODO: Verify dequantization for lossless case * comp->data can be float or int * band->stepsize can be float or int @@ -1775,6 +1817,20 @@ static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile) s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize); } +static inline void roi_scale_cblk(int x, int y, Jpeg2000Cblk *cblk, + Jpeg2000Component *comp, + Jpeg2000T1Context *t1) +{ + int i, j; + int w = cblk->coord[0][1] - cblk->coord[0][0]; + for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) { + 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) + src[i] = roi_shift_param(comp, src[i]); + } +} + static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile) { Jpeg2000T1Context t1; @@ -1818,7 +1874,7 @@ static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile int ret = decode_cblk(s, codsty, &t1, cblk, cblk->coord[0][1] - cblk->coord[0][0], cblk->coord[1][1] - cblk->coord[1][0], - bandpos); + bandpos, comp->roi_shift); if (ret) coded = 1; else @@ -1826,6 +1882,8 @@ static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile x = cblk->coord[0][0] - band->coord[0][0]; y = cblk->coord[1][0] - band->coord[1][0]; + if (comp->roi_shift) + roi_scale_cblk(x, y, cblk, comp, &t1); if (codsty->transform == FF_DWT97) dequantization_float(x, y, cblk, comp, &t1, band); else if (codsty->transform == FF_DWT97_INT) @@ -2046,6 +2104,9 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s) case JPEG2000_COD: ret = get_cod(s, codsty, properties); break; + case JPEG2000_RGN: + ret = get_rgn(s, len); + break; case JPEG2000_QCC: ret = get_qcc(s, len, qntsty, properties); break;