From patchwork Sat Mar 28 12:28:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 18454 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 DC6D144B602 for ; Sat, 28 Mar 2020 14:28:26 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B527468B4AB; Sat, 28 Mar 2020 14:28:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E06C968B0FE for ; Sat, 28 Mar 2020 14:28:19 +0200 (EET) Received: by mail-pg1-f196.google.com with SMTP id 142so6015587pgf.11 for ; Sat, 28 Mar 2020 05:28:19 -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=yFlZdJqBEVhoc2XIOusfVivvb+xfb7cuOajejOd4amw=; b=M6W1PaNKHCW66FLB7aKyOD8K73A8TyVTnwhZc/vt8JjFOWu0d3/iMVELcrbqkk3SvS U7bb6ZxV2Paj5NIR3xcEbrW6hGKl20TcSqnGn+kTulFliW7xNJKXJMBZ1U1Fm53hrxCO k9v1qUOD3aqmd0zy295M8ARrG9L+ha5Mq0D29C7uSrd3frMdCLLF1oy8Ej36oz47nvPH IRv9u+LsO9m0IXSOTl9962JW6ys7H5u/ek6XVCPRgzGWQGR0X2KDcV/0Fz8xTIjhKOQD 6OQrssDBL9dS03tbvDUpcqE9mL7xJcahkY73YXSa89/59wGtv7dTpAIPTlvIkDUiMaZV 9Xmw== 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=yFlZdJqBEVhoc2XIOusfVivvb+xfb7cuOajejOd4amw=; b=btmywJkLnXUuCqy/vA6+dzBqqwDBr8Ftu9hktV/rGnseGsLXb3lLl+QD62EdMT1Pw7 D1qOiJzIgXo52mRCI0EdJV8VQKTRmbNELqSWm8VjCWqGDH4XiAp1VhzKOk9Buc7VNUcq TUpbiWhKd4n3+QwAGngOYWji51QRT0chJS88DrHaO/VuPW4HF5M9Ap8rk/VGi9u72Zed /6SOHXTVCKMd+qP7anPnB1K8+eDEu9PRMemZ3LaoTAmUGABHPi3PDz5ubrNGDnAPTBf9 C37ZaqTgmn/ZQmg1hc0zBWZB7+UW+tARQzLZLxvuod3iqkpgXJiqRMgOo2ADpVj7iFv/ 7l3g== X-Gm-Message-State: ANhLgQ2G/jxJSmflK5iSiRzfxHcIfwh2pw+j9nsijgq6SMncVCXUvxuw D+1yKZ32H0a6vmknP211YuxxkuG27LU= X-Google-Smtp-Source: ADFU+vv9tNjRYOBhmYAcBrfPNikkqjbptI7omsyWQYQxD5bTrsfS8YlDN6Jm5qV3k32MIELBO+FMeg== X-Received: by 2002:aa7:8607:: with SMTP id p7mr4330560pfn.198.1585398497424; Sat, 28 Mar 2020 05:28:17 -0700 (PDT) Received: from localhost.localdomain ([223.235.255.212]) by smtp.gmail.com with ESMTPSA id d23sm6078522pfq.210.2020.03.28.05.28.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Mar 2020 05:28:16 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 28 Mar 2020 17:58:09 +0530 Message-Id: <20200328122810.25084-1-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH v4 1/2] 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 adds support for the PPT marker. It breaks down the jpeg2000_decode_packet() function to decode headers and data separately. --- libavcodec/jpeg2000dec.c | 200 +++++++++++++++++++++++++++++++++------ 1 file changed, 170 insertions(+), 30 deletions(-) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 7103cd6ceb..44b3e7e41b 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,30 +963,38 @@ static int getlblockinc(Jpeg2000DecoderContext *s) return res; } -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) +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; - int cwsno; - if (layno < rlevel->band[0].prec[precno].decoded_layers) + if (layno < rlevel->band[0].prec[precno].decoded_layers) { + *process_data = 0; 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 (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 (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; + *process_data = 0; + goto end; } else if (ret < 0) return ret; @@ -1055,6 +1099,34 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, 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; @@ -1097,6 +1169,7 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, av_freep(&cblk->lengthinc); } } + tile->tile_part[*tp_index].tpg = s->g; return 0; } @@ -1124,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; + } } } } @@ -1150,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; + } } } } @@ -1218,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) @@ -1288,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; + } } } } @@ -1359,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) @@ -1929,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); @@ -1991,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", @@ -2020,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; }