From patchwork Tue Apr 14 10:24:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Gisquet X-Patchwork-Id: 18945 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 0C04444B934 for ; Tue, 14 Apr 2020 13:25:19 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id ECEC668B597; Tue, 14 Apr 2020 13:25:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7086768B532 for ; Tue, 14 Apr 2020 13:25:11 +0300 (EEST) Received: by mail-wr1-f46.google.com with SMTP id d17so6766829wrg.11 for ; Tue, 14 Apr 2020 03:25:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=9vqZUEs5E6IxvsBeWDwhx7RhUawgEgJks4GhsB4gEr8=; b=IVHbTJbd/4lY0khDx905maAaVlo+MyZ6prCVL5QPcCh9B64WqQekQ+gq57rNI8czTv jgpSn/jvgFn8Ve7SbQe4mA2TzQqQmWdxpwxJsby4UdsfAO8VSa8d5unzdwL2pIxgI9Am zK/a66AjoadNEFfh/bJUkmaJ9RmtZgkUqb99ARRaXV1Db3SW2VMEEj9bqE4ma7zCwOWd zC7ST12XrKTI0Ydz8NHorSvhMkyuJo4mtGYvCeU+SROqWmpgl44rOsKgTpNFKFdRWJPP CZsCMK3xx4x+w+RaMR65z3GW/fHxKiox+uPjZQ9ZBXSPXNSAyRTvkTsTNuf1zzQgEzbj sbeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9vqZUEs5E6IxvsBeWDwhx7RhUawgEgJks4GhsB4gEr8=; b=IKRX/fgYfPa+e55X+YeDaYK3qfxYUTy6UtjDymu+h/OOHygJUFNpIqe/6opfLCbw28 iq7tjLL1fbp+L64T5HzoVb7vvR1MHdM5nhiPderQScZQnC16K297GeZd9rU1aRnCroaS bgmdQ+nGU/SjkwFpuTkBCp206vioGuASObt5YPeZ4JUnU8aegKRj7agp/fO59A86vsFV RZP49P1wcUd8cXZCJE5YAALgeb/50p+9oN4jdhyqBd3Yn88BMdH6U+xYwRX2c85ZpRK2 04NRxd4DriaO5N5yoy2o/HU4RCOzHSWjB14Iqap8bLUTKIIDIv1YeetJcj3Z83ZtHHxq FHzQ== X-Gm-Message-State: AGi0PuY/t0RIucfIEnqGbArImbHe0KCmBe4vsOyjnsdB2GTueqFg4Sd9 vboZNFOYKkHFuwzXQcrYyEmWQ4A= X-Google-Smtp-Source: APiQypJPhKTPy+nTpJtjpxUmv8ToOaVmRnZy0eK7ptBDeqN2lhekp9t37GgcE+qLg8wt7yDu11+wpg== X-Received: by 2002:a05:6000:1008:: with SMTP id a8mr17209755wrx.189.1586859910702; Tue, 14 Apr 2020 03:25:10 -0700 (PDT) Received: from localhost.localdomain (2a01cb0881b29e00157b082460d14b2a.ipv6.abo.wanadoo.fr. [2a01:cb08:81b2:9e00:157b:824:60d1:4b2a]) by smtp.gmail.com with ESMTPSA id i17sm8574487wru.39.2020.04.14.03.25.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 03:25:10 -0700 (PDT) From: Christophe Gisquet To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 Apr 2020 10:24:57 +0000 Message-Id: <20200414102503.7858-2-christophe.gisquet@gmail.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200414102503.7858-1-christophe.gisquet@gmail.com> References: <20200414102503.7858-1-christophe.gisquet@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/7] fate: add a MVHA test 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- tests/fate/video.mak | 3 +++ tests/ref/fate/mvha | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 tests/ref/fate/mvha diff --git a/tests/fate/video.mak b/tests/fate/video.mak index d2d43e518d..8e54718c16 100644 --- a/tests/fate/video.mak +++ b/tests/fate/video.mak @@ -364,6 +364,9 @@ fate-xxan-wc4: CMD = framecrc -i $(TARGET_SAMPLES)/wc4-xan/wc4trailer-partial.av FATE_VIDEO-$(call DEMDEC, WAV, SMVJPEG) += fate-smvjpeg fate-smvjpeg: CMD = framecrc -idct simple -flags +bitexact -i $(TARGET_SAMPLES)/smv/clock.smv -an +FATE_VIDEO-$(call DEMDEC, AVI, MVHA) += fate-mvha +fate-mvha: CMD = framecrc -i $(TARGET_SAMPLES)/mvha/mvha.avi -an + FATE_VIDEO += $(FATE_VIDEO-yes) FATE_SAMPLES_FFMPEG += $(FATE_VIDEO) diff --git a/tests/ref/fate/mvha b/tests/ref/fate/mvha new file mode 100644 index 0000000000..3635f97730 --- /dev/null +++ b/tests/ref/fate/mvha @@ -0,0 +1,6 @@ +#tb 0: 1001/30000 +#media_type 0: video +#codec_id 0: rawvideo +#dimensions 0: 640x480 +#sar 0: 0/1 +0, 0, 0, 1, 614400, 0xff8fb84b From patchwork Tue Apr 14 10:24:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Gisquet X-Patchwork-Id: 18946 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 C47B244B934 for ; Tue, 14 Apr 2020 13:25:20 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B0CDD68B682; Tue, 14 Apr 2020 13:25:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0F1F068B532 for ; Tue, 14 Apr 2020 13:25:12 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id a201so12958267wme.1 for ; Tue, 14 Apr 2020 03:25:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=N8Mr7GGO6E3/0WWy/eas5ZfNBBfLjwZHijCW90H92cY=; b=T4ryIFnzHmFuF5EG3Gi2KNye9M3320xZb9qdWIeMxfEmRsQs5pSpbZXUr/ffZEo01G gzc9fh18cqfO7FSMvXpz6EtzUPsKnO2Br5rBYhrE5y3hABNEceyyk+YylRqwxPJnQeWt k9y61rpsVv+6QWbVNyxxwwLJtGqClxOp14/qo1ZrljMbdTD9yuZWCBCNnrLq+A8vHMKb l9/iq4VD4SIU/UyFeN0t5TOMfpgiUJagBsOulit5PfSa85dmK0BGaJFmGHprWNoh+B/C kyEdZ7T0j5VHNWsQtNiqJP9K1DrC/U7jkmlp7BjYfPKizWfT/nGj6G6dtule+4kSpIlY gVQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N8Mr7GGO6E3/0WWy/eas5ZfNBBfLjwZHijCW90H92cY=; b=Hjms4Nbuv5wbVup2Jb6I22Gx+tERgZw34OK5UwpkbcejFQDMjmLjmL+U1Ax6ik+ecv K9DTnr1gYIpWpV1j6w/xpYoLiocOdW74SaD2RMXyQEZeMTuUgByOJiBFT0vwkLmo+058 JjnVhDi033F38V1ANJp9BYo2FIQeaIgNDOEyA4DYJNBnFEYZ+XccN+awzqAzvg6wgJ/9 7rDX/7WhiUlRhgbLMLzxOV0YLAztRm0I6KBzxEPu6Kmamlteb5K2JOGQnp4ifsAelqiJ L4RtxzHRoHxsc2iyKiB0o6Ft4yWgEq3iXQz4WNA8DCJ29NfpzOSIXUjtSVvdAFKSpJWM BgMA== X-Gm-Message-State: AGi0PubV5QXKIZybswdnHzSSFkE+0WwrB9FdE6rZeChU123/4lSvkizg bj0np3btJraymwN8VT50yNr0XYc= X-Google-Smtp-Source: APiQypLrPBf5w7vsRTal2MWSkE8+oyxWrNBlenH0CWR7RkXx7ygSUh67MuDAh2uXmR2/0E/uD24JUQ== X-Received: by 2002:a1c:7ed7:: with SMTP id z206mr21522288wmc.64.1586859911232; Tue, 14 Apr 2020 03:25:11 -0700 (PDT) Received: from localhost.localdomain (2a01cb0881b29e00157b082460d14b2a.ipv6.abo.wanadoo.fr. [2a01:cb08:81b2:9e00:157b:824:60d1:4b2a]) by smtp.gmail.com with ESMTPSA id i17sm8574487wru.39.2020.04.14.03.25.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 03:25:10 -0700 (PDT) From: Christophe Gisquet To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 Apr 2020 10:24:58 +0000 Message-Id: <20200414102503.7858-3-christophe.gisquet@gmail.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200414102503.7858-1-christophe.gisquet@gmail.com> References: <20200414102503.7858-1-christophe.gisquet@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/7] get_bits: support 32bits cache 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Therefore, also activate it under ARCH_X86 (testing for more archs welcome) for the only codecs supporting said cache reader. For UTVideo, on 8 bits samples and ARCH_X86_32 (X86_64 being unaffected), timings for one line do ~19.4k -> 15.1k and 16.5k (roughly 17% speedup). --- libavcodec/get_bits.h | 110 ++++++++++++++++++++++++++++------------ libavcodec/mvha.c | 2 +- libavcodec/utvideodec.c | 2 +- 3 files changed, 80 insertions(+), 34 deletions(-) diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index 66fb877599..cb4df98e54 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -58,10 +58,40 @@ #define CACHED_BITSTREAM_READER 0 #endif +#if CACHED_BITSTREAM_READER + +# ifndef BITSTREAM_BITS +# if HAVE_FAST_64BIT || defined(LONG_BITSTREAM_READER) +# define BITSTREAM_BITS 64 +# else +# define BITSTREAM_BITS 32 +# endif +# endif + +# if BITSTREAM_BITS == 64 +# define BITSTREAM_HBITS 32 +typedef uint64_t cache_type; +# define AV_RB_ALL AV_RB64 +# define AV_RL_ALL AV_RL64 +# define AV_RB_HALF AV_RB32 +# define AV_RL_HALF AV_RL32 +# define CACHE_TYPE(a) UINT64_C(a) +# else +# define BITSTREAM_HBITS 16 +typedef uint32_t cache_type; +# define AV_RB_ALL AV_RB32 +# define AV_RL_ALL AV_RL32 +# define AV_RB_HALF AV_RB16 +# define AV_RL_HALF AV_RL16 +# define CACHE_TYPE(a) UINT32_C(a) +#endif + +#endif + typedef struct GetBitContext { const uint8_t *buffer, *buffer_end; #if CACHED_BITSTREAM_READER - uint64_t cache; + cache_type cache; unsigned bits_left; #endif int index; @@ -121,7 +151,11 @@ static inline unsigned int show_bits(GetBitContext *s, int n); */ #if CACHED_BITSTREAM_READER -# define MIN_CACHE_BITS 64 +# if BITSTREAM_BITS == 32 +# define MIN_CACHE_BITS (32-7) +# else +# define MIN_CACHE_BITS 32 +# endif #elif defined LONG_BITSTREAM_READER # define MIN_CACHE_BITS 32 #else @@ -226,22 +260,34 @@ static inline int get_bits_count(const GetBitContext *s) } #if CACHED_BITSTREAM_READER -static inline void refill_32(GetBitContext *s, int is_le) +static inline void refill_half(GetBitContext *s, int is_le) { #if !UNCHECKED_BITSTREAM_READER if (s->index >> 3 >= s->buffer_end - s->buffer) return; #endif +#if BITSTREAM_BITS == 32 + if (s->bits_left > 16) { + if (is_le) + s->cache |= (uint32_t)s->buffer[s->index >> 3] << s->bits_left; + else + s->cache |= (uint32_t)s->buffer[s->index >> 3] << (32 - s->bits_left); + s->index += 8; + s->bits_left += 8; + return; + } +#endif + if (is_le) - s->cache = (uint64_t)AV_RL32(s->buffer + (s->index >> 3)) << s->bits_left | s->cache; + s->cache |= (cache_type)AV_RL_HALF(s->buffer + (s->index >> 3)) << s->bits_left; else - s->cache = s->cache | (uint64_t)AV_RB32(s->buffer + (s->index >> 3)) << (32 - s->bits_left); - s->index += 32; - s->bits_left += 32; + s->cache |= (cache_type)AV_RB_HALF(s->buffer + (s->index >> 3)) << (BITSTREAM_HBITS - s->bits_left); + s->index += BITSTREAM_HBITS; + s->bits_left += BITSTREAM_HBITS; } -static inline void refill_64(GetBitContext *s, int is_le) +static inline void refill_all(GetBitContext *s, int is_le) { #if !UNCHECKED_BITSTREAM_READER if (s->index >> 3 >= s->buffer_end - s->buffer) @@ -249,22 +295,22 @@ static inline void refill_64(GetBitContext *s, int is_le) #endif if (is_le) - s->cache = AV_RL64(s->buffer + (s->index >> 3)); + s->cache = AV_RL_ALL(s->buffer + (s->index >> 3)); else - s->cache = AV_RB64(s->buffer + (s->index >> 3)); - s->index += 64; - s->bits_left = 64; + s->cache = AV_RB_ALL(s->buffer + (s->index >> 3)); + s->index += BITSTREAM_BITS; + s->bits_left = BITSTREAM_BITS; } -static inline uint64_t get_val(GetBitContext *s, unsigned n, int is_le) +static inline cache_type get_val(GetBitContext *s, unsigned n, int is_le) { - uint64_t ret; + cache_type ret; av_assert2(n>0 && n<=63); if (is_le) { - ret = s->cache & ((UINT64_C(1) << n) - 1); + ret = s->cache & ((CACHE_TYPE(1) << n) - 1); s->cache >>= n; } else { - ret = s->cache >> (64 - n); + ret = s->cache >> (BITSTREAM_BITS - n); s->cache <<= n; } s->bits_left -= n; @@ -274,12 +320,12 @@ static inline uint64_t get_val(GetBitContext *s, unsigned n, int is_le) static inline unsigned show_val(const GetBitContext *s, unsigned n) { #ifdef BITSTREAM_READER_LE - return s->cache & ((UINT64_C(1) << n) - 1); + return s->cache & ((CACHE_TYPE(1) << n) - 1); #else - return s->cache >> (64 - n); + return s->cache >> (BITSTREAM_BITS - n); #endif } -#endif +#endif // ~CACHED_BITSTREAM_READER /** * Skips the specified number of bits. @@ -384,11 +430,11 @@ static inline unsigned int get_bits(GetBitContext *s, int n) av_assert2(n>0 && n<=32); if (n > s->bits_left) { #ifdef BITSTREAM_READER_LE - refill_32(s, 1); + refill_half(s, 1); #else - refill_32(s, 0); + refill_half(s, 0); #endif - if (s->bits_left < 32) + if (s->bits_left < BITSTREAM_HBITS) s->bits_left = n; } @@ -422,8 +468,8 @@ static inline unsigned int get_bits_le(GetBitContext *s, int n) #if CACHED_BITSTREAM_READER av_assert2(n>0 && n<=32); if (n > s->bits_left) { - refill_32(s, 1); - if (s->bits_left < 32) + refill_half(s, 1); + if (s->bits_left < BITSTREAM_HBITS) s->bits_left = n; } @@ -449,9 +495,9 @@ static inline unsigned int show_bits(GetBitContext *s, int n) #if CACHED_BITSTREAM_READER if (n > s->bits_left) #ifdef BITSTREAM_READER_LE - refill_32(s, 1); + refill_half(s, 1); #else - refill_32(s, 0); + refill_half(s, 0); #endif tmp = show_val(s, n); @@ -474,16 +520,16 @@ static inline void skip_bits(GetBitContext *s, int n) s->cache = 0; s->bits_left = 0; - if (n >= 64) { + if (n >= BITSTREAM_BITS) { unsigned skip = (n / 8) * 8; n -= skip; s->index += skip; } #ifdef BITSTREAM_READER_LE - refill_64(s, 1); + refill_all(s, 1); #else - refill_64(s, 0); + refill_all(s, 0); #endif if (n) skip_remaining(s, n); @@ -500,9 +546,9 @@ static inline unsigned int get_bits1(GetBitContext *s) #if CACHED_BITSTREAM_READER if (!s->bits_left) #ifdef BITSTREAM_READER_LE - refill_64(s, 1); + refill_all(s, 1); #else - refill_64(s, 0); + refill_all(s, 0); #endif #ifdef BITSTREAM_READER_LE @@ -642,7 +688,7 @@ static inline int init_get_bits_xe(GetBitContext *s, const uint8_t *buffer, #if CACHED_BITSTREAM_READER s->cache = 0; s->bits_left = 0; - refill_64(s, is_le); + refill_all(s, is_le); #endif return ret; diff --git a/libavcodec/mvha.c b/libavcodec/mvha.c index c603ef6975..9d6dd9352a 100644 --- a/libavcodec/mvha.c +++ b/libavcodec/mvha.c @@ -24,7 +24,7 @@ #include #include -#define CACHED_BITSTREAM_READER !ARCH_X86_32 +#define CACHED_BITSTREAM_READER ARCH_X86 #include "libavutil/intreadwrite.h" #include "avcodec.h" diff --git a/libavcodec/utvideodec.c b/libavcodec/utvideodec.c index 4da257fc61..36dd661e90 100644 --- a/libavcodec/utvideodec.c +++ b/libavcodec/utvideodec.c @@ -27,7 +27,7 @@ #include #include -#define CACHED_BITSTREAM_READER !ARCH_X86_32 +#define CACHED_BITSTREAM_READER ARCH_X86 #define UNCHECKED_BITSTREAM_READER 1 #include "libavutil/intreadwrite.h" From patchwork Tue Apr 14 10:24:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Gisquet X-Patchwork-Id: 18947 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 79E9144B934 for ; Tue, 14 Apr 2020 13:25:21 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 64DBA68B6CA; Tue, 14 Apr 2020 13:25:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C6E8168B5CB for ; Tue, 14 Apr 2020 13:25:12 +0300 (EEST) Received: by mail-wr1-f54.google.com with SMTP id k11so13057742wrp.5 for ; Tue, 14 Apr 2020 03:25:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=cH8NL+BnYClXRk5GoLMWhbi/2e66emoL/v6KEWeZIp8=; b=eZL07vRIIsRUMc27lZkJYS6UwYSIa2r7F2N9grG4O3fdIhHO4rx65fi9HKak/nkDDS hhVnBwiB/Xlnu4T0y3V3y3mCeHvWFKr7ymcJEyHQvw5id0rSuPf7AhW9dq4TBOC0up6f YAaKcGvgeYjEnwRw2WAG4v5NujRexslxPkMyUKhj0tgCaN4kX8ZTFa81Pz6RBVaF8CNd i9WCsvvKcChjQ2oh9vtX88DzKtQxhOGeFGlf9aiJCG0dJrlSv02Rr+Ov8imOsdcBQ8+D 23IJMTmL/3MmNbIRKduMKcXhUseCY2sRIO5TlAySxce6IFlbRWnoAp7Z51Z9rbE0goU3 iE2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cH8NL+BnYClXRk5GoLMWhbi/2e66emoL/v6KEWeZIp8=; b=A9S4y9SsgW6EwJiu5UxexpeUiOkS1j7uRRNaC8Pcw7LCzKsWP35wukOj8TcTsvsSA3 HUvrxWLVKZ7vR7TYyt6UbQRoWhoskMezTOQCk5xpf9Dd300+jSs4VA9svnmU4pkxcwHD yl/3rdNjJd3axVDHfHfFJDdxdYmHglkq/5qHBsi1uI94NORgLosPLNK+ZL5hsXahjuk7 6DnZsZf75Yemk/+AsAhSNLy+9xf8/qttBvA8EHYbzubzGM/GIChcmuY0t3lN6RJZFeNm fwrdsKdcEsaS0Q68/wSHSFkrH5sT3zCSR94vMX9apsfi6ZN5xDawZYC9wljWp5H4escE Q3Dg== X-Gm-Message-State: AGi0PuZzKgD39TO1WCLKo8AF0DYiQQWUnzTwZumrd65tG+7Qsjuv4R4J QvGf3RYT40kah/UImt2R7W5MjxI= X-Google-Smtp-Source: APiQypL7YfLKFCiQO7Iq5eNBfVKD2mUr/Ey+Z16u7L5mj/UZWx7P9bptgQiFZkKgF2FWkWiS3sav4A== X-Received: by 2002:a5d:4d8c:: with SMTP id b12mr22773442wru.35.1586859911712; Tue, 14 Apr 2020 03:25:11 -0700 (PDT) Received: from localhost.localdomain (2a01cb0881b29e00157b082460d14b2a.ipv6.abo.wanadoo.fr. [2a01:cb08:81b2:9e00:157b:824:60d1:4b2a]) by smtp.gmail.com with ESMTPSA id i17sm8574487wru.39.2020.04.14.03.25.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 03:25:11 -0700 (PDT) From: Christophe Gisquet To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 Apr 2020 10:24:59 +0000 Message-Id: <20200414102503.7858-4-christophe.gisquet@gmail.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200414102503.7858-1-christophe.gisquet@gmail.com> References: <20200414102503.7858-1-christophe.gisquet@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/7] get_xbits: request fewer bits 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Also allows it to not break 32bits readers. --- libavcodec/get_bits.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index cb4df98e54..59bfbdd88b 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -367,8 +367,24 @@ static inline void skip_remaining(GetBitContext *s, unsigned n) static inline int get_xbits(GetBitContext *s, int n) { #if CACHED_BITSTREAM_READER - int32_t cache = show_bits(s, 32); - int sign = ~cache >> 31; + int32_t cache; + int sign; + + if (n > s->bits_left) +#ifdef BITSTREAM_READER_LE + refill_half(s, 1); +#else + refill_half(s, 0); +#endif + +#if BITSTREAM_BITS == 32 + cache = s->cache; +#elif defined(BITSTREAM_READER_LE) + cache = s->cache & 0xFFFFFFFF; +#else + cache = s->cache >> 32; +#endif + sign = ~cache >> 31; skip_remaining(s, n); return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign; From patchwork Tue Apr 14 10:25:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Gisquet X-Patchwork-Id: 18948 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 3914444B934 for ; Tue, 14 Apr 2020 13:25:22 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 22BE068B6F3; Tue, 14 Apr 2020 13:25:22 +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 4ED5868B5EE for ; Tue, 14 Apr 2020 13:25:13 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id j2so13679278wrs.9 for ; Tue, 14 Apr 2020 03:25:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=YzbkHM6ecuVXmZ5FvggpUL4h05XGfnli2Vubmqfr+is=; b=XFz33lZJz/5E365DtRldhcE5VjR5tw2XUo8iq3j08M/ob6J8VN8CiT552karmsA1gv y1slf13odVaZBAjkcH/Gw2xeyPg8p2xJRTtRJc1I9sq4cegt+JkF8D2yMrfYaInep/dI 4MsjtDASXHOMcnDtM32wbOgbkP+TTg2sp3T5z4lm9NzGlQWzYEoAaegHkENK+pzto3Qd kbI5E4iXn+7zz03+Nt4Cped3OML3yKUjg2gn+/dANbYiftNZtW13fpbNA5Zi2O6+buzI 3ljW5WYeWuee67X44NVUflviX+gOuuL96UP3B31EU+sBkfT1eIfHYWV0H9EBhrFFjguv +ILQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YzbkHM6ecuVXmZ5FvggpUL4h05XGfnli2Vubmqfr+is=; b=rHFB6iSlgiKELHudtGq4nNZzHWqbDOZhvOPN8W18ihdlgPHytjdcxrKf8OvDZIIW7r z8WWKra8mMtnNyrqVHz03zt9Q168ILEjAMVFPmg7zefXytmlv90uLEhK+wB1ZSP7l3Dj USjHAdPjoDDFVkl+vZOJPBUEzb/2pJqTVL8Cp8ez0pcY9ROnovccVT5rJcZMtOSbJCsv MWrJ/80xicqIMsN0Cj1ank+eNm/yGS0B/AtQTx4tF5+KLJoFuBjBuuoZ65IRMqxn+RuZ G66SOv9eS0Wcmf6//REVjzQmF/2LcmXL9211ge7y9cvoZbKTPZHv4ltmhU8o+xwYx9sU 7Bog== X-Gm-Message-State: AGi0PuaeazGqeY+AB3RLUb4wLZMosdNld+rEi2oUafejsMgFy75269MU aJ9bb4evkhfVIOfD62FdVTu2ONk= X-Google-Smtp-Source: APiQypKk09FFPR1rJoYFC3Zs1oTDgsmZI3dKsnDerk/15lxLzXa6ARCm1/jthTb23YaPFFqncFyEJA== X-Received: by 2002:a5d:6647:: with SMTP id f7mr21785584wrw.41.1586859912466; Tue, 14 Apr 2020 03:25:12 -0700 (PDT) Received: from localhost.localdomain (2a01cb0881b29e00157b082460d14b2a.ipv6.abo.wanadoo.fr. [2a01:cb08:81b2:9e00:157b:824:60d1:4b2a]) by smtp.gmail.com with ESMTPSA id i17sm8574487wru.39.2020.04.14.03.25.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 03:25:11 -0700 (PDT) From: Christophe Gisquet To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 Apr 2020 10:25:00 +0000 Message-Id: <20200414102503.7858-5-christophe.gisquet@gmail.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200414102503.7858-1-christophe.gisquet@gmail.com> References: <20200414102503.7858-1-christophe.gisquet@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/7] get_bits: replace index by an incremented pointer 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The main effect is actually code size reduction, due to the smaller refill code (or difference in inlining decision), e.g. on Win32 of {magicyuv,huffyuvdec,utvideodec}.o as follows: 19068/41460/16512 -> 18892/40760/16448 It should also be a small speedup (because it simplifies the address computation), but no change was measured. --- libavcodec/get_bits.h | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index 59bfbdd88b..4f75f9dd84 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -91,10 +91,12 @@ typedef uint32_t cache_type; typedef struct GetBitContext { const uint8_t *buffer, *buffer_end; #if CACHED_BITSTREAM_READER + const uint8_t *ptr; cache_type cache; unsigned bits_left; +#else + int index; // Cached version advances ptr instead #endif - int index; int size_in_bits; int size_in_bits_plus8; } GetBitContext; @@ -253,7 +255,7 @@ static inline unsigned int show_bits(GetBitContext *s, int n); static inline int get_bits_count(const GetBitContext *s) { #if CACHED_BITSTREAM_READER - return s->index - s->bits_left; + return 8*(s->ptr - s->buffer) - s->bits_left; #else return s->index; #endif @@ -263,42 +265,42 @@ static inline int get_bits_count(const GetBitContext *s) static inline void refill_half(GetBitContext *s, int is_le) { #if !UNCHECKED_BITSTREAM_READER - if (s->index >> 3 >= s->buffer_end - s->buffer) + if (s->ptr >= s->buffer_end) return; #endif #if BITSTREAM_BITS == 32 if (s->bits_left > 16) { if (is_le) - s->cache |= (uint32_t)s->buffer[s->index >> 3] << s->bits_left; + s->cache |= (uint32_t)s->ptr[0] << s->bits_left; else - s->cache |= (uint32_t)s->buffer[s->index >> 3] << (32 - s->bits_left); - s->index += 8; + s->cache |= (uint32_t)s->ptr[0] << (32 - s->bits_left); + s->ptr++; s->bits_left += 8; return; } #endif if (is_le) - s->cache |= (cache_type)AV_RL_HALF(s->buffer + (s->index >> 3)) << s->bits_left; + s->cache |= (cache_type)AV_RL_HALF(s->ptr) << s->bits_left; else - s->cache |= (cache_type)AV_RB_HALF(s->buffer + (s->index >> 3)) << (BITSTREAM_HBITS - s->bits_left); - s->index += BITSTREAM_HBITS; + s->cache |= (cache_type)AV_RB_HALF(s->ptr) << (BITSTREAM_HBITS - s->bits_left); + s->ptr += sizeof(s->cache)/2; s->bits_left += BITSTREAM_HBITS; } static inline void refill_all(GetBitContext *s, int is_le) { #if !UNCHECKED_BITSTREAM_READER - if (s->index >> 3 >= s->buffer_end - s->buffer) + if (s->ptr >= s->buffer_end) return; #endif if (is_le) - s->cache = AV_RL_ALL(s->buffer + (s->index >> 3)); + s->cache = AV_RL_ALL(s->ptr); else - s->cache = AV_RB_ALL(s->buffer + (s->index >> 3)); - s->index += BITSTREAM_BITS; + s->cache = AV_RB_ALL(s->ptr); + s->ptr += sizeof(s->cache); s->bits_left = BITSTREAM_BITS; } @@ -534,13 +536,12 @@ static inline void skip_bits(GetBitContext *s, int n) else { n -= s->bits_left; s->cache = 0; - s->bits_left = 0; if (n >= BITSTREAM_BITS) { - unsigned skip = (n / 8) * 8; + unsigned skip = n / 8; - n -= skip; - s->index += skip; + n -= 8*skip; + s->ptr += skip; } #ifdef BITSTREAM_READER_LE refill_all(s, 1); @@ -699,12 +700,14 @@ static inline int init_get_bits_xe(GetBitContext *s, const uint8_t *buffer, s->size_in_bits = bit_size; s->size_in_bits_plus8 = bit_size + 8; s->buffer_end = buffer + buffer_size; - s->index = 0; #if CACHED_BITSTREAM_READER + s->ptr = buffer; s->cache = 0; s->bits_left = 0; refill_all(s, is_le); +#else + s->index = 0; #endif return ret; @@ -757,7 +760,11 @@ static inline const uint8_t *align_get_bits(GetBitContext *s) int n = -get_bits_count(s) & 7; if (n) skip_bits(s, n); +#if CACHED_BITSTREAM_READER + return s->ptr; +#else return s->buffer + (s->index >> 3); +#endif } /** From patchwork Tue Apr 14 10:25:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Gisquet X-Patchwork-Id: 18949 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 EA25944B934 for ; Tue, 14 Apr 2020 13:25:22 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D4D0168B71C; Tue, 14 Apr 2020 13:25:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CA51D68B611 for ; Tue, 14 Apr 2020 13:25:13 +0300 (EEST) Received: by mail-wr1-f66.google.com with SMTP id d27so4467551wra.1 for ; Tue, 14 Apr 2020 03:25:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=cSedmOrP2Iv9HNy++Gqk5O7GUYDF3+jlqIcy3Bim+po=; b=i6KLY4ANhTlMye1DdkSXmD40YXPVwccQAXYrTmu3wa5WUpHIZBfknlM6+Gl1b/H9DC T0Cx/RvQHNGRC4vnLf25I3MWhhiq6AlLmVPDYl7936lGBmoBNosfnJGV/HUMiN5u/ycS wpxPLnYZgVo8PwvcRw2/erYsahKnytLu5KZHItOZbT/LurRf0yOgyb6Q6s6P+7Blm7vn 6cNJYnyL36TbLf6k1EeXaoHyO/XkC4Uw6Q4o8BnfwZ1PfUIkhakz8rxxfeRmc7w3sU5M Gb0+YluiEEHNCXGzPyyLdKzXZBA2mvTTZ1USCM2zJ9GGCfNCnL/hO87iesbTGvGk0y+B 1aOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cSedmOrP2Iv9HNy++Gqk5O7GUYDF3+jlqIcy3Bim+po=; b=R8BMtUHuddulBC8WiRlC1XErdNwXLRdn13VLhAzQp5ALAxawDSwONJ6F7l1aew/QHt BiI8zDjDbkKjEMPcUz4DRycvxaQ546PmiSTVbbW4b1TqwYi6iDaqfHwOgRc6Aw+ZRnN3 HW/OKcR5yBUm6/wJ+ABua8sasMPfz8dRuPct6qlweylWZpeh5kOpIxXlBDiISOFmPcXR PLj5GC+tvg4GXGBDqGK5p8KiBTJRSwXdWWn26EuskqH458L4aU7YmZj8lWQ1s2o6KCnF QoWCojkvy6IJ64L8tPk9LB+kdepA39qeyeEYjWP2p2SLwta8CDEWbgm17yMlwf52yZGF Acrw== X-Gm-Message-State: AGi0PuYze05hFjt+nvayrEiDUbbv5j4id9r39cvKdjGzVS6etWk6y/pb nigzDrUOYkbjUOw5QIdpKDvDyc0= X-Google-Smtp-Source: APiQypJg077jvW6Hber2gMJ4DWdAYb5msUVT/0F7dkbpwSrrB8FUldsEv1qtEohptHimTNrsQ+S3ew== X-Received: by 2002:adf:ab17:: with SMTP id q23mr22599885wrc.248.1586859913058; Tue, 14 Apr 2020 03:25:13 -0700 (PDT) Received: from localhost.localdomain (2a01cb0881b29e00157b082460d14b2a.ipv6.abo.wanadoo.fr. [2a01:cb08:81b2:9e00:157b:824:60d1:4b2a]) by smtp.gmail.com with ESMTPSA id i17sm8574487wru.39.2020.04.14.03.25.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 03:25:12 -0700 (PDT) From: Christophe Gisquet To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 Apr 2020 10:25:01 +0000 Message-Id: <20200414102503.7858-6-christophe.gisquet@gmail.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200414102503.7858-1-christophe.gisquet@gmail.com> References: <20200414102503.7858-1-christophe.gisquet@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/7] get_bits: improve and fix get_bits_long for 32b 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The new code is guaranteed to read at least 32bits, which is likely ok with the usual case that get_bits without cache can read up to 25. --- libavcodec/get_bits.h | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index 4f75f9dd84..da054ebfcb 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -608,21 +608,44 @@ static inline void skip_bits1(GetBitContext *s) */ static inline unsigned int get_bits_long(GetBitContext *s, int n) { + unsigned ret = 0; av_assert2(n>=0 && n<=32); if (!n) { return 0; #if CACHED_BITSTREAM_READER } - return get_bits(s, n); + +# ifdef BITSTREAM_READER_LE + unsigned left = 0; +# endif + if (n > s->bits_left) { + n -= s->bits_left; +# ifdef BITSTREAM_READER_LE + left = s->bits_left; + ret = get_val(s, s->bits_left, 1); + refill_all(s, 1); +# else + ret = get_val(s, s->bits_left, 0); + refill_all(s, 0); +# endif + } + +#ifdef BITSTREAM_READER_LE + ret = get_val(s, n, 1) << left | ret; +#else + ret = get_val(s, n, 0) | ret << n; +#endif + + return ret; #else } else if (n <= MIN_CACHE_BITS) { return get_bits(s, n); } else { #ifdef BITSTREAM_READER_LE - unsigned ret = get_bits(s, 16); + ret = get_bits(s, 16); return ret | (get_bits(s, n - 16) << 16); #else - unsigned ret = get_bits(s, 16) << (n - 16); + ret = get_bits(s, 16) << (n - 16); return ret | get_bits(s, n - 16); #endif } From patchwork Tue Apr 14 10:25:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Gisquet X-Patchwork-Id: 18950 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 EAE5844B934 for ; Tue, 14 Apr 2020 13:25:24 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D751A68B67A; Tue, 14 Apr 2020 13:25:24 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CFA1F68B4CC for ; Tue, 14 Apr 2020 13:25:14 +0300 (EEST) Received: by mail-wr1-f42.google.com with SMTP id h26so2270876wrb.7 for ; Tue, 14 Apr 2020 03:25:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=wEllzq1XN+WIqn2iqqxKy2ENO2YkSfTj6CiZd7jOMq0=; b=tUrXMQ5ghQ9mMQ3UH8G5ZwEJI5FV3WIUKsxitl0PgsyGwvBaqXZiToZtUaet95rjJa jQc4NnBGIhaEcoqHAsgEYFRy38lIKhUP6mac8SDbTpi7r24HCbi82YGcXbmrG7tBkYTm h+41QhQyNNwsNh2B1mDfkiIL7J+Nsfepfoor8tZBl88PZgS2/7r3d7ilGSSkveBxHl4E OhzyJsbvVM1cELC3IxXAcZ9wpLbX0qEvP2KaDwRBP5wR7inPID16a2Ya3g9oumFC/Dre w4Kne1vlAMI8wNRln5+P3vvJTD6McMy+I57UjRtdwnaRN8WbNTgkShdxq38ldas2GFAn IRMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wEllzq1XN+WIqn2iqqxKy2ENO2YkSfTj6CiZd7jOMq0=; b=U0BcC35cWjnRZDN14nz/sjAW5iskCu8dLtMTW0n4+0GChUe1VRMfoScwnXb9wKKJZn 9IZCBjWoO7GRFto7XZfTyeUMuKRuPGo4g1Btg90J7F0Frqn5u/GP+OVXpGYEulf6jC2v cMkcUQaNzY8aIaz7LVjX3vJ6v+G2pYhIawggIpZN0x0/gq/l2005JwJiYq/k8XkROgZD 1R2JeakI06wfNR+Yr7qlsdQR0E0+vNyjtAqUYp+VOTfcPpD8U41HOZdumGweeWFlnCPy gAOQhPjzOxSKpFW/RE7yXkcI15Dye3iCoptP5Firwjn67exzbTCb20YG12xB/xxVcCvI EsMQ== X-Gm-Message-State: AGi0Puag7MYGrjwYXYQnswxY+VyZhLmOS/6FO8ymV9J7+r1D9RTr9b3x ZRI7W1vD0wKZ47C6TfB/w981KMM= X-Google-Smtp-Source: APiQypLiKMIqoMSDDZB3EgaFC+oTY8XT9cQnOCaWPAKxnsivAA2PHfNxUsP8AcyQj3yr6uT/Dad2VQ== X-Received: by 2002:adf:ef51:: with SMTP id c17mr22762678wrp.130.1586859913945; Tue, 14 Apr 2020 03:25:13 -0700 (PDT) Received: from localhost.localdomain (2a01cb0881b29e00157b082460d14b2a.ipv6.abo.wanadoo.fr. [2a01:cb08:81b2:9e00:157b:824:60d1:4b2a]) by smtp.gmail.com with ESMTPSA id i17sm8574487wru.39.2020.04.14.03.25.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 03:25:13 -0700 (PDT) From: Christophe Gisquet To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 Apr 2020 10:25:02 +0000 Message-Id: <20200414102503.7858-7-christophe.gisquet@gmail.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200414102503.7858-1-christophe.gisquet@gmail.com> References: <20200414102503.7858-1-christophe.gisquet@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/7] get_bits: change refill to RAD pattern 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Described as variant 4 in the linked article. Results in faster and smaller code. Also, cases for the "refill_all" cases (usually when we want to empty/fill it) have been inlined. --- libavcodec/get_bits.h | 103 +++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 62 deletions(-) diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index da054ebfcb..baff86ecf6 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -153,11 +153,7 @@ static inline unsigned int show_bits(GetBitContext *s, int n); */ #if CACHED_BITSTREAM_READER -# if BITSTREAM_BITS == 32 -# define MIN_CACHE_BITS (32-7) -# else -# define MIN_CACHE_BITS 32 -# endif +# define MIN_CACHE_BITS (BITSTREAM_BITS-7) #elif defined LONG_BITSTREAM_READER # define MIN_CACHE_BITS 32 #else @@ -262,46 +258,21 @@ static inline int get_bits_count(const GetBitContext *s) } #if CACHED_BITSTREAM_READER -static inline void refill_half(GetBitContext *s, int is_le) +// See variant 4 in the following article: +// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/ +static inline void refill_gb(GetBitContext *s, int is_le) { #if !UNCHECKED_BITSTREAM_READER if (s->ptr >= s->buffer_end) return; #endif -#if BITSTREAM_BITS == 32 - if (s->bits_left > 16) { - if (is_le) - s->cache |= (uint32_t)s->ptr[0] << s->bits_left; - else - s->cache |= (uint32_t)s->ptr[0] << (32 - s->bits_left); - s->ptr++; - s->bits_left += 8; - return; - } -#endif - if (is_le) - s->cache |= (cache_type)AV_RL_HALF(s->ptr) << s->bits_left; + s->cache |= (cache_type)AV_RL_ALL(s->ptr) << s->bits_left; else - s->cache |= (cache_type)AV_RB_HALF(s->ptr) << (BITSTREAM_HBITS - s->bits_left); - s->ptr += sizeof(s->cache)/2; - s->bits_left += BITSTREAM_HBITS; -} - -static inline void refill_all(GetBitContext *s, int is_le) -{ -#if !UNCHECKED_BITSTREAM_READER - if (s->ptr >= s->buffer_end) - return; -#endif - - if (is_le) - s->cache = AV_RL_ALL(s->ptr); - else - s->cache = AV_RB_ALL(s->ptr); - s->ptr += sizeof(s->cache); - s->bits_left = BITSTREAM_BITS; + s->cache |= (cache_type)AV_RB_ALL(s->ptr) >> s->bits_left; + s->ptr += (BITSTREAM_BITS-1 - s->bits_left) >> 3; + s->bits_left |= BITSTREAM_BITS-8; } static inline cache_type get_val(GetBitContext *s, unsigned n, int is_le) @@ -374,9 +345,9 @@ static inline int get_xbits(GetBitContext *s, int n) if (n > s->bits_left) #ifdef BITSTREAM_READER_LE - refill_half(s, 1); + refill_gb(s, 1); #else - refill_half(s, 0); + refill_gb(s, 0); #endif #if BITSTREAM_BITS == 32 @@ -448,9 +419,9 @@ static inline unsigned int get_bits(GetBitContext *s, int n) av_assert2(n>0 && n<=32); if (n > s->bits_left) { #ifdef BITSTREAM_READER_LE - refill_half(s, 1); + refill_gb(s, 1); #else - refill_half(s, 0); + refill_gb(s, 0); #endif if (s->bits_left < BITSTREAM_HBITS) s->bits_left = n; @@ -486,7 +457,7 @@ static inline unsigned int get_bits_le(GetBitContext *s, int n) #if CACHED_BITSTREAM_READER av_assert2(n>0 && n<=32); if (n > s->bits_left) { - refill_half(s, 1); + refill_gb(s, 1); if (s->bits_left < BITSTREAM_HBITS) s->bits_left = n; } @@ -513,9 +484,9 @@ static inline unsigned int show_bits(GetBitContext *s, int n) #if CACHED_BITSTREAM_READER if (n > s->bits_left) #ifdef BITSTREAM_READER_LE - refill_half(s, 1); + refill_gb(s, 1); #else - refill_half(s, 0); + refill_gb(s, 0); #endif tmp = show_val(s, n); @@ -535,7 +506,6 @@ static inline void skip_bits(GetBitContext *s, int n) skip_remaining(s, n); else { n -= s->bits_left; - s->cache = 0; if (n >= BITSTREAM_BITS) { unsigned skip = n / 8; @@ -543,11 +513,14 @@ static inline void skip_bits(GetBitContext *s, int n) n -= 8*skip; s->ptr += skip; } + #ifdef BITSTREAM_READER_LE - refill_all(s, 1); + s->cache = AV_RL_ALL(s->ptr); #else - refill_all(s, 0); + s->cache = AV_RB_ALL(s->ptr); #endif + s->ptr += sizeof(cache_type); + s->bits_left = BITSTREAM_BITS; if (n) skip_remaining(s, n); } @@ -561,12 +534,15 @@ static inline void skip_bits(GetBitContext *s, int n) static inline unsigned int get_bits1(GetBitContext *s) { #if CACHED_BITSTREAM_READER - if (!s->bits_left) + if (!s->bits_left) { #ifdef BITSTREAM_READER_LE - refill_all(s, 1); + s->cache = AV_RL_ALL(s->ptr); #else - refill_all(s, 0); + s->cache = AV_RB_ALL(s->ptr); #endif + s->ptr += sizeof(cache_type); + s->bits_left = BITSTREAM_BITS; + } #ifdef BITSTREAM_READER_LE return get_val(s, 1, 1); @@ -621,20 +597,21 @@ static inline unsigned int get_bits_long(GetBitContext *s, int n) if (n > s->bits_left) { n -= s->bits_left; # ifdef BITSTREAM_READER_LE - left = s->bits_left; - ret = get_val(s, s->bits_left, 1); - refill_all(s, 1); + ret = s->cache & ((CACHE_TYPE(1) << s->bits_left) - 1); + s->cache = AV_RL_ALL(s->ptr); # else - ret = get_val(s, s->bits_left, 0); - refill_all(s, 0); + ret = s->cache >> (BITSTREAM_BITS - s->bits_left); + s->cache = AV_RB_ALL(s->ptr); # endif + s->ptr += sizeof(cache_type); + s->bits_left = BITSTREAM_BITS; // OK because consumed right after } -#ifdef BITSTREAM_READER_LE +# ifdef BITSTREAM_READER_LE ret = get_val(s, n, 1) << left | ret; -#else +# else ret = get_val(s, n, 0) | ret << n; -#endif +# endif return ret; #else @@ -725,10 +702,12 @@ static inline int init_get_bits_xe(GetBitContext *s, const uint8_t *buffer, s->buffer_end = buffer + buffer_size; #if CACHED_BITSTREAM_READER - s->ptr = buffer; - s->cache = 0; - s->bits_left = 0; - refill_all(s, is_le); + if (is_le) + s->cache = AV_RL_ALL(buffer); + else + s->cache = AV_RB_ALL(buffer); + s->ptr = buffer+sizeof(cache_type); + s->bits_left = BITSTREAM_BITS; #else s->index = 0; #endif From patchwork Tue Apr 14 10:25:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Gisquet X-Patchwork-Id: 18951 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 1C6B144B934 for ; Tue, 14 Apr 2020 13:25:26 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 010A868B6DB; Tue, 14 Apr 2020 13:25:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3007868B4CC for ; Tue, 14 Apr 2020 13:25:15 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id x4so12402818wmj.1 for ; Tue, 14 Apr 2020 03:25:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=DesOzyW9X5tnuKDUAUdPG1oUvcNRp3VI8MCI8o6SP8U=; b=Ql9tjkkZuG6mnLo6BmQqs1yGWUFFqV6guPToEhss/kr7RvSGRwW1Wa9DGvqqIldXAV AghNZ3VjTUyYdmSvwx7zQeCKQ6MPxGDEN/iaRJc2KhtJ9Hla0NR92nKkdTMYzXDE45iH t/X76d4hZH7ybpUqJ/TP4LXGkErexhSKOUStKF6MoU1VvnYeDzdXgFSXhMUFYhuKy5II pfvHcwY1mEKTPJ7V89EA3Z/NHETlZe4at3tO3PD+VPVtIXlyJEa+YmsQZabgVhBKkPAC IfZFVrH20Oj6NqexND9GWVnrxzaOjRl/rdzUX/4MGgN6axe/PmjXSYA2PskdDqxrfNUH Lckg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DesOzyW9X5tnuKDUAUdPG1oUvcNRp3VI8MCI8o6SP8U=; b=mnBIeL4jJBF4/h7Rh9CiH/lBoDsUPJNyiZAkyGYuOwxBiggt5P5hOJjrvEQegpTOx+ exzk+5RHIaHc/rRd3BQaieTgX0P0fqppB+q4Xc60u35M6mNFFYC2XONy5sSspUz+UwEh BITS6LD1bREkNMzNq2LbfkXGPKEOA2cHOK6+qAVrpKT38y+ToFrk4zOQSW2cfMzaeRKz elxIm5rdxY24sgMhnobhMGvAEqp3wiV9sGckBOwTrU44PFF1lRw9IBNY66NXbQ9Az+Fp ZTZmXZQ2pvSstQRzRCdxiYNeNV0mnyVfxg7E8+QkWpOMkMNVtIqwWuWCz70rtp3tj3NT I45w== X-Gm-Message-State: AGi0PuYgj2C+g8xlfFSUk74hxIoU3cCaa7M6lUeoA/tBLRJrRcnXfRR8 7wjjB3Ej5a1C87KNnD2MTqqyBt0= X-Google-Smtp-Source: APiQypLdBvuScl8OcchZajSH6TtXnFjOtPdfH0NiEMsvarB60m40+hVxM9cz26FXMkJ5sbOQYwf/vA== X-Received: by 2002:a7b:c0d5:: with SMTP id s21mr22796808wmh.107.1586859914500; Tue, 14 Apr 2020 03:25:14 -0700 (PDT) Received: from localhost.localdomain (2a01cb0881b29e00157b082460d14b2a.ipv6.abo.wanadoo.fr. [2a01:cb08:81b2:9e00:157b:824:60d1:4b2a]) by smtp.gmail.com with ESMTPSA id i17sm8574487wru.39.2020.04.14.03.25.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 03:25:14 -0700 (PDT) From: Christophe Gisquet To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 Apr 2020 10:25:03 +0000 Message-Id: <20200414102503.7858-8-christophe.gisquet@gmail.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200414102503.7858-1-christophe.gisquet@gmail.com> References: <20200414102503.7858-1-christophe.gisquet@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 7/7] get_bits: use immediate in skip_remaining 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" When the entry informs to continue reading, this means the current read will be entirely skipped. Small object size reduction, depending on inlining. --- libavcodec/get_bits.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index baff86ecf6..d1e29b9917 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -793,7 +793,7 @@ static inline const uint8_t *align_get_bits(GetBitContext *s) code = table[index][0]; \ n = table[index][1]; \ if (max_depth > 2 && n < 0) { \ - LAST_SKIP_BITS(name, gb, nb_bits); \ + LAST_SKIP_BITS(name, gb, bits); \ UPDATE_CACHE(name, gb); \ \ nb_bits = -n; \ @@ -878,7 +878,7 @@ static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], skip_remaining(s, bits); code = set_idx(s, code, &n, &nb_bits, table); if (max_depth > 2 && n < 0) { - skip_remaining(s, nb_bits); + skip_remaining(s, bits); code = set_idx(s, code, &n, &nb_bits, table); } }