From patchwork Fri Sep 4 23:17:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22103 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 B4EDC44B56A for ; Sat, 5 Sep 2020 02:17:32 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 92A8768AB18; Sat, 5 Sep 2020 02:17:32 +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 5DECF68AAB0 for ; Sat, 5 Sep 2020 02:17:26 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id j2so8945320wrx.7 for ; Fri, 04 Sep 2020 16:17: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:mime-version :content-transfer-encoding; bh=KTUIH/R+HoF1MtBdq0JewJnQzPEnVwibJyGS1UaMj9s=; b=m0N5Zl/R3OTRwVJrs6LyJG4rs3fDft8fN7bpczoxGvz/Xo9AbDubiIQCdqj8SdOJM5 hLPGICka1Hlrtfbj/2aUnmgWywB/3Gf97N3pPdbCg76fOeD+4OeR6UBn0MdXxv0IQY/k pVCjpPYM2sEaLx2EUJHPcmt0sWUU7eMXacvif26YkM5yTV3j84Xhz+po+MbruZJVN2ZC 3L8lPiYZTgLYziORPrdBT9ZKvxeDT99U6sAsLrOt8NLvMmgcT2/zrEQ2x604Mx5zBmqI JAnIdBq4byHe5nBCC6EIwXL11KEEz3XkVfEGTKtEOWrFMy8OCWmE7CcZDxGL1ybMi7dG iSUA== 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:mime-version :content-transfer-encoding; bh=KTUIH/R+HoF1MtBdq0JewJnQzPEnVwibJyGS1UaMj9s=; b=mUyPPsr+igZAmeVYg0TtBoPoYXO+NuyNPBG7BHzSy7jAYCVpPcKGxgncWn1/Gxvwhs XAyWJoPYebY65f1UrbLBdU4ahMFs5RNX2L8h1LP877i4qj0XujS2mnQ0ynxgADch43YU PP3/DsQlvrqT+nyGky8LLwpdGts4s4sxvXcrZ3lQ5BkNcO7pYcgmRlgPclP70WLxnTcj uYPuvhKLYUW4b678TFdNaUMZJvPGfstn/ubhaZTJ7iC5lk7Hpso0SWN98shosSrJS768 qLE6jintr9qarILrmd4fLEfM+sKNRZR5B5b2ialvHu2iIZfYMB1gx0R8B4j3+F3IUZVs ZxFA== X-Gm-Message-State: AOAM530goT0IxnDDGX1ZHg/4mcGabc2d/358sOMMtYe8v7rCpn8u7v87 H+nmYLRxOtnc3fMnLLQKBm2epC3NdwQ= X-Google-Smtp-Source: ABdhPJx9O+7SLNFJgOKcCPCqoIRjUqAPKRo/hytba+O04++ywtjAChe4dOVGjDmwj7nFbIYePScNMQ== X-Received: by 2002:adf:fe42:: with SMTP id m2mr9530680wrs.367.1599261445286; Fri, 04 Sep 2020 16:17:25 -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.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 16:17:24 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Sep 2020 01:17:09 +0200 Message-Id: <20200904231716.16182-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/8] avcodec/jpeglsenc: Don't use put bits API for byte-aligned writes 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" Signed-off-by: Andreas Rheinhardt --- Is it actually guaranteed that the allocated packet size is sufficient? If it is, one could use unchecked writes throughout; if it isn't, should I add a check to properly error out in case the buffer is too small? (And what would the return value be in this case?) libavcodec/jpeglsenc.c | 80 +++++++++++++++++++++--------------- libavcodec/mjpegenc.h | 6 --- libavcodec/mjpegenc_common.c | 6 +++ 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c index 5ecd430db7..feea6fdde7 100644 --- a/libavcodec/jpeglsenc.c +++ b/libavcodec/jpeglsenc.c @@ -26,6 +26,7 @@ */ #include "avcodec.h" +#include "bytestream.h" #include "get_bits.h" #include "put_bits.h" #include "golomb.h" @@ -41,6 +42,18 @@ typedef struct JPEGLSContext { int pred; } JPEGLSContext; +static inline void put_markeru(PutByteContext *pb, enum JpegMarker code) +{ + bytestream2_put_byteu(pb, 0xff); + bytestream2_put_byteu(pb, code); +} + +static inline void put_marker(PutByteContext *pb, enum JpegMarker code) +{ + bytestream2_put_byte(pb, 0xff); + bytestream2_put_byte(pb, code); +} + /** * Encode error from regular symbol */ @@ -230,7 +243,7 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, } } -static void ls_store_lse(JLSState *state, PutBitContext *pb) +static void ls_store_lse(JLSState *state, PutByteContext *pb) { /* Test if we have default params and don't need to store LSE */ JLSState state2 = { 0 }; @@ -243,14 +256,14 @@ static void ls_store_lse(JLSState *state, PutBitContext *pb) state->reset == state2.reset) return; /* store LSE type 1 */ - put_marker(pb, LSE); - put_bits(pb, 16, 13); - put_bits(pb, 8, 1); - put_bits(pb, 16, state->maxval); - put_bits(pb, 16, state->T1); - put_bits(pb, 16, state->T2); - put_bits(pb, 16, state->T3); - put_bits(pb, 16, state->reset); + put_markeru(pb, LSE); + bytestream2_put_be16u(pb, 13); + bytestream2_put_byteu(pb, 1); + bytestream2_put_be16u(pb, state->maxval); + bytestream2_put_be16u(pb, state->T1); + bytestream2_put_be16u(pb, state->T2); + bytestream2_put_be16u(pb, state->T3); + bytestream2_put_be16u(pb, state->reset); } static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, @@ -258,7 +271,8 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, { JPEGLSContext *ctx = avctx->priv_data; const AVFrame *const p = pict; - PutBitContext pb, pb2; + PutByteContext pb; + PutBitContext pb2; GetBitContext gb; uint8_t *buf2 = NULL; uint8_t *zero = NULL; @@ -289,33 +303,33 @@ FF_ENABLE_DEPRECATION_WARNINGS if (!buf2) goto memfail; - init_put_bits(&pb, pkt->data, pkt->size); + bytestream2_init_writer(&pb, pkt->data, pkt->size); init_put_bits(&pb2, buf2, pkt->size); /* write our own JPEG header, can't use mjpeg_picture_header */ - put_marker(&pb, SOI); - put_marker(&pb, SOF48); - put_bits(&pb, 16, 8 + comps * 3); // header size depends on components - put_bits(&pb, 8, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp - put_bits(&pb, 16, avctx->height); - put_bits(&pb, 16, avctx->width); - put_bits(&pb, 8, comps); // components + put_markeru(&pb, SOI); + put_markeru(&pb, SOF48); + bytestream2_put_be16u(&pb, 8 + comps * 3); // header size depends on components + bytestream2_put_byteu(&pb, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp + bytestream2_put_be16u(&pb, avctx->height); + bytestream2_put_be16u(&pb, avctx->width); + bytestream2_put_byteu(&pb, comps); // components for (i = 1; i <= comps; i++) { - put_bits(&pb, 8, i); // component ID - put_bits(&pb, 8, 0x11); // subsampling: none - put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext + bytestream2_put_byteu(&pb, i); // component ID + bytestream2_put_byteu(&pb, 0x11); // subsampling: none + bytestream2_put_byteu(&pb, 0); // Tiq, used by JPEG-LS ext } - put_marker(&pb, SOS); - put_bits(&pb, 16, 6 + comps * 2); - put_bits(&pb, 8, comps); + put_markeru(&pb, SOS); + bytestream2_put_be16u(&pb, 6 + comps * 2); + bytestream2_put_byteu(&pb, comps); for (i = 1; i <= comps; i++) { - put_bits(&pb, 8, i); // component ID - put_bits(&pb, 8, 0); // mapping index: none + bytestream2_put_byteu(&pb, i); // component ID + bytestream2_put_byteu(&pb, 0); // mapping index: none } - put_bits(&pb, 8, ctx->pred); - put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line - put_bits(&pb, 8, 0); // point transform: none + bytestream2_put_byteu(&pb, ctx->pred); + bytestream2_put_byteu(&pb, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line + bytestream2_put_byteu(&pb, 0); // point transform: none state = av_mallocz(sizeof(JLSState)); if (!state) @@ -397,22 +411,20 @@ FF_ENABLE_DEPRECATION_WARNINGS while (get_bits_count(&gb) < size) { int v; v = get_bits(&gb, 8); - put_bits(&pb, 8, v); + bytestream2_put_byte(&pb, v); if (v == 0xFF) { v = get_bits(&gb, 7); - put_bits(&pb, 8, v); + bytestream2_put_byte(&pb, v); } } - avpriv_align_put_bits(&pb); av_freep(&buf2); /* End of image */ put_marker(&pb, EOI); - flush_put_bits(&pb); emms_c(); - pkt->size = put_bits_count(&pb) >> 3; + pkt->size = bytestream2_tell_p(&pb); pkt->flags |= AV_PKT_FLAG_KEY; *got_packet = 1; return 0; diff --git a/libavcodec/mjpegenc.h b/libavcodec/mjpegenc.h index d7ddc35ef5..a4307dbf35 100644 --- a/libavcodec/mjpegenc.h +++ b/libavcodec/mjpegenc.h @@ -98,12 +98,6 @@ enum HuffmanTableOption { NB_HUFFMAN_TABLE_OPTION = 2 }; -static inline void put_marker(PutBitContext *p, enum JpegMarker code) -{ - put_bits(p, 8, 0xff); - put_bits(p, 8, code); -} - int ff_mjpeg_encode_init(MpegEncContext *s); void ff_mjpeg_encode_close(MpegEncContext *s); void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64]); diff --git a/libavcodec/mjpegenc_common.c b/libavcodec/mjpegenc_common.c index 3038ebde6e..fa965071a0 100644 --- a/libavcodec/mjpegenc_common.c +++ b/libavcodec/mjpegenc_common.c @@ -62,6 +62,12 @@ av_cold void ff_init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac } } +static inline void put_marker(PutBitContext *p, enum JpegMarker code) +{ + put_bits(p, 8, 0xff); + put_bits(p, 8, code); +} + /* table_class: 0 = DC coef, 1 = AC coefs */ static int put_huffman_table(PutBitContext *p, int table_class, int table_id, const uint8_t *bits_table, const uint8_t *value_table) From patchwork Fri Sep 4 23:17:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22104 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 048A644B56A for ; Sat, 5 Sep 2020 02:17:55 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D35E568B32D; Sat, 5 Sep 2020 02:17:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D9809687ED6 for ; Sat, 5 Sep 2020 02:17:47 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id j2so8945791wrx.7 for ; Fri, 04 Sep 2020 16:17:47 -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=0Wul8fNjDuTPPvGwZQwWUrEBK5Hg+9cL+/VFuiCF1gQ=; b=kyl3vT72AOwumvN/ndJGHGrpVvz6hU6TB5N9qxBhG8LHYGyk8h4pjInyLNoKe56SPo qYJzZoz1wTYjkVV0RcnVCF9+qkNOXau7GzLg5IE96JXopf2ClLGh5ZC3rd8L8CxR4VD7 7YJQF3o/JDOE05naO7brvLidx7XCgWrCjZrxs5+wdYcoy01IxYXn1JxsQ94h3Blp3py3 1dYsm0dhsoAA1ujlSfK7B4KMgdQSGMU0+nGRwehPO35t92fZd4e0Viiq0W9K5aKvK43q UtaqqJuK5YfiGv0xNnxu9sqGWCtehEXcpfXJphI2gCacIehbeS9QesklNQRak2Akvp7H lU6w== 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=0Wul8fNjDuTPPvGwZQwWUrEBK5Hg+9cL+/VFuiCF1gQ=; b=TZM9o/QNi+j4LfQttiYg4vJwXr006gtGtKWHsOr6Db0XIuHIi8p/nsTBRgC9ns3pB6 piOspRRoSZ7vrtc+lupdjoRMFWYGAu+698t3Fsq/bQdKSc2GL8AOeu4q/slyL8acWEOU ayrnBp3J0bc7T2648lEAgH2SLNBEt/SvmpX9Upzxm2DlEq+sY9FmUICixf5M+Mz/4N1R PxdFViP4fBaldCSj/xns6EkqkA9cRbMTO8HfTFmKzjShnOCmtDpBT2T8Zx1ct1CGCzX7 h3HyniMma1K2XoH/mJkMNlEL0MzD3QOpP2PFvek0JOz2ug/nM+snoGv6cE9mbwXEZdRs 0WHw== X-Gm-Message-State: AOAM5332kfwLAZcbUBjzSB9E+9+3zeGKaA7L7qXQKxjfMiA0kg/7KRNg 5imNiZrM6CBxUDqKwDNmOIoKn7AhSNM= X-Google-Smtp-Source: ABdhPJzYBN0WgnayPFPugUbm+TzL26cJnBCcJTATRTi+aSeC1WzIMShl0AzPvuOfnv6dYIRDYL6dmA== X-Received: by 2002:adf:e6cf:: with SMTP id y15mr9733648wrm.346.1599261466784; Fri, 04 Sep 2020 16:17:46 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 16:17:46 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Sep 2020 01:17:10 +0200 Message-Id: <20200904231716.16182-2-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 2/8] avcodec/jpeglsenc: Don't modify frame we don't own 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" The jpegls encoder uses three buffers (as well as its state) to perform its function: A copy of the last encoded line as a decoder would decode it, the part of the current line that has been encoded (again, as a decoder would decode it) and the part of the current line that is not yet encoded. The encoder solves this by modifying the input frame as it encodes the output (it also zero-allocates a line to serve as last line for the first line where no preceding line exists); yet this is wrong as said frame is not owned by the encoder, so it must not be modified (and it is given to the encoder as const AVFrame *) without making it writable. This patch solves this bug by allocating two lines, one for the last and one for the currently encoded line of output (as a decoder would decode it). Notice that the frame is only modified if the encoder is in the non-default non-lossless mode. Signed-off-by: Andreas Rheinhardt --- Example command line to trigger this behaviour: ffmpeg -i fate-suite/png1/lena-rgb24.png -an -map 0 -map 0 -map 0 -c:v:0 jpegls -c:v:1 jpegls -pred:v:1 median -c:v:2 jpegls -f streamhash - The output of the first and third stream differ with git master. (For some reason "pred" is used to set how lossy the mode is.) libavcodec/jpeglsenc.c | 50 +++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c index feea6fdde7..67897ce8ae 100644 --- a/libavcodec/jpeglsenc.c +++ b/libavcodec/jpeglsenc.c @@ -141,7 +141,7 @@ 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, int last2, int w, + void *last, void *cur, const void *in, int last2, int w, int stride, int comp, int bits) { int x = 0; @@ -168,7 +168,7 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, run = 0; RUNval = Ra; - while (x < w && (FFABS(R(cur, x) - RUNval) <= state->near)) { + while (x < w && (FFABS(R(in, x) - RUNval) <= state->near)) { run++; W(cur, x, Ra); x += stride; @@ -179,7 +179,7 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, Rb = R(last, x); RItype = FFABS(Ra - Rb) <= state->near; pred = RItype ? Ra : Rb; - err = R(cur, x) - pred; + err = R(in, x) - pred; if (!RItype && Ra > Rb) err = -err; @@ -195,7 +195,9 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, else Ra = av_clip(pred - err * state->twonear, 0, state->maxval); W(cur, x, Ra); - } + } else + W(cur, x, R(in, x)); + if (err < 0) err += state->range; if (err >= state->range + 1 >> 1) @@ -218,11 +220,11 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, context = -context; sign = 1; pred = av_clip(pred - state->C[context], 0, state->maxval); - err = pred - R(cur, x); + err = pred - R(in, x); } else { sign = 0; pred = av_clip(pred + state->C[context], 0, state->maxval); - err = R(cur, x) - pred; + err = R(in, x) - pred; } if (state->near) { @@ -235,7 +237,8 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, else Ra = av_clip(pred - err * state->twonear, 0, state->maxval); W(cur, x, Ra); - } + } else + W(cur, x, R(in, x)); ls_encode_regular(state, pb, context, err); } @@ -276,8 +279,8 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, GetBitContext gb; uint8_t *buf2 = NULL; uint8_t *zero = NULL; - uint8_t *cur = NULL; - uint8_t *last = NULL; + const uint8_t *in; + uint8_t *last, *cur; JLSState *state = NULL; int i, size, ret; int comps; @@ -343,28 +346,29 @@ FF_ENABLE_DEPRECATION_WARNINGS ls_store_lse(state, &pb); - zero = last = av_mallocz(FFABS(p->linesize[0])); + zero = last = av_calloc(FFABS(p->linesize[0]), 2); if (!zero) goto memfail; + cur = zero + FFABS(p->linesize[0]); - cur = p->data[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, t, avctx->width, 1, 0, 8); + ls_encode_line(state, &pb2, last, cur, in, t, avctx->width, 1, 0, 8); t = last[0]; - last = cur; - cur += p->linesize[0]; + FFSWAP(void *, last, cur); + 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, t, avctx->width, 1, 0, 16); + ls_encode_line(state, &pb2, last, cur, in, t, avctx->width, 1, 0, 16); t = *((uint16_t *)last); - last = cur; - cur += p->linesize[0]; + FFSWAP(void *, last, cur); + in += p->linesize[0]; } } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) { int j, width; @@ -373,12 +377,12 @@ 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, Rc[j], + ls_encode_line(state, &pb2, last + j, cur + j, in + j, Rc[j], width, 3, j, 8); Rc[j] = last[j]; } - last = cur; - cur += p->linesize[0]; + FFSWAP(void *, last, cur); + in += p->linesize[0]; } } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) { int j, width; @@ -387,12 +391,12 @@ 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, Rc[j], + ls_encode_line(state, &pb2, last + j, cur + j, in + j, Rc[j], width, 3, j, 8); Rc[j] = last[j]; } - last = cur; - cur += p->linesize[0]; + FFSWAP(void *, last, cur); + in += p->linesize[0]; } } From patchwork Fri Sep 4 23:17:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22105 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 02AEE440046 for ; Sat, 5 Sep 2020 02:17:56 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DE9EB68B2B1; Sat, 5 Sep 2020 02:17:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A4B71687ED6 for ; Sat, 5 Sep 2020 02:17:48 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id w5so8949729wrp.8 for ; Fri, 04 Sep 2020 16:17:48 -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=1vH+sQ7iqDnm4kxjdFVJH+uZ+fTWmuC3HgbD2wf4VdA=; b=m1tBCNnzoK1GojPE5bswQAahPAJMC8FFMDaU0d95KUpKESvuF4VbRg6xkPqi7o578P jU0d6YyoMhPgT2Z80xbFyfkDDzbBfW9s+UpKKLWyfHF5O5jek6T0Lh5wkPhY5uCeubhC pi8yPOhc6RHXmw7qp1iw2vHFSRw5u++6TVr5HDzknNMbcBiiTahl63vsCUhDjsDAZ2yE 9gIyUX2A01ctngyedKUlGO4eL2EngyvSu3OAI2evXbvptG391n3aln+yu65lP02A48hV floVGuhvpVtsZ7teU3gn/rnXuSD5t6DjmW0+Xll7+s0O5qwT+XxMjgjo1u7ikkhOEcVr vfAA== 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=1vH+sQ7iqDnm4kxjdFVJH+uZ+fTWmuC3HgbD2wf4VdA=; b=CoOKZaWoQCRePu9QI6XHZbLdIOGrkwG6bHgSpOBqvKRHTv0AGK4aLWRJ+85xbPl9oX KLsX5pl3egoc0vGM8QKJi9Y0LeHvP6mABrNGBa/9ll2kBy/tChSIhOcYCxfcJfiyKY1f i4ImLJnNK2XNBjRYoKO1Lev90zktNT1oHs65UXLWdMl2tqKEZnjExj/bbDPlOYl/lzCv AzCdRjQc8Wh6czZKuygIYr1IlrsWZrV5MYMJRCOsoUupQJaLiL97BJh/0QjCm++7vleK 1ksfZ5qymsK108RqABLcbhKDeyCORDUykxuh3emFCKnUR/34NQ+EAhx3eQnpMSBBLlus 1TxA== X-Gm-Message-State: AOAM532k6FRucHf8Etpe9X9v93bFgWiCyYGVqhNeQR/3ag8Sqs6azN7f xkKb1qlHVw96u1Km0VWE3mQUPGhmk+E= X-Google-Smtp-Source: ABdhPJx1zIHpl8ny7IrbeQO9CepIPKfLA4/XRuM0qLIYuVg0XwDk60w9bjureakAoFVkX0zGYShRPw== X-Received: by 2002:adf:9e43:: with SMTP id v3mr5335027wre.306.1599261467751; Fri, 04 Sep 2020 16:17:47 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 16:17:47 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Sep 2020 01:17:11 +0200 Message-Id: <20200904231716.16182-3-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 3/8] avcodec/jpeglsenc: Move check out of loop 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 a line of input, going from left to right. In order to calculate a predicted value it uses the left and upper-left value of the output picture (that is, it uses how a decoder would see the already encoded part of the picture), unless this is the very first pixel of this line in which case one uses the first pixel of the last (upper) line and the line before the last line. Therefore the loop contained a check for whether this is the beginning of a new line. This commit moves said check out of the loop by initializing these values before the loop and by updating these values at the end of the loop body; already read/calculated values are reused for this (the prediction also needs the value of the upper pixel and this can be reused for the upper left value of the next iteration of the loop). Signed-off-by: Andreas Rheinhardt --- libavcodec/jpeglsenc.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c index 67897ce8ae..b9c04e979c 100644 --- a/libavcodec/jpeglsenc.c +++ b/libavcodec/jpeglsenc.c @@ -145,16 +145,14 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, int stride, int comp, int bits) { int x = 0; - int Ra, Rb, Rc, Rd; + int Ra = R(last, 0), Rb, Rc = last2, Rd; int D0, D1, D2; while (x < w) { int err, pred, sign; /* compute gradients */ - Ra = x ? R(cur, x - stride) : R(last, x); Rb = R(last, x); - Rc = x ? R(last, x - stride) : last2; Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride); D0 = Rd - Rb; D1 = Rb - Rc; @@ -194,9 +192,9 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, Ra = av_clip(pred + err * state->twonear, 0, state->maxval); else Ra = av_clip(pred - err * state->twonear, 0, state->maxval); - W(cur, x, Ra); } else - W(cur, x, R(in, x)); + Ra = R(in, x); + W(cur, x, Ra); if (err < 0) err += state->range; @@ -236,12 +234,13 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, Ra = av_clip(pred + err * state->twonear, 0, state->maxval); else Ra = av_clip(pred - err * state->twonear, 0, state->maxval); - W(cur, x, Ra); } else - W(cur, x, R(in, x)); + Ra = R(in, x); + W(cur, x, Ra); ls_encode_regular(state, pb, context, err); } + Rc = Rb; x += stride; } } 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); } From patchwork Fri Sep 4 23:17:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22107 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 EF2C7447D3F 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 D732968B494; Sat, 5 Sep 2020 02:17:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7A63168B2B1 for ; Sat, 5 Sep 2020 02:17:50 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id e16so8971392wrm.2 for ; Fri, 04 Sep 2020 16:17: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:in-reply-to:references :mime-version:content-transfer-encoding; bh=F6yMBQ1lxPAelKaGW1oZf8mkzeeNbMosIFHR4Bbay/c=; b=ZV5uuCpxD0f5xKyBhNzjXCgg60FJGulsQjnUEOi4F1em0YOK94tarSKjyIjcXJppfd Em+mKh1/XJ89gPCt0LH2Te+1Rzy/xYe5VD4FdfifIdKAHcKtCBzY3vFgb6MSw25yEqXn TYqJnobbujjmxQHlt0rDCp78DDDxsMxaTwuI/5FDVeDyCEjoLHV/znnuffkRhQJsCJ+/ 1N0gfrTeC9m5DaWsJViPT0/nsx68ccAYIbpv0s24Ji5jfOxHKrh74Brog3xK0wspiTI4 fKOWqqFZOyaIq39JvdVgGAOqUYC20i0vZfDEIgN9VkBbW8iYuY6TCHWtRunIGC91CbI2 ta6Q== 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=F6yMBQ1lxPAelKaGW1oZf8mkzeeNbMosIFHR4Bbay/c=; b=satcudXVTswb8gg+dKHbJdIq3gjUzcps0/nXcHEn+4XNJ9wP8AO+r8MQCAO1wpCldM YZwuPOtCIvZda9KEc2OWnp/kQiJS3Ncf+rjHWXW8NgT6AM/3uooHrw613YThd0GvBfvt oVNetias16etkBFWd2GOIeVVHR4H4xljyu2J+HJPYvCrIsVI4v7UamgSwwA/FD+2yp6f 0C/9q1enIZ81qhpR9MHb/20IjTXyulTBeqg4nF69OA/Bw/aVgdaa0P3hTec8Davs7XT5 ke9+IkRK7m0TlGbFqyscmCrAfY8vudBTHDN88jeWL4ImtosE+au2NSeRdUrXVWim/WKX In7w== X-Gm-Message-State: AOAM532SZJ3cJzCCKlWSmgIBwWfrYszJ3dgN4r3iEvt9ksv8BBwy8iV5 X+RnZyvAMQorNHPk3+tGd7VwaKvA/XY= X-Google-Smtp-Source: ABdhPJyV2sk/zCzmp2ud4fj+t+9eY5xySguyt28uoXVkyC7w5qYdIjrGVQMTf0LWiF38ZiizLBFmMA== X-Received: by 2002:adf:ed12:: with SMTP id a18mr10366205wro.178.1599261469768; Fri, 04 Sep 2020 16:17:49 -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.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 16:17:49 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Sep 2020 01:17:13 +0200 Message-Id: <20200904231716.16182-5-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 5/8] avcodec/jpeglsenc: Remove redundant av_packet_unref() 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" If encoding fails, the AVPacket that ought to contain the encoded packet is already unreferenced generically. Signed-off-by: Andreas Rheinhardt --- libavcodec/jpeglsenc.c | 1 - 1 file changed, 1 deletion(-) diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c index d05fe4564b..b1e54431e9 100644 --- a/libavcodec/jpeglsenc.c +++ b/libavcodec/jpeglsenc.c @@ -431,7 +431,6 @@ FF_ENABLE_DEPRECATION_WARNINGS return 0; memfail: - av_packet_unref(pkt); av_freep(&buf2); av_freep(&state); av_freep(&last); From patchwork Fri Sep 4 23:17:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22108 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 E9CBF449193 for ; Sat, 5 Sep 2020 02:17:58 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D941368B50A; Sat, 5 Sep 2020 02:17:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BE19668B4CF for ; Sat, 5 Sep 2020 02:17:51 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id e17so8239696wme.0 for ; Fri, 04 Sep 2020 16:17:51 -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=vQKtsQAbo39csEGhT8UgGXVOyPlCc1OekDrjKZoyfJ8=; b=t1wwk0Zmjt3ZXdIcDWuHjtM0Hjr3I0gAfjmzcNm0PmkH6faXkxnKZuUu4kDXeLyBTa J9IPTG3rcpnlvdmlbKp3UxEwtvOG2puqYqN/g+IySsurJY8sQRzVH0ODpZFQv0fVQUki 4gUNCNVw1H23khn1tnQXB6pjZ5aJGVpR+kaiPFQMZupkpil//KUcv+4Ovr8mCZrh6hmW FwpEFh/FEe+glY1am7skZsQZ4rJsp5PYhsVrIu37FGxkFWwZLHXGonGhCuYc8fn/39+L wcy+zNVSLlUSjWKTdawYwbnyaLRcBtRu7Q1s1hM2cHjUSheL9LCtvbXZ7zG6rX8GFfHn IwoQ== 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=vQKtsQAbo39csEGhT8UgGXVOyPlCc1OekDrjKZoyfJ8=; b=HSDLom4JFQ2Ui8a8VIPdc49lgS2dGaLQXgyd1cvwZKo7VDaPMAt3ZsfOl3CjXC/EXG ytA5oCvWgojz+5sITZOyYUs+/WjbsOPHAqkPRcPCvSx7B0PbsLRum1B97TgApnGjYEKm CSSk5jtkan41mUobm6yZnRfFSWSvm34qvIvHY0Id0dJ/m2faM7N0tzKlMv83fTCxVq+7 rLWdgz0hYz8d/fG7gjUTU3uYOY8NTVTdXBjtfP/LEbp4fxKYWoXq8jc635XFEe7X93F3 Kk+xpIBx9V0uh5v65+DOREguMQZFuMuOfqiN7VgSZXMqh1vxdp138fJdduC+ipJuojmV bGTQ== X-Gm-Message-State: AOAM531hzR03kE9ZDL/ICP2QpZDe8nJ+2a6tirgf7wTXaRVB3/GZ+QXW lkThudsJy5HSp0Bn0U/RBABFD0c7eOc= X-Google-Smtp-Source: ABdhPJzMv3FMYG0PjUef/qtGtnn97o/f+v1yYOoiSdbZzXiX5Q16AdiQUj9n8vtIBCNONwAwH0HazA== X-Received: by 2002:a7b:cb47:: with SMTP id v7mr9535600wmj.36.1599261470780; Fri, 04 Sep 2020 16:17:50 -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.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 16:17:50 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Sep 2020 01:17:14 +0200 Message-Id: <20200904231716.16182-6-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 6/8] avcodec/jpeglsenc: Avoid allocation of JLSState 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" This state is currently allocated and freed for every packet; but it can just be moved to the stack instead. Signed-off-by: Andreas Rheinhardt --- libavcodec/jpeglsenc.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c index b1e54431e9..7fd109f0cf 100644 --- a/libavcodec/jpeglsenc.c +++ b/libavcodec/jpeglsenc.c @@ -279,7 +279,7 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, uint8_t *buf2 = NULL; const uint8_t *in; uint8_t *last = NULL; - JLSState *state = NULL; + JLSState state = { 0 }; int i, size, ret; int comps; @@ -332,17 +332,13 @@ FF_ENABLE_DEPRECATION_WARNINGS bytestream2_put_byteu(&pb, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line bytestream2_put_byteu(&pb, 0); // point transform: none - state = av_mallocz(sizeof(JLSState)); - if (!state) - goto memfail; - /* initialize JPEG-LS state from JPEG parameters */ - state->near = ctx->pred; - state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8; - ff_jpegls_reset_coding_parameters(state, 0); - ff_jpegls_init_state(state); + state.near = ctx->pred; + state.bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8; + ff_jpegls_reset_coding_parameters(&state, 0); + ff_jpegls_init_state(&state); - ls_store_lse(state, &pb); + ls_store_lse(&state, &pb); last = av_mallocz(FFABS(p->linesize[0])); if (!last) @@ -354,7 +350,7 @@ FF_ENABLE_DEPRECATION_WARNINGS for (i = 0; i < avctx->height; i++) { int last0 = last[0]; - ls_encode_line(state, &pb2, last, in, t, avctx->width, 1, 0, 8); + ls_encode_line(&state, &pb2, last, in, t, avctx->width, 1, 0, 8); t = last0; in += p->linesize[0]; } @@ -363,7 +359,7 @@ FF_ENABLE_DEPRECATION_WARNINGS for (i = 0; i < avctx->height; i++) { int last0 = *((uint16_t *)last); - ls_encode_line(state, &pb2, last, in, t, avctx->width, 1, 0, 16); + ls_encode_line(&state, &pb2, last, in, t, avctx->width, 1, 0, 16); t = last0; in += p->linesize[0]; } @@ -375,7 +371,7 @@ FF_ENABLE_DEPRECATION_WARNINGS for (i = 0; i < avctx->height; i++) { for (j = 0; j < 3; j++) { int last0 = last[j]; - ls_encode_line(state, &pb2, last + j, in + j, Rc[j], + ls_encode_line(&state, &pb2, last + j, in + j, Rc[j], width, 3, j, 8); Rc[j] = last0; } @@ -389,7 +385,7 @@ FF_ENABLE_DEPRECATION_WARNINGS for (i = 0; i < avctx->height; i++) { for (j = 2; j >= 0; j--) { int last0 = last[j]; - ls_encode_line(state, &pb2, last + j, in + j, Rc[j], + ls_encode_line(&state, &pb2, last + j, in + j, Rc[j], width, 3, j, 8); Rc[j] = last0; } @@ -398,7 +394,6 @@ FF_ENABLE_DEPRECATION_WARNINGS } av_freep(&last); - av_freep(&state); /* the specification says that after doing 0xff escaping unused bits in * the last byte must be set to 0, so just append 7 "optional" zero bits @@ -432,7 +427,6 @@ FF_ENABLE_DEPRECATION_WARNINGS memfail: av_freep(&buf2); - av_freep(&state); av_freep(&last); return AVERROR(ENOMEM); } From patchwork Fri Sep 4 23:17:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22109 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 F1214449193 for ; Sat, 5 Sep 2020 02:17:59 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DBF9068B525; Sat, 5 Sep 2020 02:17:59 +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 A527C68B40E for ; Sat, 5 Sep 2020 02:17:52 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id z4so8959629wrr.4 for ; Fri, 04 Sep 2020 16:17: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 :mime-version:content-transfer-encoding; bh=5H6tN9+Q6r9XQHqdDvu6Nh5yedVMqOCzKZhrJp6aQCw=; b=qKzNEKkex2JnxxnhtMjZYq5fv3JPVMg5/7rC58NskFd9wplKJKYHVSgs6gTkG0yNgU U9nuvMsNDTO1/mmtEIemtu2WLu0+2PCJHdh7ENL5ebWyAwamDet+kvRdiY4G36YyYbl2 yFIqv2fjXgn+5tb2BJant9dnYY6dzg5tKfFXy7PTTjMcZGapRamblqhefUYyd6+gpO0+ CVTPZlAPfb4TOb8AerASfRw5vd2QS3eWOgT/h0g9YP17bp8DMay7MeCiciO11g7iicvl v4VFZxYdJXICJyOl6b0bfDag7GF6ZKm0fI6LyrLEJ73bNhiv+N0qvSsUms/N59i2h2pX u4ZQ== 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=5H6tN9+Q6r9XQHqdDvu6Nh5yedVMqOCzKZhrJp6aQCw=; b=jzC4rVWTuSo3S6akI7K020kt3N2KaDAMx7VBY0mtjBJbePQ2Ke3VHVFRvEXnspcDP/ /V+/Pxv08Zyw+LdHofInDq1RrdBRj1ydlhrDzWGQudfBUpX2USDdHjHtQA24ZcL/M9AP 1Ywamw9SLLj0pR0VAJZgfaAoNv/Amgikq2v70o+Rrwd4K81Ng/FuK30EETjpgDJcJ26l hygiKPypY+lL3jm+MPradCiYl9NJdI5WfNOzq5b1F7JfP3aqRK+9r9e5C/5r5LENjUyf S883Aqorz2GpNiGM261BDptuo9lYNGlTtuDzsYtQJoBklyFDT0cx7Cm/hJyb9HS+ln+y Xe+Q== X-Gm-Message-State: AOAM530thgCAFgOjW2MPcvfCWpusWw8hH7cfr/alp9dDrU1Pbxsg9c+q zlDfauL0S47V5RcDiIQoTmbKKlSEB/A= X-Google-Smtp-Source: ABdhPJwxRTpPQZMBvmpAaXmd8CukE6cuHGO2EA7Kd9fdMtWOIE0yA+z7+l4M7TcK5ug7offmBEsjUQ== X-Received: by 2002:a5d:4bcf:: with SMTP id l15mr9639576wrt.384.1599261471690; Fri, 04 Sep 2020 16:17:51 -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.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 16:17:51 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Sep 2020 01:17:15 +0200 Message-Id: <20200904231716.16182-7-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 7/8] avcodec/jpeglsenc: Allocate buffers together 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" Having only one allocation that is not automatically freed in particular means that one does not need to free the already allocated buffers when allocating another one fails. Signed-off-by: Andreas Rheinhardt --- libavcodec/jpeglsenc.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c index 7fd109f0cf..304b0806a7 100644 --- a/libavcodec/jpeglsenc.c +++ b/libavcodec/jpeglsenc.c @@ -276,7 +276,6 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, PutByteContext pb; PutBitContext pb2; GetBitContext gb; - uint8_t *buf2 = NULL; const uint8_t *in; uint8_t *last = NULL; JLSState state = { 0 }; @@ -300,12 +299,13 @@ FF_ENABLE_DEPRECATION_WARNINGS AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0) return ret; - buf2 = av_malloc(pkt->size); - if (!buf2) - goto memfail; + last = av_malloc((unsigned)pkt->size + FFABS(p->linesize[0])); + if (!last) + return AVERROR(ENOMEM); + memset(last, 0, FFABS(p->linesize[0])); bytestream2_init_writer(&pb, pkt->data, pkt->size); - init_put_bits(&pb2, buf2, pkt->size); + init_put_bits(&pb2, last + FFABS(p->linesize[0]), pkt->size); /* write our own JPEG header, can't use mjpeg_picture_header */ put_markeru(&pb, SOI); @@ -340,10 +340,6 @@ FF_ENABLE_DEPRECATION_WARNINGS ls_store_lse(&state, &pb); - last = av_mallocz(FFABS(p->linesize[0])); - if (!last) - goto memfail; - in = p->data[0]; if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) { int t = 0; @@ -393,8 +389,6 @@ FF_ENABLE_DEPRECATION_WARNINGS } } - av_freep(&last); - /* the specification says that after doing 0xff escaping unused bits in * the last byte must be set to 0, so just append 7 "optional" zero bits * to avoid special-casing. */ @@ -402,7 +396,7 @@ FF_ENABLE_DEPRECATION_WARNINGS size = put_bits_count(&pb2); flush_put_bits(&pb2); /* do escape coding */ - init_get_bits(&gb, buf2, size); + init_get_bits(&gb, pb2.buf, size); size -= 7; while (get_bits_count(&gb) < size) { int v; @@ -413,7 +407,7 @@ FF_ENABLE_DEPRECATION_WARNINGS bytestream2_put_byte(&pb, v); } } - av_freep(&buf2); + av_freep(&last); /* End of image */ put_marker(&pb, EOI); @@ -424,11 +418,6 @@ FF_ENABLE_DEPRECATION_WARNINGS pkt->flags |= AV_PKT_FLAG_KEY; *got_packet = 1; return 0; - -memfail: - av_freep(&buf2); - av_freep(&last); - return AVERROR(ENOMEM); } static av_cold int encode_init_ls(AVCodecContext *ctx)