From patchwork Wed Sep 13 21:19:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 43724 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:9307:b0:149:dfde:5c0a with SMTP id r7csp705316pzh; Wed, 13 Sep 2023 14:20:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE225Cix8tSrqCTjfyrtYYzKJMw9cRiPqdq/Cb3cpKrEf0zb7jgXIuWZ/VF1mZGLubD4b5Q X-Received: by 2002:a17:906:8444:b0:9a1:c352:b6a5 with SMTP id e4-20020a170906844400b009a1c352b6a5mr2954140ejy.69.1694640011861; Wed, 13 Sep 2023 14:20:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694640011; cv=none; d=google.com; s=arc-20160816; b=kA3+J0zzjksgNGYPQeuzONTWPnw95YXfq5ctumiQGDxHrmfwiDYwOcxkg+6jN6NzRr ZTcwCfQZrfRQjPfs8h15zUngVFbpLpkK0wJSnQeoZ1B76vsOwMpaYqfTwNieK5P1jHj0 zCVVN5PmkmFrVzXZDJeiCV23dOf9DhGBjE/r/Kl4nYqa6w+87CjEyJUB7QCAALzupWA2 lwyINYwlkncyWwV9DktvVbVHYUq14Hy/rlmNrigzWhUpNeThr5LPYjYkuRQEWl6uvpVb /GMpggywxH2l7H4JdrATiUGJ0n5ihIHqZ5ZwS8Vwzl/8k1JMmVM0XmVq+z7jGZ16I0/l FdxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:mime-version:dkim-signature:delivered-to; bh=Pv2oYKttqbd/YgDxAR2JfjvHQZF9kkrezaB7e9cRLC4=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=mq+ASR3addi/c1/T33yfovvjlouDRu1zZYAj9T/voKUVFbmAMgwHy5Nydqp6TALWU2 7q3BiIj2AlAZSbQcZWV5iHY9VZvd69t6bVWPBP1HJkwBpMQGh4FU7tSesRuNRnyVEgSF JgsVkT5BYx+aaO97CNY+FjmXdKQjhuOLg/KruIg77Q/6E3rFQbqZBlzFbWJPxmqmVIvM 7UkJsbvqb9GP6OmwUk2r8rlvO3XGroEOjDQ6DT8gF5xRhmCetysG7zxlxKdQFCC4mpPg 6DI5so3FDqAZfV7eST6drqTDS4mxklD2AEFgjS1F043vt1O1pPvfmqBjwbyfBtmqVAKP gPeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=dBZFfTgT; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id lc27-20020a170906dffb00b00992bcbf775asi10854876ejc.940.2023.09.13.14.19.48; Wed, 13 Sep 2023 14:20:11 -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=@gmail.com header.s=20221208 header.b=dBZFfTgT; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C05A068C9D5; Thu, 14 Sep 2023 00:19:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ua1-f45.google.com (mail-ua1-f45.google.com [209.85.222.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0208D68C9A1 for ; Thu, 14 Sep 2023 00:19:37 +0300 (EEST) Received: by mail-ua1-f45.google.com with SMTP id a1e0cc1a2514c-7a754db0fbcso134567241.2 for ; Wed, 13 Sep 2023 14:19:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694639976; x=1695244776; darn=ffmpeg.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=p4kUzyEpdf+OkeGV7+0TCGUdJS9lBD4T8ma+WtMUuOQ=; b=dBZFfTgT+6aGblb5oRoIAfxZq907131mRLWEkNCIYuJdhgbnyHBE3b1meObo15Cig1 lUMnhPLHb3ZjMA4LYEdqsgKF09c10ODmpQN4BXrD9H/MULi6TFedBWrVI3k9Pat5/4Tu KOvKyqY2vtkQi5tg1pGk7U5mWfpWYYGYY+RmfV5+G3IlgP3g0RQWK7dEABPWml0VTgUM Cbnk0WmiA9GISvjjaZJP/k9EQqXb4EzJoUS54OVj2xEwLwUTYB1/DMCQDmhSigYNPl/J QenP3WXl+idzLWEn9wUls9EwWGhRIQroRvY1fT30NCsWqDGFIVoDVG0eG3f7zXhNkRiA t6dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694639976; x=1695244776; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=p4kUzyEpdf+OkeGV7+0TCGUdJS9lBD4T8ma+WtMUuOQ=; b=hGlj32/1FapMmaS+EMlfZ5DxXKNuioXevDUILfPzwjw8kLBz6wvCYcyWlXAJoHL9t7 JrXzxMKz914NpMzI4npcLPO+7beHrla7pyK0K82XgnFdnnkN427CEGD3yhy2DHLnRJw2 ea0yYyjkZkFhoHXCC6pmmt30dy1jwdHjoFspfYzCAlOMrIUg252SWHNJKMeqkoH8b8r5 EEVzPO7c7S0g0vXsabBUktEBLJxvqulLxHaiAp3KbF3nmaWWs+qLXLwKP7ptcdVGVzJl PlKDK/f2B9vCkYdXZh6wcdssAuDwW3fps6wmS8aYlvPIJviv9+FdXPP243MoXAz+ZnCq yRfw== X-Gm-Message-State: AOJu0Yzl3aJZv6VdA0/rs+X9mMtq4oJWx+hn0oYQ8H8I3P5Si6aRzLEh qniWM47gHsPJ685GYY8hVpSUjtspf8VDnSeLdbfo1TUHpo8= X-Received: by 2002:a1f:c6c6:0:b0:487:e8f1:a2bf with SMTP id w189-20020a1fc6c6000000b00487e8f1a2bfmr3288135vkf.15.1694639976168; Wed, 13 Sep 2023 14:19:36 -0700 (PDT) MIME-Version: 1.0 From: Paul B Mahol Date: Wed, 13 Sep 2023 23:19:23 +0200 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.29 Subject: [FFmpeg-devel] [PATCH] avcodec/lagarith: use VLC for probe code length 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: MTE6XtB96Kuq Patch Attached. From cd774229dcf32cb44455031663b4357f0597b4c0 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Tue, 12 Sep 2023 01:29:55 +0200 Subject: [PATCH] avcodec/lagarith: use VLC for prob code length Signed-off-by: Paul B Mahol --- libavcodec/lagarith.c | 57 ++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/libavcodec/lagarith.c b/libavcodec/lagarith.c index ebc1f7613a..31cad907c0 100644 --- a/libavcodec/lagarith.c +++ b/libavcodec/lagarith.c @@ -27,6 +27,9 @@ #include +#include "libavutil/mem_internal.h" +#include "libavutil/thread.h" + #include "avcodec.h" #include "codec_internal.h" #include "get_bits.h" @@ -35,6 +38,8 @@ #include "lossless_videodsp.h" #include "thread.h" +#define VLC_BITS 11 + enum LagarithFrameType { FRAME_RAW = 1, /**< uncompressed */ FRAME_U_RGB24 = 2, /**< unaligned RGB24 */ @@ -56,6 +61,35 @@ typedef struct LagarithContext { int zeros_rem; /**< number of zero bytes remaining to output */ } LagarithContext; +static VLC lag_tab; + +static const uint8_t lag_bits[] = { + 7, 7, 7, 2, 7, 3, 4, 5, 6, 7, 7, 7, 7, 7, 6, 7, 4, 5, 7, 7, 7, 7, + 5, 6, 7, 7, 7, 7, 7, 7, 6, 7, 7, 7, 7, 7, 6, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, +}; + +static const uint8_t lag_codes[] = { + 0x00, 0x01, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x05, + 0x08, 0x09, 0x0A, 0x0B, 0x0B, 0x0B, 0x0B, 0x10, 0x11, 0x12, 0x13, + 0x13, 0x13, 0x14, 0x15, 0x20, 0x21, 0x22, 0x23, 0x23, 0x24, 0x25, + 0x28, 0x29, 0x2A, 0x2B, 0x2B, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, + 0x48, 0x49, 0x4A, 0x4B, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, +}; + +static const uint8_t lag_symbols[] = { + -1, 20, 12, 0, 12, 1, 2, 4, 7, 7, 28, 4, 25, 17, + 10, 17, 3, 6, 2, 23, 15, 15, 5, 9, 10, 31, 1, 22, + 14, 14, 8, 9, 30, 6, 27, 19, 11, 19, 0, 21, 13, 13, + 8, 29, 5, 26, 18, 18, 3, 24, 16, 16, 11, 32, +}; + +static av_cold void lag_init_static_data(void) +{ + VLC_INIT_SPARSE_STATIC(&lag_tab, VLC_BITS, FF_ARRAY_ELEMS(lag_bits), + lag_bits, 1, 1, lag_codes, 1, 1, lag_symbols, 1, 1, 2048); +} + /** * Compute the 52-bit mantissa of 1/(double)denom. * This crazy format uses floats in an entropy coder and we have to match x86 @@ -101,23 +135,10 @@ static uint8_t lag_calc_zero_run(int8_t x) static int lag_decode_prob(GetBitContext *gb, uint32_t *value) { - static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 }; - int i; - int bit = 0; - int bits = 0; - int prevbit = 0; - unsigned val; - - for (i = 0; i < 7; i++) { - if (prevbit && bit) - break; - prevbit = bit; - bit = get_bits1(gb); - if (bit && !prevbit) - bits += series[i]; - } - bits--; - if (bits < 0 || bits > 31) { + unsigned val, bits; + + bits = get_vlc2(gb, lag_tab.table, VLC_BITS, 3); + if (bits > 31) { *value = 0; return AVERROR_INVALIDDATA; } else if (bits == 0) { @@ -720,10 +741,12 @@ static int lag_decode_frame(AVCodecContext *avctx, AVFrame *p, static av_cold int lag_decode_init(AVCodecContext *avctx) { + static AVOnce init_static_once = AV_ONCE_INIT; LagarithContext *l = avctx->priv_data; l->avctx = avctx; ff_llviddsp_init(&l->llviddsp); + ff_thread_once(&init_static_once, lag_init_static_data); return 0; } -- 2.39.1