From patchwork Sun May 19 15:07:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Anthony Lemieux X-Patchwork-Id: 49030 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:3a48:b0:1af:fc2d:ff5a with SMTP id zu8csp3972360pzb; Sun, 19 May 2024 08:37:47 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVPVmiuzeh05XIp6Ica06YjfnFcvBzkwesRt8D8lHqZGIAmYpOYJRWV/8SrJ/ECnY4l2oTeGVoZnHdKHuI4lq6Xxuek5ogkguzUxw== X-Google-Smtp-Source: AGHT+IGudsoc0scx1JoUG3dXASXz6INA2ZeQGjaeZn9g+c9XWbi85q6cvLQA/YknQe+wzNlvTswX X-Received: by 2002:a50:9e6d:0:b0:572:8aab:441c with SMTP id 4fb4d7f45d1cf-5734d6ed908mr17496371a12.26.1716133067243; Sun, 19 May 2024 08:37:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1716133067; cv=none; d=google.com; s=arc-20160816; b=BBARfo5+pf5Wd1AdB2XsxEEBCZs8f1JYVno/fsjAoIqe0tChNZWVP98v2vnUfzc3sW BurgBjtExhlbh8PmM9Lg76L8TSCalARCi9P3qEKsGDXn8/b7Xaz5ibWUnBrZk8O5n7dF DDXdnOT9mXmDHHwPfkfwHWMWwhj39ziQe6mY9UHyA4iFCi8CqeIjbalXbposVE0X2aMK +AXYvrHHpH31AXN6anabFSkhrKx4w8i2UMbdq8NpSoho9lteOvpn4yUiwQ7WFYGnPzSp MSduZZbvRYBQSS2qOd5YDUMVxAO1wC89wDD2R5Mzg4gs6uRzSjacrUjvNx8uDDuM35RN VWvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=rNwOIPvI3Pk6P4ubdPBTlC5RRma28jRom9ta3mdtiSg=; fh=+XurmlYRMEKPhRXR+/nIyJb2QQRlsbrq6s3sAgqOh8E=; b=cBOBVaEoWV8Z+Reoi85GTPKJPyVvEbXmjKJ1wfQ6tDa0vPYy7AlZDGT9ot2+06DMne TgrJynBRrgsjU0KOtpI2Ptdz6fSpUVnavBOoEgdadiJskU66ztI3zxAd9vIlS4kedEzO Z1OFhKvrQ/zek8PTxv91GrZK07XXbXJNDEQjQkHQ+oYpolHjcrfvv5OWlLBfjNs0nApk xI0jhs/K4imIET500K2bz0dCGiUd1KRFL431xK/Gvn/SQ/9VV6gQyc7OH+ZuvPyfAY6W lKl9V8sx9diKgF6Yf+t24cdSsBKn6WiHp54wP9V7Y/GNa00MLYlaPKg9QvXoCZOuZwJ1 JwrQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sandflow-com.20230601.gappssmtp.com header.s=20230601 header.b=YzmNWLEl; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 4fb4d7f45d1cf-574e0a7db44si7156483a12.266.2024.05.19.08.37.46; Sun, 19 May 2024 08:37:47 -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=@sandflow-com.20230601.gappssmtp.com header.s=20230601 header.b=YzmNWLEl; 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 738A368B004; Sun, 19 May 2024 18:37:43 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5263668C1EF for ; Sun, 19 May 2024 18:37:36 +0300 (EEST) Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a59cf8140d0so520389266b.3 for ; Sun, 19 May 2024 08:37:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sandflow-com.20230601.gappssmtp.com; s=20230601; t=1716133055; x=1716737855; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=x4gJcnRH6l8BpFv1qpIYynY+vqP6gGZa+z8Ecro3MkE=; b=YzmNWLElq6r5G5WN5aY02YAPBj321bRh0jhQ1WABmMvrMTd5E1t4aOMtJjeArMgTfp ZP3JGtQ+1SMBLRnpEU14efQFshWUQy0yMbJ2vLzxCeeOc94lP0b8UQFJ+UHTopbmONO+ yPlNASFezkVYa8UwWZj+dBoiCaAUil+JbaKXCoZ5zxO6sudJI9oklSG2H4T3QT0xJsM/ IG2asxBbPvdDQMtesHIzYy1woi+RxC08FXhkHXK4v09LmBWpGUPeXmqznzj3BiV00NzX LzPBGzBwA+vg/muiws60vvrAABdcw/HCBS8ILbQDCb48teU73C00T+978YUKmb3lkDnB 4Xhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716133055; x=1716737855; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=x4gJcnRH6l8BpFv1qpIYynY+vqP6gGZa+z8Ecro3MkE=; b=k5eyOzkAQNUa4knjo6M7J6fw92Rk8IgteF2ccrE6bssfP9nGrN14XwRS5GtRPzw9MD vmU7WiEE2xBdUPajX7HQfCt8c8mdfq1ZnV+2YS0/Sd9N3oJG0NUs7EN0BX4kNP5QWms8 SI8G3acw/69ahu9pIe6HjOd/T8765aZtBycfHIwIGWjT4EW+eAMrBqCU8C3/qtqcwM49 oxZRKC3HlOL0/JQMKIFqONMqKhGSyBUDewI3WxhqoUF3+1xAjCPD8yMrSLwjboLnnInw TVV2WMDkBQs2FspCOGHdRaxOPchgzLxO53cDbAFbSemjWNzXia3nG3uREKiEzCiCyBQk UP5g== X-Gm-Message-State: AOJu0Yw0oPsQXinl8SsCXAv99ButoNOp1ALDWjsNjlF06RAbijulnoYB iVejXBhBpg8HKxGX7iBJ4THaEE5XwGdCkVSe7FC6JQrVmIFLUshBmgK9rkdkmnyJ/DaqJ6i5hcd E+NU= X-Received: by 2002:a17:906:1401:b0:a59:b136:e40f with SMTP id a640c23a62f3a-a5a2d5d08e0mr1718956666b.38.1716133055088; Sun, 19 May 2024 08:37:35 -0700 (PDT) Received: from pal-laptop-2023.sandflow.com ([62.214.2.210]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a5a17922159sm1363065066b.97.2024.05.19.08.37.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 May 2024 08:37:34 -0700 (PDT) Received: by pal-laptop-2023.sandflow.com (sSMTP sendmail emulation); Sun, 19 May 2024 17:07:41 +0200 From: pal@sandflow.com To: ffmpeg-devel@ffmpeg.org Date: Sun, 19 May 2024 17:07:38 +0200 Message-Id: <20240519150738.89960-1-pal@sandflow.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/jpeg2000dec: fix HT block decoder 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 , Pierre-Anthony Lemieux Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: c1MvrziuU1Cp From: Pierre-Anthony Lemieux Addresses https://trac.ffmpeg.org/ticket/10905 Co-authored-by: Osamu Watanabe Signed-off-by: Pierre-Anthony Lemieux --- libavcodec/jpeg2000htdec.c | 122 ++++++++++++----------- tests/ref/fate/jpeg2000dec-ds0_ht_01_b11 | 2 +- 2 files changed, 63 insertions(+), 61 deletions(-) diff --git a/libavcodec/jpeg2000htdec.c b/libavcodec/jpeg2000htdec.c index fa704b665e..62f70c32a8 100644 --- a/libavcodec/jpeg2000htdec.c +++ b/libavcodec/jpeg2000htdec.c @@ -196,7 +196,7 @@ static void jpeg2000_bitbuf_refill_forward(StateVars *buffer, const uint8_t *arr while (buffer->bits_left < 32) { buffer->tmp = 0xFF; buffer->bits = (buffer->last == 0xFF) ? 7 : 8; - if (buffer->pos <= length) { + if (buffer->pos < length) { buffer->tmp = array[buffer->pos]; buffer->pos += 1; buffer->last = buffer->tmp; @@ -508,17 +508,17 @@ static int jpeg2000_decode_sig_emb(const Jpeg2000DecoderContext *s, MelDecoderSt } av_always_inline -static int jpeg2000_get_state(int x1, int x2, int width, int shift_by, +static int jpeg2000_get_state(int x1, int x2, int stride, int shift_by, const uint8_t *block_states) { - return (block_states[(x1 + 1) * (width + 2) + (x2 + 1)] >> shift_by) & 1; + return (block_states[(x1 + 1) * stride + (x2 + 1)] >> shift_by) & 1; } av_always_inline -static void jpeg2000_modify_state(int x1, int x2, int width, - int value, uint8_t *block_states) +static void jpeg2000_modify_state(int x1, int x2, int stride, + int value, uint8_t *block_states) { - block_states[(x1 + 1) * (width + 2) + (x2 + 1)] |= value; + block_states[(x1 + 1) * stride + (x2 + 1)] |= value; } av_always_inline @@ -528,8 +528,8 @@ static int jpeg2000_decode_ht_cleanup_segment(const Jpeg2000DecoderContext *s, StateVars *mel_stream, StateVars *vlc_stream, StateVars *mag_sgn_stream, const uint8_t *Dcup, uint32_t Lcup, uint32_t Pcup, uint8_t pLSB, - int width, int height, int32_t *sample_buf, - uint8_t *block_states) + int width, int height, const int stride, + int32_t *sample_buf, uint8_t *block_states) { uint16_t q = 0; // Represents current quad position uint16_t q1, q2; @@ -958,32 +958,32 @@ static int jpeg2000_decode_ht_cleanup_segment(const Jpeg2000DecoderContext *s, j1 = 2 * y; j2 = 2 * x; - sample_buf[j2 + (j1 * width)] = (int32_t)*mu; - jpeg2000_modify_state(j1, j2, width, *sigma, block_states); + sample_buf[j2 + (j1 * stride)] = (int32_t)*mu; + jpeg2000_modify_state(j1, j2, stride, *sigma, block_states); sigma += 1; mu += 1; x1 = y != quad_height - 1 || is_border_y == 0; - sample_buf[j2 + ((j1 + 1) * width)] = ((int32_t)*mu) * x1; - jpeg2000_modify_state(j1 + 1, j2, width, (*sigma) * x1, block_states); + sample_buf[j2 + ((j1 + 1) * stride)] = ((int32_t)*mu) * x1; + jpeg2000_modify_state(j1 + 1, j2, stride, (*sigma) * x1, block_states); sigma += 1; mu += 1; x2 = x != quad_width - 1 || is_border_x == 0; - sample_buf[(j2 + 1) + (j1 * width)] = ((int32_t)*mu) * x2; - jpeg2000_modify_state(j1, j2 + 1, width, (*sigma) * x2, block_states); + sample_buf[(j2 + 1) + (j1 * stride)] = ((int32_t)*mu) * x2; + jpeg2000_modify_state(j1, j2 + 1, stride, (*sigma) * x2, block_states); sigma += 1; mu += 1; x3 = x1 | x2; - sample_buf[(j2 + 1) + (j1 + 1) * width] = ((int32_t)*mu) * x3; - jpeg2000_modify_state(j1 + 1, j2 + 1, width, (*sigma) * x3, block_states); + sample_buf[(j2 + 1) + (j1 + 1) * stride] = ((int32_t)*mu) * x3; + jpeg2000_modify_state(j1 + 1, j2 + 1, stride, (*sigma) * x3, block_states); sigma += 1; mu += 1; } } ret = 1; -free: + free: av_freep(&sigma_n); av_freep(&E); av_freep(&mu_n); @@ -992,39 +992,39 @@ free: 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 width) + uint8_t *block_states, int stride) { int local_mbr = 0; - local_mbr |= jpeg2000_get_state(i - 1, j - 1, width, HT_SHIFT_SIGMA, block_states); - local_mbr |= jpeg2000_get_state(i - 1, j + 0, width, HT_SHIFT_SIGMA, block_states); - local_mbr |= jpeg2000_get_state(i - 1, j + 1, width, 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 - 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, width, HT_SHIFT_SIGMA, block_states); - local_mbr |= jpeg2000_get_state(i + 0, j + 1, width, 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, width, HT_SHIFT_SIGMA, block_states) * causal_cond; - local_mbr |= jpeg2000_get_state(i + 1, j + 0, width, HT_SHIFT_SIGMA, block_states) * causal_cond; - local_mbr |= jpeg2000_get_state(i + 1, j + 1, width, 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 + 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, width, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i - 1, j - 1, width, HT_SHIFT_SCAN, block_states); - local_mbr |= jpeg2000_get_state(i - 1, j + 0, width, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i - 1, j - 1, width, HT_SHIFT_SCAN, block_states); - local_mbr |= jpeg2000_get_state(i - 1, j + 1, width, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i - 1, j + 1, width, 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 - 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, width, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 0, j - 1, width, HT_SHIFT_SCAN, block_states); - local_mbr |= jpeg2000_get_state(i + 0, j + 1, width, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 0, j + 1, width, 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, width, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 1, j - 1, width, HT_SHIFT_SCAN, block_states) * causal_cond; - local_mbr |= jpeg2000_get_state(i + 1, j + 0, width, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 1, j + 0, width, HT_SHIFT_SCAN, block_states) * causal_cond; - local_mbr |= jpeg2000_get_state(i + 1, j + 1, width, HT_SHIFT_REF, block_states) * - jpeg2000_get_state(i + 1, j + 1, width, 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; + 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; } @@ -1040,18 +1040,18 @@ static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s int modify_state, cond; uint8_t bit; uint8_t causal_cond = i != (i_s + height - 1); - int32_t *sp = &sample_buf[j + (i * (stride - 2))]; + 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) - jpeg2000_calc_mbr(&mbr, i, j, mbr_info & 0x1EF, causal_cond, block_states, stride - 2); + 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; - jpeg2000_modify_state(i, j, stride - 2, modify_state, block_states); + jpeg2000_modify_state(i, j, stride, modify_state, block_states); } } } @@ -1060,8 +1060,8 @@ static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s * See procedure decodeSigPropMag at Rec. ITU-T T.814, 7.4. */ av_noinline -static void jpeg2000_decode_sigprop_segment(Jpeg2000Cblk *cblk, uint16_t width, - uint16_t height, uint8_t *magref_segment, +static void jpeg2000_decode_sigprop_segment(Jpeg2000Cblk *cblk, uint16_t width, uint16_t height, + const int stride, uint8_t *magref_segment, uint32_t magref_length, uint8_t pLSB, int32_t *sample_buf, uint8_t *block_states) { @@ -1071,7 +1071,6 @@ static void jpeg2000_decode_sigprop_segment(Jpeg2000Cblk *cblk, uint16_t width, const uint16_t num_h_stripe = width / 4; int b_width = 4; int b_height = 4; - int stride = width + 2; int last_width; uint16_t i = 0, j = 0; @@ -1114,7 +1113,7 @@ static void jpeg2000_decode_sigprop_segment(Jpeg2000Cblk *cblk, uint16_t width, * See procedure decodeSigPropMag at Rec. ITU-T T.814, 7.5. */ static int -jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, +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) { @@ -1134,7 +1133,7 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, * We move column wise, going from one quad to another. See * Rec. ITU-T T.814, Figure 7. */ - sp = &sample_buf[j + i * width]; + 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; @@ -1146,7 +1145,7 @@ jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height, height = block_height % 4; for (int j = 0; j < width; j++) { for (int i = i_start; i < i_start + height; i++) { - sp = &sample_buf[j + i * width]; + 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; @@ -1194,6 +1193,9 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c int32_t M_b = magp; + const int quad_buf_width = width + 4; + const int quad_buf_height = height + 4; + /* codeblock size as constrained by Rec. ITU-T T.800, Table A.18 */ av_assert0(width <= 1024U && height <= 1024U); av_assert0(width * height <= 4096); @@ -1261,23 +1263,23 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c jpeg2000_init_mel_decoder(&mel_state); - sample_buf = av_calloc((width + 4) * (height + 4), sizeof(int32_t)); - block_states = av_calloc((width + 4) * (height + 4), sizeof(uint8_t)); + sample_buf = av_calloc(quad_buf_width * quad_buf_height, sizeof(int32_t)); + block_states = av_calloc(quad_buf_width * quad_buf_height, sizeof(uint8_t)); if (!sample_buf || !block_states) { ret = AVERROR(ENOMEM); goto free; } if ((ret = jpeg2000_decode_ht_cleanup_segment(s, cblk, t1, &mel_state, &mel, &vlc, - &mag_sgn, Dcup, Lcup, Pcup, pLSB, width, - height, sample_buf, block_states)) < 0) { + &mag_sgn, Dcup, Lcup, Pcup, pLSB, width, + height, quad_buf_width, sample_buf, block_states)) < 0) { av_log(s->avctx, AV_LOG_ERROR, "Bad HT cleanup segment\n"); goto free; } if (cblk->npasses > 1) - jpeg2000_decode_sigprop_segment(cblk, width, height, Dref, Lref, - pLSB - 1, sample_buf, block_states); + jpeg2000_decode_sigprop_segment(cblk, width, height, quad_buf_width, Dref, Lref, + pLSB - 1, sample_buf, block_states); if (cblk->npasses > 2) { @@ -1286,7 +1288,7 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c ret = AVERROR_INVALIDDATA; goto free; } - if ((ret = jpeg2000_decode_magref_segment(width, height, Dref, Lref, + if ((ret = jpeg2000_decode_magref_segment(width, height, quad_buf_width, Dref, Lref, pLSB - 1, sample_buf, block_states)) < 0) goto free; } @@ -1297,14 +1299,14 @@ ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { n = x + (y * t1->stride); - val = sample_buf[x + (y * width)]; + val = sample_buf[x + (y * quad_buf_width)]; /* Convert sign-magnitude to two's complement. */ val = val >> 31 ? 0x80000000 - val : val; val >>= (pLSB - 1); t1->data[n] = val; } } -free: + free: av_freep(&sample_buf); av_freep(&block_states); return ret; diff --git a/tests/ref/fate/jpeg2000dec-ds0_ht_01_b11 b/tests/ref/fate/jpeg2000dec-ds0_ht_01_b11 index af3eaa086d..d923cc2109 100644 --- a/tests/ref/fate/jpeg2000dec-ds0_ht_01_b11 +++ b/tests/ref/fate/jpeg2000dec-ds0_ht_01_b11 @@ -3,4 +3,4 @@ #codec_id 0: rawvideo #dimensions 0: 128x128 #sar 0: 0/1 -0, 0, 0, 1, 16384, 0x44426324 +0, 0, 0, 1, 16384, 0x04a3647e