From patchwork Sat Mar 28 04:19:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 18452 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 8FB3B44ADB7 for ; Sat, 28 Mar 2020 06:25:14 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 771FF68B876; Sat, 28 Mar 2020 06:25:14 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 25DEA687FE6 for ; Sat, 28 Mar 2020 06:25:07 +0200 (EET) Received: by mail-pl1-f195.google.com with SMTP id w3so4277785plz.5 for ; Fri, 27 Mar 2020 21:25:07 -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=C9b51g/XIgse6K/MOAy/KGQ3pqL9UbRns6jDAt1FV4w=; b=cSzVN45PKctlAkCKrB+WSfcEkKyecZEbqenqUEPlaOKeghMFE4a2Mrlzle+TBH8Pql fR/zqM0mMN/rIBhvAV9BRArCP/fTkMkb7BrfSF4MV8/eA/r+zPXcr0AdSqT7sMvAvxeM ipzdyFnocwsLIZ2KwXB3pvZKUGmEktZe8ygJtWmB3BiHiQFuvb6MI6G8v0RSyvC4eS/E OeWne46r/CUpOug2d9H19WE5o5y5WjX3Wfw6Gujqg5TMMruGj+6t5ZlALkriwwsH0NPZ oSkr/msBIgTWL5NhsMkXHMmESdUyMys1s8pFjNIi9BH3iizGpQvbWHfrjRrWl7knhnD6 5ssg== 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=C9b51g/XIgse6K/MOAy/KGQ3pqL9UbRns6jDAt1FV4w=; b=gY0WrPrnGGsMifjFCzkWeNj6TBxHhmIshraXm/gra8si9XQ9VPFqT4Pl3r+spRmhSq TUxhKVUW3wb4J406sWiCf3zrJ5UonIHqtTb4TO7v6YkIHIcSCooquy+uvTxJzCorAbsa /yVQ2oqd5iGyo/9q/yh91SLq/ubQ6yA0XtIvsRBUf+/ZhwKMlhXBCfx983ft6XVW1Zmh iy+L4cmXfrwzrVZ+7NjqqkAACLiLXBA9WQ/Rxt3/WDPopqtv+udUJDSHepPx+VpXCMf3 J5O60383BznxaHLmwcfP8kO8XxQLadjgMliE9fgOSQbT/KLLb0zQtA5IWK3pF0CNjGvs 2IHQ== X-Gm-Message-State: ANhLgQ16S5l4tqrEPaiuhU6PHDnhLuH0WqaqwZs4McNEG5nuXGvepULU tk+aqyNA+LKInOV+xf4BwzL6ik1gZFY= X-Google-Smtp-Source: ADFU+vumLXFqrG7G3WjIKhDVTAcpmiv6rjf4f7djFKofcyPuqoC7soN2uSzzwTvUD73YEs2i2t9NHg== X-Received: by 2002:a17:902:b004:: with SMTP id o4mr2145338plr.54.1585369173049; Fri, 27 Mar 2020 21:19:33 -0700 (PDT) Received: from localhost.localdomain ([223.235.255.212]) by smtp.gmail.com with ESMTPSA id h4sm5008191pgk.72.2020.03.27.21.19.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Mar 2020 21:19:32 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 28 Mar 2020 09:49:17 +0530 Message-Id: <20200328041919.3024-1-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH v3 1/3] libavcodec/jpeg2000dec.c: Add functions and modify structs for PPT 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 modifies the Jpeg2000Tile struct and breaks the jpeg2000_decode_packet() function into two to decode packet header and data separately. --- libavcodec/jpeg2000dec.c | 250 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 248 insertions(+), 2 deletions(-) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 7103cd6ceb..001bc8d057 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -83,8 +83,12 @@ typedef struct Jpeg2000Tile { Jpeg2000QuantStyle qntsty[4]; Jpeg2000POC poc; Jpeg2000TilePart tile_part[32]; - uint16_t tp_idx; // Tile-part index - int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} + uint8_t has_ppt; // whether this tile has a ppt marker + uint8_t *packed_headers; // contains packed headers. Used only along with PPT marker + int packed_headers_size; // size in bytes of the packed headers + GetByteContext packed_headers_stream; // byte context corresponding to packed headers + uint16_t tp_idx; // Tile-part index + int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} } Jpeg2000Tile; typedef struct Jpeg2000DecoderContext { @@ -855,6 +859,38 @@ static int get_plt(Jpeg2000DecoderContext *s, int n) return 0; } +static int get_ppt(Jpeg2000DecoderContext *s, int n) +{ + Jpeg2000Tile *tile; + + tile = &s->tile[s->curtileno]; + + av_assert1(s->curtileno >= 0); + if (tile->tp_idx != 0) { + av_log(s->avctx, AV_LOG_ERROR, + "PPT marker can occur only on first tile part of a tile.\n"); + return AVERROR_INVALIDDATA; + } + + tile->has_ppt = 1; // this tile has a ppt marker +/* Zppt = */ bytestream2_get_byte(&s->g); + if (!tile->packed_headers) { + tile->packed_headers = av_malloc_array(n - 3, sizeof(uint8_t)); + memcpy(tile->packed_headers, s->g.buffer, sizeof(uint8_t)*(n - 3)); + tile->packed_headers_size = n - 3; + } else { + tile->packed_headers = av_realloc_array(tile->packed_headers, + tile->packed_headers_size + n - 3, + sizeof(uint8_t)); + memcpy(tile->packed_headers + tile->packed_headers_size, + s->g.buffer, sizeof(uint8_t)*(n - 3)); + tile->packed_headers_size += n - 3; + } + bytestream2_skip(&s->g, n - 3); + + return 0; +} + static int init_tile(Jpeg2000DecoderContext *s, int tileno) { int compno; @@ -927,6 +963,216 @@ static int getlblockinc(Jpeg2000DecoderContext *s) return res; } +static int jpeg2000_decode_packet_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, + int *tp_index, + Jpeg2000CodingStyle *codsty, + Jpeg2000ResLevel *rlevel, int precno, + int layno, uint8_t *expn, int numgbits, + int *process_data) +{ + int bandno, cblkno, ret, nb_code_blocks; + + if (layno < rlevel->band[0].prec[precno].decoded_layers) { + *process_data = 0; + return 0; + } + rlevel->band[0].prec[precno].decoded_layers = layno + 1; + + if (tile->has_ppt) { + s->g = tile->packed_headers_stream; + } else { + s->g = tile->tile_part[*tp_index].tpg; + if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) { + if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) { + s->g = tile->tile_part[++(*tp_index)].tpg; + } + } + if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES) + bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH); + } + + if (!(ret = get_bits(s, 1))) { + jpeg2000_flush(s); + *process_data = 0; + goto end; + } else if (ret < 0) + return ret; + + for (bandno = 0; bandno < rlevel->nbands; bandno++) { + Jpeg2000Band *band = rlevel->band + bandno; + Jpeg2000Prec *prec = band->prec + precno; + + if (band->coord[0][0] == band->coord[0][1] || + band->coord[1][0] == band->coord[1][1]) + continue; + nb_code_blocks = prec->nb_codeblocks_height * + prec->nb_codeblocks_width; + for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) { + Jpeg2000Cblk *cblk = prec->cblk + cblkno; + int incl, newpasses, llen; + void *tmp; + + if (cblk->npasses) + incl = get_bits(s, 1); + else + incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno; + if (!incl) + continue; + else if (incl < 0) + return incl; + + if (!cblk->npasses) { + int v = expn[bandno] + numgbits - 1 - + tag_tree_decode(s, prec->zerobits + cblkno, 100); + if (v < 0 || v > 30) { + av_log(s->avctx, AV_LOG_ERROR, + "nonzerobits %d invalid or unsupported\n", v); + return AVERROR_INVALIDDATA; + } + 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; + } + + cblk->lblock += llen; + + cblk->nb_lengthinc = 0; + cblk->nb_terminationsinc = 0; + av_free(cblk->lengthinc); + cblk->lengthinc = av_mallocz_array(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; + + while (newpasses1 < newpasses) { + newpasses1 ++; + if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) { + cblk->nb_terminationsinc ++; + break; + } + } + + if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0) + return ret; + if (ret > cblk->data_allocated) { + size_t new_size = FFMAX(2*cblk->data_allocated, ret); + void *new = av_realloc(cblk->data, new_size); + if (new) { + cblk->data = new; + cblk->data_allocated = new_size; + } + } + if (ret > 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); + } + } + jpeg2000_flush(s); + + if (codsty->csty & JPEG2000_CSTY_EPH) { + if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH) + bytestream2_skip(&s->g, 2); + else + av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g)); + } +end: + if (tile->has_ppt) + tile->packed_headers_stream = s->g; + else + tile->tile_part[*tp_index].tpg = s->g; + return 0; +} + +static int jpeg2000_decode_packet_data(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, + int *tp_index, + Jpeg2000CodingStyle *codsty, + Jpeg2000ResLevel *rlevel, int precno, + int layno, uint8_t *expn, int numgbits) +{ + int bandno, cblkno, nb_code_blocks; + int cwsno; + + s->g = tile->tile_part[*tp_index].tpg; + if (tile->has_ppt) { + if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) { + if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) { + s->g = tile->tile_part[++(*tp_index)].tpg; + } + } + if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES) + bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH); + } + + + for (bandno = 0; bandno < rlevel->nbands; bandno++) { + Jpeg2000Band *band = rlevel->band + bandno; + Jpeg2000Prec *prec = band->prec + precno; + + nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width; + for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) { + Jpeg2000Cblk *cblk = prec->cblk + cblkno; + if (!cblk->nb_terminationsinc && !cblk->lengthinc) + continue; + for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) { + if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) { + size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4); + void *new = av_realloc(cblk->data, new_size); + if (new) { + cblk->data = new; + cblk->data_allocated = new_size; + } + } + if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno] + || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4 + ) { + av_log(s->avctx, AV_LOG_ERROR, + "Block length %"PRIu16" or lengthinc %d is too large, left %d\n", + cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g)); + return AVERROR_INVALIDDATA; + } + + bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]); + cblk->length += cblk->lengthinc[cwsno]; + cblk->lengthinc[cwsno] = 0; + if (cblk->nb_terminationsinc) { + cblk->nb_terminationsinc--; + cblk->nb_terminations++; + cblk->data[cblk->length++] = 0xFF; + cblk->data[cblk->length++] = 0xFF; + cblk->data_start[cblk->nb_terminations] = cblk->length; + } + } + av_freep(&cblk->lengthinc); + } + } + tile->tile_part[*tp_index].tpg = s->g; + return 0; +} + static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index, Jpeg2000CodingStyle *codsty, Jpeg2000ResLevel *rlevel, int precno, From patchwork Sat Mar 28 04:19:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 18453 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 2D08444821D for ; Sat, 28 Mar 2020 06:42:32 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 124BE68B618; Sat, 28 Mar 2020 06:42:32 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 52C8368B3E7 for ; Sat, 28 Mar 2020 06:42:26 +0200 (EET) Received: by mail-pf1-f173.google.com with SMTP id z65so5579873pfz.8 for ; Fri, 27 Mar 2020 21:42:26 -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=VzG3jXVXbQLufuOCUbPI8NTgQ4/SjyLDZ6Op1n6j08Q=; b=i6RPbDsSCc+4xN1wzjZl+JBJaBIlwKxomdM6hJRUeehj2Cb2RwxckrGfpwGng64dLR MDecSEtz4Xd/a21Y4ERoySeuPkjQZj//36TKsI6hzeKxZlNCGQoBRqhs+bP9PQPw7dN8 swxal7drX75hisyYNMsFFS9AbHmK+sZezgtxMClHeFq0Cw2BaH0sEgiLAU/S/KV+udKo 2PRzraLr9kmLsBAtFsxgM3nJqyZ805WKUwPNOhVYLgXLJjAq8fooKCfHVpE2kN4LXXu6 2LukmZt1AxoZBupw4Ey6YB9V/Am3siehjcNsx+9Wzb4h9pKtywl/LYbC8mofrgnRVsWq 98Ag== 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=VzG3jXVXbQLufuOCUbPI8NTgQ4/SjyLDZ6Op1n6j08Q=; b=k3r1goDMUDneOighYf05/4O9d5OPmcadYzxqo21CbouAE/trUxSpew+V8kUlDD2ykX QFyZbpqhY6ZzwRHr6J+dsTIdNe2W0fX62nk4Xgwk+we4mpVhQuhOa4s3gFxqoaxWiAWB M6FKsoEhUTBWoxHvvz0+3CwVGjvVB8ZeSTJUIYdJtTG3ziIxu+blnSfUOhSZZ100P/uk gaWdW7n6uEms1fI5TMIZxhnbKu51KvzvDMyuRGCSJqgtNmAFD9h3uahQqIaAbuH8LHJe vKWWmeQLQskQfxbWsI4rEtPfQXfme5p4qRWgNmtOVl+2BbAL+gI3U3qhSmYWBRoPxUWh NQ6Q== X-Gm-Message-State: ANhLgQ1FQqRKuYRQVZMKjsHQIixy3qoaW9QYvEdvNqG8F1dqrxnzPpR5 JA6gTsY7SEEExiZAw5f5asj6DlPrGww= X-Google-Smtp-Source: ADFU+vslfWy0zCS4Ond758XEcLmQjYVRVmjQO2dy1C9mjca5CDPltBh1pF7jbbGgFCzfIiIqE4Fhfw== X-Received: by 2002:aa7:9d0a:: with SMTP id k10mr2707736pfp.266.1585369176468; Fri, 27 Mar 2020 21:19:36 -0700 (PDT) Received: from localhost.localdomain ([223.235.255.212]) by smtp.gmail.com with ESMTPSA id h4sm5008191pgk.72.2020.03.27.21.19.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Mar 2020 21:19:35 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 28 Mar 2020 09:49:18 +0530 Message-Id: <20200328041919.3024-2-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200328041919.3024-1-gautamramk@gmail.com> References: <20200328041919.3024-1-gautamramk@gmail.com> Subject: [FFmpeg-devel] [PATCH v3 2/3] libavcodec/jpeg2000dec.c: Functional changes to support PPT 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 functional changes to support the PPT marker. It allows the PPT marker to be read in jpeg2000_read_main_headers(). --- libavcodec/jpeg2000dec.c | 270 ++++++++++++--------------------------- 1 file changed, 82 insertions(+), 188 deletions(-) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 001bc8d057..44b3e7e41b 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -1173,179 +1173,6 @@ static int jpeg2000_decode_packet_data(Jpeg2000DecoderContext *s, Jpeg2000Tile * return 0; } -static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index, - Jpeg2000CodingStyle *codsty, - Jpeg2000ResLevel *rlevel, int precno, - int layno, uint8_t *expn, int numgbits) -{ - int bandno, cblkno, ret, nb_code_blocks; - int cwsno; - - if (layno < rlevel->band[0].prec[precno].decoded_layers) - return 0; - rlevel->band[0].prec[precno].decoded_layers = layno + 1; - - if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) { - if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) { - s->g = tile->tile_part[++(*tp_index)].tpg; - } - } - - if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES) - bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH); - - if (!(ret = get_bits(s, 1))) { - jpeg2000_flush(s); - return 0; - } else if (ret < 0) - return ret; - - for (bandno = 0; bandno < rlevel->nbands; bandno++) { - Jpeg2000Band *band = rlevel->band + bandno; - Jpeg2000Prec *prec = band->prec + precno; - - if (band->coord[0][0] == band->coord[0][1] || - band->coord[1][0] == band->coord[1][1]) - continue; - nb_code_blocks = prec->nb_codeblocks_height * - prec->nb_codeblocks_width; - for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) { - Jpeg2000Cblk *cblk = prec->cblk + cblkno; - int incl, newpasses, llen; - void *tmp; - - if (cblk->npasses) - incl = get_bits(s, 1); - else - incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno; - if (!incl) - continue; - else if (incl < 0) - return incl; - - if (!cblk->npasses) { - int v = expn[bandno] + numgbits - 1 - - tag_tree_decode(s, prec->zerobits + cblkno, 100); - if (v < 0 || v > 30) { - av_log(s->avctx, AV_LOG_ERROR, - "nonzerobits %d invalid or unsupported\n", v); - return AVERROR_INVALIDDATA; - } - 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; - } - - cblk->lblock += llen; - - cblk->nb_lengthinc = 0; - cblk->nb_terminationsinc = 0; - av_free(cblk->lengthinc); - cblk->lengthinc = av_mallocz_array(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; - - while (newpasses1 < newpasses) { - newpasses1 ++; - if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) { - cblk->nb_terminationsinc ++; - break; - } - } - - if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0) - return ret; - if (ret > cblk->data_allocated) { - size_t new_size = FFMAX(2*cblk->data_allocated, ret); - void *new = av_realloc(cblk->data, new_size); - if (new) { - cblk->data = new; - cblk->data_allocated = new_size; - } - } - if (ret > 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); - } - } - jpeg2000_flush(s); - - if (codsty->csty & JPEG2000_CSTY_EPH) { - if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH) - bytestream2_skip(&s->g, 2); - else - av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g)); - } - - for (bandno = 0; bandno < rlevel->nbands; bandno++) { - Jpeg2000Band *band = rlevel->band + bandno; - Jpeg2000Prec *prec = band->prec + precno; - - nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width; - for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) { - Jpeg2000Cblk *cblk = prec->cblk + cblkno; - if (!cblk->nb_terminationsinc && !cblk->lengthinc) - continue; - for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) { - if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) { - size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4); - void *new = av_realloc(cblk->data, new_size); - if (new) { - cblk->data = new; - cblk->data_allocated = new_size; - } - } - if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno] - || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4 - ) { - av_log(s->avctx, AV_LOG_ERROR, - "Block length %"PRIu16" or lengthinc %d is too large, left %d\n", - cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g)); - return AVERROR_INVALIDDATA; - } - - bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]); - cblk->length += cblk->lengthinc[cwsno]; - cblk->lengthinc[cwsno] = 0; - if (cblk->nb_terminationsinc) { - cblk->nb_terminationsinc--; - cblk->nb_terminations++; - cblk->data[cblk->length++] = 0xFF; - cblk->data[cblk->length++] = 0xFF; - cblk->data_start[cblk->nb_terminations] = cblk->length; - } - } - av_freep(&cblk->lengthinc); - } - } - return 0; -} - static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int RSpoc, int CSpoc, int LYEpoc, int REpoc, int CEpoc, @@ -1370,13 +1197,25 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno; ok_reslevel = 1; - for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) - if ((ret = jpeg2000_decode_packet(s, tile, tp_index, + for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) { + int process_data = 1; + if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index, + codsty, rlevel, + precno, layno, + qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), + qntsty->nguardbits, &process_data)) < 0) + return ret; + + if (!process_data) + continue; + + if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index, codsty, rlevel, precno, layno, qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), qntsty->nguardbits)) < 0) return ret; + } } } } @@ -1396,13 +1235,25 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno; ok_reslevel = 1; - for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) - if ((ret = jpeg2000_decode_packet(s, tile, tp_index, + for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) { + int process_data = 1; + if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index, + codsty, rlevel, + precno, layno, + qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), + qntsty->nguardbits, &process_data)) < 0) + return ret; + + if (!process_data) + continue; + + if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index, codsty, rlevel, precno, layno, qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), qntsty->nguardbits)) < 0) return ret; + } } } } @@ -1464,7 +1315,19 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2 } for (layno = 0; layno < LYEpoc; layno++) { - if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel, + int process_data = 1; + if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index, + codsty, rlevel, + precno, layno, + qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), + qntsty->nguardbits, &process_data)) < 0) + return ret; + + if (!process_data) + continue; + + if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index, + codsty, rlevel, precno, layno, qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), qntsty->nguardbits)) < 0) @@ -1534,14 +1397,25 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2 continue; } - for (layno = 0; layno < LYEpoc; layno++) { - if ((ret = jpeg2000_decode_packet(s, tile, tp_index, - codsty, rlevel, - precno, layno, - qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), - qntsty->nguardbits)) < 0) - return ret; - } + for (layno = 0; layno < LYEpoc; layno++) { + int process_data = 1; + if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index, + codsty, rlevel, + precno, layno, + qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), + qntsty->nguardbits, &process_data)) < 0) + return ret; + + if (!process_data) + continue; + + if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index, + codsty, rlevel, + precno, layno, + qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), + qntsty->nguardbits)) < 0) + return ret; + } } } } @@ -1605,7 +1479,19 @@ static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2 } for (layno = 0; layno < LYEpoc; layno++) { - if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel, + int process_data = 1; + if ((ret = jpeg2000_decode_packet_header(s, tile, tp_index, + codsty, rlevel, + precno, layno, + qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), + qntsty->nguardbits, &process_data)) < 0) + return ret; + + if (!process_data) + continue; + + if ((ret = jpeg2000_decode_packet_data(s, tile, tp_index, + codsty, rlevel, precno, layno, qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), qntsty->nguardbits)) < 0) @@ -2175,6 +2061,11 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s) av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n"); return AVERROR_INVALIDDATA; } + + if (tile->has_ppt && tile->tp_idx == 0) { + bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size); + } + bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer); bytestream2_skip(&s->g, tp->tp_end - s->g.buffer); @@ -2237,6 +2128,10 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s) // Packet length, tile-part header ret = get_plt(s, len); break; + case JPEG2000_PPT: + // Packed headers, tile-part header + ret = get_ppt(s, len); + break; default: av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n", @@ -2266,7 +2161,6 @@ static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s) if ((ret = init_tile(s, tileno)) < 0) return ret; - s->g = tile->tile_part[0].tpg; if ((ret = jpeg2000_decode_packets(s, tile)) < 0) return ret; } From patchwork Sat Mar 28 04:19:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 18451 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 A44FF44B8FF for ; Sat, 28 Mar 2020 06:19:48 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8569268B882; Sat, 28 Mar 2020 06:19:48 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 21BCC68B879 for ; Sat, 28 Mar 2020 06:19:41 +0200 (EET) Received: by mail-pl1-f170.google.com with SMTP id d24so437316pll.8 for ; Fri, 27 Mar 2020 21:19:41 -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=8yTUF/tpM9gxj+GxIQ+sCxzZxVZ13EC0pHHRKkLlK18=; b=qVdlwliLfPNneUEJ1XW4wTwr2xK31dHYclhRgQAs1J8Jz8L2Db2RQDpqUS/qAt2sVL HvP9EDmxJklJ/gCAOk6N5F23G+oC9oVuFpdbdxnKDSrI85XXln0OxgyBhleFvjDIyDNI gXMtcGhfIgDbUWdfS1iYOxTLWhcxoxzp/ZKTPPHZp0xX94TfZnA7bPYqIYxPrDPxl9co +8qNC5Biq5WKYWnyYtx/JkLFBgzm2DSWDvaaAwm17DRe34a+nMj4mGO4DQsBdDIZ3bhR cMsZBkSFyWcRpr3Sti5iXTVmratd3v7K8uoc24u3T66wZdmaW+CiYQySfjE2IV/9gchP qN+Q== 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=8yTUF/tpM9gxj+GxIQ+sCxzZxVZ13EC0pHHRKkLlK18=; b=MMnu0ma3/mtpUfpkBui6riI1s82mpE6gcQs4MFCeNYbaH5LkW3J+w3WrC3KdL9ugF+ jWC6E1QTtlF4YvmbuI7xrEZkeJEbt6tAKjJZB20XWSbZp6xCze2dywlO4CCOHH2ycF7F Gw1rtXrQYL9fjtbUBXLnJH5aPljf6kNaHmt7WkSiEqR5hW75uZ1gJYgVLswFEnJnPcqX YyxpUEL7ELAH/aH/69rtMV4nbOJTSiTKYrLB6sSBnCvhuHVRJmaNaBLogAAq6DaRb83l hk1EmXRavCgMFoCURk579xU7Pt2a2iRX3q1pNle2Lymy1D3hN4iniooebJhityV7zRbU KKtw== X-Gm-Message-State: ANhLgQ1l0TWUUcu1IgzgQxhLGPALb7MEQW4WWQvsCeCyIdP0RWiioN4o knQJiw23E4Ce3XqStJ30aiERF5k7Kvw= X-Google-Smtp-Source: ADFU+vvVLFS8yvRGCGTT21Y19C1iW0wRqt1iDKq/v62FvKr2uvcStVRfHnrDmNyiDnO9BJ9taZsMjg== X-Received: by 2002:a17:902:107:: with SMTP id 7mr2225374plb.302.1585369179253; Fri, 27 Mar 2020 21:19:39 -0700 (PDT) Received: from localhost.localdomain ([223.235.255.212]) by smtp.gmail.com with ESMTPSA id h4sm5008191pgk.72.2020.03.27.21.19.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Mar 2020 21:19:38 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 28 Mar 2020 09:49:19 +0530 Message-Id: <20200328041919.3024-3-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200328041919.3024-1-gautamramk@gmail.com> References: <20200328041919.3024-1-gautamramk@gmail.com> Subject: [FFmpeg-devel] [PATCH v3 3/3] libavcodec/jpeb2000dec.c: Handle non EOC streams 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 allows decoding of j2k streams which do not have an EOC marker. OpenJPEG implements a similar check. --- libavcodec/jpeg2000dec.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 44b3e7e41b..99fcb2cf68 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -2076,8 +2076,9 @@ static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s) len = bytestream2_get_be16(&s->g); if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) { - av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g)); - return AVERROR_INVALIDDATA; + av_log(s->avctx, AV_LOG_WARNING, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g)); + av_log(s->avctx, AV_LOG_WARNING, "Stream does not end with EOC.\n"); + return 0; } switch (marker) {