From patchwork Fri Sep 4 23:17:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22106 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 0D0BD443897 for ; Sat, 5 Sep 2020 02:17:57 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E8D0168B503; Sat, 5 Sep 2020 02:17:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AF0DD68ABA8 for ; Sat, 5 Sep 2020 02:17:49 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id o5so8901967wrn.13 for ; Fri, 04 Sep 2020 16:17:49 -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 :mime-version:content-transfer-encoding; bh=HuuLsnFzhturcvt7eldznGxauToWuqi6q18NHhlGwwo=; b=Nh8sKnPOz5Xa0C1wMxDn+dHAiFI5bsyKzOBkNR2kOWmOROEbO2ZpB9zS1/74Xbjkf/ 9bRDhc3JC/+f9pXYvLPlbfoslNUJ3OTpsZAwoH369O9YiumC3Z0xFejVfV5YB5aRRBDR +plH8XxKSsqLzIdQ3V+MVJMm3MmpbXuNoky3JQMkUKh5AVNXkIa2fzgEhI0x2KEHb1jJ 54SqxD6P0eaYsneucL+HU7JkUL/Ium8PYWgJVNFkg6HCnyFh3hj++xGWcN5iyFAke1Zd V+8NLvjcPm0A53DPoS/I/kAIga9zExNXH3nbVAaa6a5pP8RGA+zUar1BpYPv3+KTJeFe 82ZA== 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:mime-version:content-transfer-encoding; bh=HuuLsnFzhturcvt7eldznGxauToWuqi6q18NHhlGwwo=; b=G+UEksmuFtKx5Ooi4Q9pVnSpazx7AB/T6Oi9/M++gXdU5hT5MneCqbeAX+rsFzWf1n hNMZ3Ra6uwOwySVHkUWxX/JNlmBLit6I0V2wHvUJEZA3ssee9QVMBXyZ0R/HwVG233Um /OM/fNUntqajnYFJ07aoM0MGgI84TFhAkYiN7QQDxcHxKMhxMlLkfvN+u2b7mB4EwC0P yJLWA16kaXCvpzjuPlf6/fqoZy/zj5fXnln1ME8YgydER5bokplEikdsYMMaMSa8TH60 SpkehJSBIiciLYtov6X94B1lIqmW83KrD64HwE0Fv4ajahv2UxoBWzzxAjimdkMBve0e Lgkw== X-Gm-Message-State: AOAM530k5ISzddhqz/gYBqPmK5x7yfgtYb8w2HgJe1UGn9n4AVHnGU1C Gk4ZWEdHgHq9LNX1hEju+QakkYR6JaQ= X-Google-Smtp-Source: ABdhPJzqIOkqwJ2sE6lBhEkK+E8ZkwCNPk4m0yaNQTAedMx6GL5mREA4IR+bx3QOGiWRq88Q/+mZBQ== X-Received: by 2002:adf:fd8d:: with SMTP id d13mr9963478wrr.104.1599261468735; Fri, 04 Sep 2020 16:17:48 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id f14sm14598098wrv.72.2020.09.04.16.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 16:17:48 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Sep 2020 01:17:12 +0200 Message-Id: <20200904231716.16182-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200904231716.16182-1-andreas.rheinhardt@gmail.com> References: <20200904231716.16182-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/8] avcodec/jpeglsenc: Only use one line at a time as spare buffer 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" ls_encode_line() encodes one line of input from left to right and to do so it uses the values of the left, upper left, upper and upper right pixels for prediction (i.e. the values that a decoder gets when it decodes the already encoded part of the picture). So a simple algorithm would use a buffer that can hold two lines, namely the current line as well as the last line and swap the pointers to the two lines after decoding each line. Yet if one is currently encoding the pixel with index k of a line, one doesn't need any pixel with index < k - 1 of the last line at all and similarly, no pixels with index >= k have been written yet. So the overlap in the effective lifetime is pretty limited and since the last patch (which stopped reading the upper left pixel and instead reused the value of the upper pixel from the last iteration of the loop) it is inexistent. Ergo one only needs one line and doesn't need to swap the lines out. Signed-off-by: Andreas Rheinhardt --- libavcodec/jpeglsenc.c | 52 ++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c index b9c04e979c..d05fe4564b 100644 --- a/libavcodec/jpeglsenc.c +++ b/libavcodec/jpeglsenc.c @@ -141,19 +141,19 @@ static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, * Encode one line of image */ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, - void *last, void *cur, const void *in, int last2, int w, + void *tmp, const void *in, int last2, int w, int stride, int comp, int bits) { int x = 0; - int Ra = R(last, 0), Rb, Rc = last2, Rd; + int Ra = R(tmp, 0), Rb, Rc = last2, Rd; int D0, D1, D2; while (x < w) { int err, pred, sign; /* compute gradients */ - Rb = R(last, x); - Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride); + Rb = R(tmp, x); + Rd = (x >= w - stride) ? R(tmp, x) : R(tmp, x + stride); D0 = Rd - Rb; D1 = Rb - Rc; D2 = Rc - Ra; @@ -168,13 +168,13 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, RUNval = Ra; while (x < w && (FFABS(R(in, x) - RUNval) <= state->near)) { run++; - W(cur, x, Ra); + W(tmp, x, Ra); x += stride; } ls_encode_run(state, pb, run, comp, x < w); if (x >= w) return; - Rb = R(last, x); + Rb = R(tmp, x); RItype = FFABS(Ra - Rb) <= state->near; pred = RItype ? Ra : Rb; err = R(in, x) - pred; @@ -194,7 +194,7 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, Ra = av_clip(pred - err * state->twonear, 0, state->maxval); } else Ra = R(in, x); - W(cur, x, Ra); + W(tmp, x, Ra); if (err < 0) err += state->range; @@ -236,7 +236,7 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, Ra = av_clip(pred - err * state->twonear, 0, state->maxval); } else Ra = R(in, x); - W(cur, x, Ra); + W(tmp, x, Ra); ls_encode_regular(state, pb, context, err); } @@ -277,9 +277,8 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, PutBitContext pb2; GetBitContext gb; uint8_t *buf2 = NULL; - uint8_t *zero = NULL; const uint8_t *in; - uint8_t *last, *cur; + uint8_t *last = NULL; JLSState *state = NULL; int i, size, ret; int comps; @@ -345,28 +344,27 @@ FF_ENABLE_DEPRECATION_WARNINGS ls_store_lse(state, &pb); - zero = last = av_calloc(FFABS(p->linesize[0]), 2); - if (!zero) + last = av_mallocz(FFABS(p->linesize[0])); + if (!last) goto memfail; - cur = zero + FFABS(p->linesize[0]); in = p->data[0]; if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) { int t = 0; for (i = 0; i < avctx->height; i++) { - ls_encode_line(state, &pb2, last, cur, in, t, avctx->width, 1, 0, 8); - t = last[0]; - FFSWAP(void *, last, cur); + int last0 = last[0]; + ls_encode_line(state, &pb2, last, in, t, avctx->width, 1, 0, 8); + t = last0; in += p->linesize[0]; } } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY16) { int t = 0; for (i = 0; i < avctx->height; i++) { - ls_encode_line(state, &pb2, last, cur, in, t, avctx->width, 1, 0, 16); - t = *((uint16_t *)last); - FFSWAP(void *, last, cur); + int last0 = *((uint16_t *)last); + ls_encode_line(state, &pb2, last, in, t, avctx->width, 1, 0, 16); + t = last0; in += p->linesize[0]; } } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) { @@ -376,11 +374,11 @@ FF_ENABLE_DEPRECATION_WARNINGS width = avctx->width * 3; for (i = 0; i < avctx->height; i++) { for (j = 0; j < 3; j++) { - ls_encode_line(state, &pb2, last + j, cur + j, in + j, Rc[j], + int last0 = last[j]; + ls_encode_line(state, &pb2, last + j, in + j, Rc[j], width, 3, j, 8); - Rc[j] = last[j]; + Rc[j] = last0; } - FFSWAP(void *, last, cur); in += p->linesize[0]; } } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) { @@ -390,16 +388,16 @@ FF_ENABLE_DEPRECATION_WARNINGS width = avctx->width * 3; for (i = 0; i < avctx->height; i++) { for (j = 2; j >= 0; j--) { - ls_encode_line(state, &pb2, last + j, cur + j, in + j, Rc[j], + int last0 = last[j]; + ls_encode_line(state, &pb2, last + j, in + j, Rc[j], width, 3, j, 8); - Rc[j] = last[j]; + Rc[j] = last0; } - FFSWAP(void *, last, cur); in += p->linesize[0]; } } - av_freep(&zero); + av_freep(&last); av_freep(&state); /* the specification says that after doing 0xff escaping unused bits in @@ -436,7 +434,7 @@ memfail: av_packet_unref(pkt); av_freep(&buf2); av_freep(&state); - av_freep(&zero); + av_freep(&last); return AVERROR(ENOMEM); }