From patchwork Tue Jul 16 17:11:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 50588 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:40f5:b0:482:c625:d099 with SMTP id lb53csp468003vqb; Tue, 16 Jul 2024 11:17:08 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUC0zEup2kfqg4Y1AR6YghnO0TRZJv9reFF8Cgnaff9UXGczKrcJgi/9T88cU4Rh5+KcXbrmkS1ULoRecgeX6khZ6Yxwwgbl/vxfQ== X-Google-Smtp-Source: AGHT+IF/pBXDq9uZFBvCayeas1CQ2RzbmAQWiOe7nSVleB0EGvV+Axz/FmS5Z9FxLkIrqX7kNvvm X-Received: by 2002:a05:6512:1149:b0:52e:be1f:bf84 with SMTP id 2adb3069b0e04-52edf0345e4mr1921978e87.53.1721153828647; Tue, 16 Jul 2024 11:17:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721153828; cv=none; d=google.com; s=arc-20160816; b=arxngNymgYavVsCJPBunucMURN7d6ZhdFdVgpIGnA5VbN9BgfHAHND/+OKtsTreUvE K8DAaXGBlNFioDjREhGbW/KIpEsKIDrFfGeZN/PcpwFTLKivfX5UfNCA78JRNk8CorbR 6mRPELIyJHsE9/R0WuBUvPBaSuZveweZ7XB7J3JIVlQgBLJryoVAxblFQFszE/xGuXeX KDogYnBM8vy+pxA0OgnerJLg59i7wiuUHx5ginJFGhr5D7HvjX8V4cj12Hb2Y/ltdt3K X+BuLz0SMFW/rXuaeuL5gOqePAQdmASJnqLeRT0kLIfKjOhmuCtfvctt62vQgThGUiDC ZczA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=jUk0/E04RWggASDyNZNOZMuRQ95mUuzdD2PlWeeTYJU=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=Gs6QXrBAt/EWovf9nBrKrdD0ubOljPii4yp/sCg9o/TZvN+AYpnZRYU5MJTDDMIn/W EXClFVxzU3eW7xxFOHz8jcbjpmaGU6GXo3LX6mjYHo/gaZXDKRiDNTxgd1CxL9dDobyK SHcQgi6lMIu3khDg1n3LaF0x4RO6vDIaDzhes02Tfq50JttNvRCzalwaj2CQPIUnjPw2 9j2qJ3XfkvPzJJyVgoR0+xFasWBanKBKNhUx2TqI2s3Je7718gKpgX6XRbsSbu8fbmK+ AdXlXaJeVH0VhQuADu3MJdM7pqaDoql3y3fgJg7rsG958nTlAOqo/wjX/FUyiTSmBvEZ kXng==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@khirnov.net header.s=mail header.b=PZHnSeoz; 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 2adb3069b0e04-52ed255c1a6si2344595e87.320.2024.07.16.11.17.08; Tue, 16 Jul 2024 11:17:08 -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=@khirnov.net header.s=mail header.b=PZHnSeoz; 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 D577268DBE1; Tue, 16 Jul 2024 21:14:32 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C310668DAB9 for ; Tue, 16 Jul 2024 21:14:04 +0300 (EEST) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=PZHnSeoz; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 3F0234E06 for ; Tue, 16 Jul 2024 19:16:35 +0200 (CEST) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id 9zLWPTOopYcP for ; Tue, 16 Jul 2024 19:16:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1721150188; bh=1Y3CMGcvJ29d0oKadwkFfgja0lbkLfY4mjlYCwMs+p8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=PZHnSeozCEy4UleZ3hOmDMTR56zyZI41fF30g2b0cEK5nJnKKdxjVKTwss1H6HWNp qMKdEYI9qWQK/3NdYxxb8kutKV92it0ieiVocY8qmrZIdwyvJY0rkgqajtkEI2TsN6 /2TY3hAAHHSyHPdFYBVEYSpJ5iH5h3I1xG5LmniXDPwZ4xCxt61RstrbWNl/jdEp04 HBPn249xJVFlqCKoMcxKrsAZ2mmIt7LZGY0xuKjPzfPfxxJYcIXKXD0DhkC0hqH8fd mMAFg71z1L74plTBA46EVkpbJih25XkvsKFpJIAtKO2NJDDx91SJpXs4cz2+Zl6bNa h5zJds4kfF8rw== Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail1.khirnov.net (Postfix) with ESMTPS id 6B57A4DEC for ; Tue, 16 Jul 2024 19:16:28 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 9AEC63A1EA0 for ; Tue, 16 Jul 2024 19:16:20 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 16 Jul 2024 19:11:25 +0200 Message-ID: <20240716171155.31838-10-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240716171155.31838-1-anton@khirnov.net> References: <20240716171155.31838-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/39] lavc/ffv1dec: move the bitreader to stack 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: KHFf4DftUHS8 There is no reason to place it in persistent state. --- libavcodec/ffv1.h | 1 - libavcodec/ffv1dec.c | 28 +++++++++++++++------------- libavcodec/ffv1dec_template.c | 23 ++++++++++++----------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h index 68d13a2964..c88aa8c30d 100644 --- a/libavcodec/ffv1.h +++ b/libavcodec/ffv1.h @@ -85,7 +85,6 @@ typedef struct FFV1Context { AVClass *class; AVCodecContext *avctx; RangeCoder c; - GetBitContext gb; PutBitContext pb; uint64_t rc_stat[256][2]; uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c index a2971d7eea..a1f7206871 100644 --- a/libavcodec/ffv1dec.c +++ b/libavcodec/ffv1dec.c @@ -94,14 +94,14 @@ static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state, return ret; } -static int is_input_end(FFV1Context *s) +static int is_input_end(FFV1Context *s, GetBitContext *gb) { if (s->ac != AC_GOLOMB_RICE) { RangeCoder *const c = &s->c; if (c->overread > MAX_OVERREAD) return AVERROR_INVALIDDATA; } else { - if (get_bits_left(&s->gb) < 1) + if (get_bits_left(gb) < 1) return AVERROR_INVALIDDATA; } return 0; @@ -118,6 +118,7 @@ static int is_input_end(FFV1Context *s) #include "ffv1dec_template.c" static int decode_plane(FFV1Context *s, FFV1SliceContext *sc, + GetBitContext *gb, uint8_t *src, int w, int h, int stride, int plane_index, int pixel_stride) { @@ -140,13 +141,13 @@ static int decode_plane(FFV1Context *s, FFV1SliceContext *sc, sample[0][w] = sample[0][w - 1]; if (s->avctx->bits_per_raw_sample <= 8) { - int ret = decode_line(s, sc, w, sample, plane_index, 8); + int ret = decode_line(s, sc, gb, w, sample, plane_index, 8); if (ret < 0) return ret; for (x = 0; x < w; x++) src[x*pixel_stride + stride * y] = sample[1][x]; } else { - int ret = decode_line(s, sc, w, sample, plane_index, s->avctx->bits_per_raw_sample); + int ret = decode_line(s, sc, gb, w, sample, plane_index, s->avctx->bits_per_raw_sample); if (ret < 0) return ret; if (s->packed_at_lsb) { @@ -262,6 +263,7 @@ static int decode_slice(AVCodecContext *c, void *arg) AVFrame * const p = f->picture.f; const int si = (FFV1Context**)arg - f->slice_context; FFV1SliceContext *sc = &f->slices[si]; + GetBitContext gb; if (f->fsrc && !(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f) ff_progress_frame_await(&f->last_picture, si); @@ -322,7 +324,7 @@ static int decode_slice(AVCodecContext *c, void *arg) if (f->version == 3 && f->micro_version > 1 || f->version > 3) get_rac(&fs->c, (uint8_t[]) { 129 }); fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0; - init_get_bits(&fs->gb, + init_get_bits(&gb, fs->c.bytestream_start + fs->ac_byte_count, (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8); } @@ -333,29 +335,29 @@ static int decode_slice(AVCodecContext *c, void *arg) const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift); const int cx = x >> f->chroma_h_shift; const int cy = y >> f->chroma_v_shift; - decode_plane(fs, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1); + decode_plane(fs, sc, &gb, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1); if (f->chroma_planes) { - decode_plane(fs, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1); - decode_plane(fs, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1); + decode_plane(fs, sc, &gb, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1); + decode_plane(fs, sc, &gb, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1); } if (fs->transparency) - decode_plane(fs, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2, 1); + decode_plane(fs, sc, &gb, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2, 1); } else if (f->colorspace == 0) { - decode_plane(fs, sc, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2); - decode_plane(fs, sc, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2); + decode_plane(fs, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2); + decode_plane(fs, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2); } else if (f->use32bit) { uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0], p->data[1] + ps * x + y * p->linesize[1], p->data[2] + ps * x + y * p->linesize[2], p->data[3] + ps * x + y * p->linesize[3] }; - decode_rgb_frame32(fs, sc, planes, width, height, p->linesize); + decode_rgb_frame32(fs, sc, &gb, planes, width, height, p->linesize); } else { uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0], p->data[1] + ps * x + y * p->linesize[1], p->data[2] + ps * x + y * p->linesize[2], p->data[3] + ps * x + y * p->linesize[3] }; - decode_rgb_frame(fs, sc, planes, width, height, p->linesize); + decode_rgb_frame(fs, sc, &gb, planes, width, height, p->linesize); } if (fs->ac != AC_GOLOMB_RICE && f->version > 2) { int v; diff --git a/libavcodec/ffv1dec_template.c b/libavcodec/ffv1dec_template.c index 8e2e38c0b9..e9d3002be9 100644 --- a/libavcodec/ffv1dec_template.c +++ b/libavcodec/ffv1dec_template.c @@ -23,8 +23,8 @@ #include "ffv1_template.c" static av_always_inline int -RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w, - TYPE *sample[2], int plane_index, int bits) +RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, GetBitContext *gb, + int w, TYPE *sample[2], int plane_index, int bits) { PlaneContext *const p = &s->plane[plane_index]; RangeCoder *const c = &s->c; @@ -33,7 +33,7 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w, int run_mode = 0; int run_index = sc->run_index; - if (is_input_end(s)) + if (is_input_end(s, gb)) return AVERROR_INVALIDDATA; if (s->slice_coding_mode == 1) { @@ -53,7 +53,7 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w, int diff, context, sign; if (!(x & 1023)) { - if (is_input_end(s)) + if (is_input_end(s, gb)) return AVERROR_INVALIDDATA; } @@ -74,13 +74,13 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w, if (run_mode) { if (run_count == 0 && run_mode == 1) { - if (get_bits1(&s->gb)) { + if (get_bits1(gb)) { run_count = 1 << ff_log2_run[run_index]; if (x + run_count <= w) run_index++; } else { if (ff_log2_run[run_index]) - run_count = get_bits(&s->gb, ff_log2_run[run_index]); + run_count = get_bits(gb, ff_log2_run[run_index]); else run_count = 0; if (run_index) @@ -105,17 +105,17 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w, if (run_count < 0) { run_mode = 0; run_count = 0; - diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], + diff = get_vlc_symbol(gb, &p->vlc_state[context], bits); if (diff >= 0) diff++; } else diff = 0; } else - diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits); + diff = get_vlc_symbol(gb, &p->vlc_state[context], bits); ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n", - run_count, run_index, run_mode, x, get_bits_count(&s->gb)); + run_count, run_index, run_mode, x, get_bits_count(gb)); } if (sign) @@ -128,6 +128,7 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w, } static int RENAME(decode_rgb_frame)(FFV1Context *s, FFV1SliceContext *sc, + GetBitContext *gb, uint8_t *src[4], int w, int h, int stride[4]) { int x, y, p; @@ -157,9 +158,9 @@ static int RENAME(decode_rgb_frame)(FFV1Context *s, FFV1SliceContext *sc, sample[p][1][-1]= sample[p][0][0 ]; sample[p][0][ w]= sample[p][0][w-1]; if (lbd && s->slice_coding_mode == 0) - ret = RENAME(decode_line)(s, sc, w, sample[p], (p + 1)/2, 9); + ret = RENAME(decode_line)(s, sc, gb, w, sample[p], (p + 1)/2, 9); else - ret = RENAME(decode_line)(s, sc, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1)); + ret = RENAME(decode_line)(s, sc, gb, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1)); if (ret < 0) return ret; }