From patchwork Tue Apr 3 11:38:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 8305 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp3581223jad; Tue, 3 Apr 2018 04:44:50 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+WgWn5Rlyi1w4nfNMLiW323sgNfJ9U02hFiOmnMdz8F3akjqMn+cbl8AgJR+ceoJxTM8zx X-Received: by 10.28.136.199 with SMTP id k190mr4178000wmd.154.1522755890044; Tue, 03 Apr 2018 04:44:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522755890; cv=none; d=google.com; s=arc-20160816; b=GRfFzWuzA9XSB7EiwSx9bsHHxqkIgF9exsWn5BvD7KphM193Lh3VbE2/Nm3iNyVF5C hRkwMgzfYg0auiLrRAMCaOzooNPZZ4hfAtQtsUTYss99YKZEVFmXMHFK2Oh2Zgxp0Dgj riFoxyuqt6lOxBIeJWxfG2dT7Sy8RRKH9dzSKnHSWK7ut64vGXHOQJZ8IsTLGQh5nxBh WCL5oDNlNBVpxaOs98f1DWMwmk8Zj+tPE5uFhKBeDYYNUZGywMfSSseFgxQtbbvheZlm J3Xmn3h5GZNGcDxvrpiMebG6l4ATBH2JaAnl3gwA6RFFZxzwNuMKqXofDG6vSlnBjUoF sRKA== 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:message-id:date:to:from :dkim-signature:delivered-to:arc-authentication-results; bh=IRURqmPTJAQ2IbVZAbnvSes4tJT/J1jK1g8dLKNfIy4=; b=mA8+GGbAv0Urk4lxvLeILRWjnlriDpNM5lq+egIw9CRRlfoySlFPewJisCAFaykKIB gBirqaOm45whqUn5XqIi+p41EbDzZI9iMqj1S/LwaehcNfHbBAyeqtthgiJ13I5vq+wu /Cwt9HaD3M/uJXXF1qZwxn0iT3tbUi7ULOOZmc3PT0xB75Rv6IRFyhToQNwnwjAus3t/ E48utJOzJ5Q/bt2zD6YdUXocIYj1b0LqCCWOSPZk2tiCx+c7xeO+gVQOTEiu9S0WziVC sWZvrTpwUBW/YEHEeq6J/WwGMfxyOz/5fk2Irwa9bCh+l4f8A/vQaGSzc+WuRC3bgiO6 jC1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=StRQvcxm; 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 n1si1901203wre.255.2018.04.03.04.44.49; Tue, 03 Apr 2018 04:44:49 -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=20161025 header.b=StRQvcxm; 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 EC2E3689B98; Tue, 3 Apr 2018 14:44:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6B51A680636 for ; Tue, 3 Apr 2018 14:44:12 +0300 (EEST) Received: by mail-wm0-f66.google.com with SMTP id i3so10786240wmf.3 for ; Tue, 03 Apr 2018 04:44:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=dNamWozt1YjnhvdSLER8YwI9HQ7KRdmEQNrvWIbao8o=; b=StRQvcxmjgLuZ6XtLUbQXMlXCbWvy75injneSfE3gKdSjFVNr9549cmLfz9OtFAgXy 6nEkDpGOKNXrwJ49lpY3Jwa+/Fxo1Kui64zinJFA9Xshy5EnvJw+mg2wDpRU60w5RRmT FGb3tBb9C6JivHMr/0Nqg3U1uCk/RMYQWUKFJpSgaQCQbAvsYeQUCxCtkPUcW6hlkB6M unZVjdLaWJ840FGoKH7Y9MWfbud5maYmNaZDKrSinwG37OscomB/KWaBssEdjFRsyKmR 9RaPFEz5mnzXxilxl6U+G/89F/BPDPwwdbuPbROAHP/+2byWHKlCbhSTiAG5EQudMDOB qp2w== 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:mime-version :content-transfer-encoding; bh=dNamWozt1YjnhvdSLER8YwI9HQ7KRdmEQNrvWIbao8o=; b=Uf4s1l+Z01ax6m9GW++k+nR25COYIMEAtdu63zYKxXluKgM6VAq9rgFAvVHYElczIQ ZBVzrETSgqBtV3WOrowIBAgWUx4CkiBDFwB1LgCK6tqIF0zbo+VxiACC9kJdm4afn+Xo uCKi0AH4a4pcXSiOSyHylR+cm7EPyCSiDf5glnFzTjkftfgl2AGeD6ITvez9LafVWiSZ u2K7z0ee5eyiW0QpP7jca/C5rIV3djssXesjSuIjucaGVdn0U3UvaRq7z2DVVl2rzUEK 5zVd/fgU3CmI2zwSqjSIUbRfwA5vvoFM4dKAi6y5BykQxbDXONI0j+i+3i5UKySSrrhq bZ+A== X-Gm-Message-State: AElRT7FZ/xrQYv1oroMdgqUn1gHYuJjV4O01S1BLxlqlnOPwvkVU2GVm E5nW6GY190btnJUVpDLAaWmnNg== X-Received: by 10.80.215.138 with SMTP id w10mr16100643edi.10.1522755537484; Tue, 03 Apr 2018 04:38:57 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id e7sm1617462edi.28.2018.04.03.04.38.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 03 Apr 2018 04:38:56 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 3 Apr 2018 13:38:12 +0200 Message-Id: <20180403113814.7937-1-onemda@gmail.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] avcodec/get_bits: add cached bitstream reader 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" Signed-off-by: Paul B Mahol --- libavcodec/get_bits.h | 268 +++++++++++++++++++++++++++++++++++++++++++++----- libavcodec/golomb.h | 151 ++++++++++++++++++++++++++++ 2 files changed, 393 insertions(+), 26 deletions(-) diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index d7cf286378..64e1643e34 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -1,5 +1,6 @@ /* - * copyright (c) 2004 Michael Niedermayer + * Copyright (c) 2004 Michael Niedermayer + * Copyright (c) 2016 Alexandra Hájková * * This file is part of FFmpeg. * @@ -55,6 +56,10 @@ typedef struct GetBitContext { const uint8_t *buffer, *buffer_end; +#ifdef CACHED_BITSTREAM_READER + uint64_t cache; + unsigned bits_left; +#endif int index; int size_in_bits; int size_in_bits_plus8; @@ -107,12 +112,16 @@ typedef struct GetBitContext { * For examples see get_bits, show_bits, skip_bits, get_vlc. */ -#ifdef LONG_BITSTREAM_READER +#ifdef CACHED_BITSTREAM_READER +# define MIN_CACHE_BITS 64 +#elif defined LONG_BITSTREAM_READER # define MIN_CACHE_BITS 32 #else # define MIN_CACHE_BITS 25 #endif +#ifndef CACHED_BITSTREAM_READER + #define OPEN_READER_NOSIZE(name, gb) \ unsigned int name ## _index = (gb)->index; \ unsigned int av_unused name ## _cache @@ -197,20 +206,113 @@ typedef struct GetBitContext { #define GET_CACHE(name, gb) ((uint32_t) name ## _cache) +#endif + static inline int get_bits_count(const GetBitContext *s) { +#ifdef CACHED_BITSTREAM_READER + return s->index - s->bits_left; +#else return s->index; +#endif } -static inline void skip_bits_long(GetBitContext *s, int n) +static inline void refill_32(GetBitContext *s) { -#if UNCHECKED_BITSTREAM_READER - s->index += n; +#ifdef CACHED_BITSTREAM_READER +#if !UNCHECKED_BITSTREAM_READER + if (s->index >> 3 >= s->buffer_end - s->buffer) + return; +#endif + +#ifdef BITSTREAM_READER_LE + s->cache = (uint64_t)AV_RL32(s->buffer + (s->index >> 3)) << s->bits_left | s->cache; #else - s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index); + s->cache = s->cache | (uint64_t)AV_RB32(s->buffer + (s->index >> 3)) << (32 - s->bits_left); +#endif + s->index += 32; + s->bits_left += 32; +#endif +} + +static inline void refill_64(GetBitContext *s) +{ +#ifdef CACHED_BITSTREAM_READER +#if !UNCHECKED_BITSTREAM_READER + if (s->index >> 3 >= s->buffer_end - s->buffer) + return; +#endif + +#ifdef BITSTREAM_READER_LE + s->cache = AV_RL64(s->buffer + (s->index >> 3)); +#else + s->cache = AV_RB64(s->buffer + (s->index >> 3)); +#endif + s->index += 64; + s->bits_left = 64; +#endif +} + +#ifdef CACHED_BITSTREAM_READER +static inline uint64_t get_val(GetBitContext *s, unsigned n, int is_le) +{ + uint64_t ret; + av_assert2(n>0 && n<=63); + if (is_le) { + ret = s->cache & ((UINT64_C(1) << n) - 1); + s->cache >>= n; + } else { + ret = s->cache >> (64 - n); + s->cache <<= n; + } + s->bits_left -= n; + return ret; +} +#endif + +#ifdef CACHED_BITSTREAM_READER +static inline unsigned show_val(const GetBitContext *s, unsigned n) +{ +#ifdef BITSTREAM_READER_LE + return s->cache & ((UINT64_C(1) << n) - 1); +#else + return s->cache >> (64 - n); +#endif +} +#endif + +/** + * Show 1-25 bits. + */ +static inline unsigned int show_bits(GetBitContext *s, int n) +{ + register int tmp; +#ifdef CACHED_BITSTREAM_READER + if (n > s->bits_left) + refill_32(s); + + tmp = show_val(s, n); +#else + OPEN_READER_NOSIZE(re, s); + av_assert2(n>0 && n<=25); + UPDATE_CACHE(re, s); + tmp = SHOW_UBITS(re, s, n); #endif + return tmp; } +#ifdef CACHED_BITSTREAM_READER +static inline void skip_remaining(GetBitContext *s, unsigned n) +{ +#ifdef BITSTREAM_READER_LE + s->cache >>= n; +#else + s->cache <<= n; +#endif + s->bits_left -= n; +} +#endif + /** * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB). * if MSB not set it is negative @@ -218,6 +320,13 @@ static inline void skip_bits_long(GetBitContext *s, int n) */ static inline int get_xbits(GetBitContext *s, int n) { +#ifdef CACHED_BITSTREAM_READER + int32_t cache = show_bits(s, 32); + int sign = ~cache >> 31; + skip_remaining(s, n); + + return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign; +#else register int sign; register int32_t cache; OPEN_READER(re, s); @@ -228,8 +337,10 @@ static inline int get_xbits(GetBitContext *s, int n) LAST_SKIP_BITS(re, s, n); CLOSE_READER(re, s); return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; +#endif } +#ifndef CACHED_BITSTREAM_READER static inline int get_xbits_le(GetBitContext *s, int n) { register int sign; @@ -243,31 +354,53 @@ static inline int get_xbits_le(GetBitContext *s, int n) CLOSE_READER(re, s); return (zero_extend(sign ^ cache, n) ^ sign) - sign; } +#endif -static inline int get_sbits(GetBitContext *s, int n) +/** + * Read 1-25 bits. + */ +static inline unsigned int get_bits(GetBitContext *s, int n) { register int tmp; +#ifdef CACHED_BITSTREAM_READER + + av_assert2(n>0 && n<=32); + if (n > s->bits_left) { + refill_32(s); + if (s->bits_left < 32) + s->bits_left = n; + } + +#ifdef BITSTREAM_READER_LE + tmp = get_val(s, n, 1); +#else + tmp = get_val(s, n, 0); +#endif +#else OPEN_READER(re, s); av_assert2(n>0 && n<=25); UPDATE_CACHE(re, s); - tmp = SHOW_SBITS(re, s, n); + tmp = SHOW_UBITS(re, s, n); LAST_SKIP_BITS(re, s, n); CLOSE_READER(re, s); +#endif return tmp; } -/** - * Read 1-25 bits. - */ -static inline unsigned int get_bits(GetBitContext *s, int n) +static inline int get_sbits(GetBitContext *s, int n) { register int tmp; +#ifdef CACHED_BITSTREAM_READER + av_assert2(n>0 && n<=25); + tmp = sign_extend(get_bits(s, n), n); +#else OPEN_READER(re, s); av_assert2(n>0 && n<=25); UPDATE_CACHE(re, s); - tmp = SHOW_UBITS(re, s, n); + tmp = SHOW_SBITS(re, s, n); LAST_SKIP_BITS(re, s, n); CLOSE_READER(re, s); +#endif return tmp; } @@ -281,6 +414,16 @@ static av_always_inline int get_bitsz(GetBitContext *s, int n) static inline unsigned int get_bits_le(GetBitContext *s, int n) { +#ifdef CACHED_BITSTREAM_READER + av_assert2(n>0 && n<=32); + if (n > s->bits_left) { + refill_32(s); + if (s->bits_left < 32) + s->bits_left = n; + } + + return get_val(s, n, 1); +#else register int tmp; OPEN_READER(re, s); av_assert2(n>0 && n<=25); @@ -289,30 +432,61 @@ static inline unsigned int get_bits_le(GetBitContext *s, int n) LAST_SKIP_BITS(re, s, n); CLOSE_READER(re, s); return tmp; -} - -/** - * Show 1-25 bits. - */ -static inline unsigned int show_bits(GetBitContext *s, int n) -{ - register int tmp; - OPEN_READER_NOSIZE(re, s); - av_assert2(n>0 && n<=25); - UPDATE_CACHE(re, s); - tmp = SHOW_UBITS(re, s, n); - return tmp; +#endif } static inline void skip_bits(GetBitContext *s, int n) { +#ifdef CACHED_BITSTREAM_READER + if (n < s->bits_left) + skip_remaining(s, n); + else { + n -= s->bits_left; + s->cache = 0; + s->bits_left = 0; + + if (n >= 64) { + unsigned skip = (n / 8) * 8; + + n -= skip; + s->index += skip; + } + refill_64(s); + if (n) + skip_remaining(s, n); + } +#else OPEN_READER(re, s); LAST_SKIP_BITS(re, s, n); CLOSE_READER(re, s); +#endif +} + +static inline void skip_bits_long(GetBitContext *s, int n) +{ +#ifdef CACHED_BITSTREAM_READER + skip_bits(s, n); +#else +#if UNCHECKED_BITSTREAM_READER + s->index += n; +#else + s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index); +#endif +#endif } static inline unsigned int get_bits1(GetBitContext *s) { +#ifdef CACHED_BITSTREAM_READER + if (!s->bits_left) + refill_64(s); + +#ifdef BITSTREAM_READER_LE + return get_val(s, 1, 1); +#else + return get_val(s, 1, 0); +#endif +#else unsigned int index = s->index; uint8_t result = s->buffer[index >> 3]; #ifdef BITSTREAM_READER_LE @@ -329,6 +503,7 @@ static inline unsigned int get_bits1(GetBitContext *s) s->index = index; return result; +#endif } static inline unsigned int show_bits1(GetBitContext *s) @@ -349,6 +524,10 @@ static inline unsigned int get_bits_long(GetBitContext *s, int n) av_assert2(n>=0 && n<=32); if (!n) { return 0; +#ifdef CACHED_BITSTREAM_READER + } + return get_bits(s, n); +#else } else if (n <= MIN_CACHE_BITS) { return get_bits(s, n); } else { @@ -360,6 +539,7 @@ static inline unsigned int get_bits_long(GetBitContext *s, int n) return ret | get_bits(s, n - 16); #endif } +#endif } /** @@ -443,6 +623,10 @@ static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer, s->buffer_end = buffer + buffer_size; s->index = 0; +#ifdef CACHED_BITSTREAM_READER + refill_64(s); +#endif + return ret; } @@ -544,6 +728,19 @@ static inline const uint8_t *align_get_bits(GetBitContext *s) SKIP_BITS(name, gb, n); \ } while (0) +/* Return the LUT element for the given bitstream configuration. */ +static inline int set_idx(GetBitContext *s, int code, int *n, int *nb_bits, + VLC_TYPE (*table)[2]) +{ + unsigned idx; + + *nb_bits = -*n; + idx = show_bits(s, *nb_bits) + code; + *n = table[idx][1]; + + return table[idx][0]; +} + /** * Parse a vlc code. * @param bits is the number of bits which will be read at once, must be @@ -556,6 +753,24 @@ static inline const uint8_t *align_get_bits(GetBitContext *s) static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth) { +#ifdef CACHED_BITSTREAM_READER + int nb_bits; + unsigned idx = show_bits(s, bits); + int code = table[idx][0]; + int n = table[idx][1]; + + if (max_depth > 1 && n < 0) { + skip_remaining(s, bits); + code = set_idx(s, code, &n, &nb_bits, table); + if (max_depth > 2 && n < 0) { + skip_remaining(s, nb_bits); + code = set_idx(s, code, &n, &nb_bits, table); + } + } + skip_remaining(s, n); + + return code; +#else int code; OPEN_READER(re, s); @@ -566,6 +781,7 @@ static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], CLOSE_READER(re, s); return code; +#endif } static inline int decode012(GetBitContext *gb) diff --git a/libavcodec/golomb.h b/libavcodec/golomb.h index efb1eff8aa..e66675b292 100644 --- a/libavcodec/golomb.h +++ b/libavcodec/golomb.h @@ -54,6 +54,23 @@ static inline int get_ue_golomb(GetBitContext *gb) { unsigned int buf; +#ifdef CACHED_BITSTREAM_READER + buf = show_bits_long(gb, 32); + + if (buf >= (1 << 27)) { + buf >>= 32 - 9; + skip_bits_long(gb, ff_golomb_vlc_len[buf]); + + return ff_ue_golomb_vlc_code[buf]; + } else { + int log = 2 * av_log2(buf) - 31; + buf >>= log; + buf--; + skip_bits_long(gb, 32 - log); + + return buf; + } +#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -77,6 +94,7 @@ static inline int get_ue_golomb(GetBitContext *gb) return buf; } +#endif } /** @@ -101,6 +119,13 @@ static inline int get_ue_golomb_31(GetBitContext *gb) { unsigned int buf; +#ifdef CACHED_BITSTREAM_READER + buf = show_bits_long(gb, 32); + + buf >>= 32 - 9; + skip_bits_long(gb, ff_golomb_vlc_len[buf]); +#else + OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -108,6 +133,7 @@ static inline int get_ue_golomb_31(GetBitContext *gb) buf >>= 32 - 9; LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); CLOSE_READER(re, gb); +#endif return ff_ue_golomb_vlc_code[buf]; } @@ -116,6 +142,33 @@ static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb) { uint32_t buf; +#ifdef CACHED_BITSTREAM_READER + buf = show_bits_long(gb, 32); + + if (buf & 0xAA800000) { + buf >>= 32 - 8; + skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]); + + return ff_interleaved_ue_golomb_vlc_code[buf]; + } else { + unsigned ret = 1; + + do { + buf >>= 32 - 8; + skip_bits_long(gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8)); + + if (ff_interleaved_golomb_vlc_len[buf] != 9) { + ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1; + ret |= ff_interleaved_dirac_golomb_vlc_code[buf]; + break; + } + ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf]; + buf = show_bits_long(gb, 32); + } while (get_bits_left(gb) > 0); + + return ret - 1; + } +#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -147,6 +200,7 @@ static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb) CLOSE_READER(re, gb); return ret - 1; } +#endif } /** @@ -184,6 +238,28 @@ static inline int get_se_golomb(GetBitContext *gb) { unsigned int buf; +#ifdef CACHED_BITSTREAM_READER + buf = show_bits_long(gb, 32); + + if (buf >= (1 << 27)) { + buf >>= 32 - 9; + skip_bits_long(gb, ff_golomb_vlc_len[buf]); + + return ff_se_golomb_vlc_code[buf]; + } else { + int log = 2 * av_log2(buf) - 31; + buf >>= log; + + skip_bits_long(gb, 32 - log); + + if (buf & 1) + buf = -(buf >> 1); + else + buf = (buf >> 1); + + return buf; + } +#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -210,6 +286,7 @@ static inline int get_se_golomb(GetBitContext *gb) return buf; } +#endif } static inline int get_se_golomb_long(GetBitContext *gb) @@ -223,6 +300,30 @@ static inline int get_interleaved_se_golomb(GetBitContext *gb) { unsigned int buf; +#ifdef CACHED_BITSTREAM_READER + buf = show_bits_long(gb, 32); + + if (buf & 0xAA800000) { + buf >>= 32 - 8; + skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]); + + return ff_interleaved_se_golomb_vlc_code[buf]; + } else { + int log; + skip_bits(gb, 8); + buf |= 1 | show_bits_long(gb, 24); + + if ((buf & 0xAAAAAAAA) == 0) + return INVALID_VLC; + + for (log = 31; (buf & 0x80000000) == 0; log--) + buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); + + skip_bits_long(gb, 63 - 2 * log - 8); + + return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; + } +#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -250,6 +351,7 @@ static inline int get_interleaved_se_golomb(GetBitContext *gb) return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; } +#endif } static inline int dirac_get_se_golomb(GetBitContext *gb) @@ -273,6 +375,24 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, unsigned int buf; int log; +#ifdef CACHED_BITSTREAM_READER + buf = show_bits_long(gb, 32); + + log = av_log2(buf); + + if (log > 31 - limit) { + buf >>= log - k; + buf += (30 - log) << k; + skip_bits_long(gb, 32 + k - log); + + return buf; + } else { + skip_bits_long(gb, limit); + buf = get_bits_long(gb, esc_len); + + return buf + limit - 1; + } +#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -297,6 +417,7 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, return buf + limit - 1; } +#endif } /** @@ -308,6 +429,35 @@ static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, unsigned int buf; int log; +#ifdef CACHED_BITSTREAM_READER + buf = show_bits_long(gb, 32); + + log = av_log2(buf); + + if (log - k >= 1 && 32 - log < limit) { + buf >>= log - k; + buf += (30 - log) << k; + skip_bits_long(gb, 32 + k - log); + + return buf; + } else { + int i; + for (i = 0; + i < limit && get_bits1(gb) == 0 && get_bits_left(gb) > 0; + i++); + + if (i < limit - 1) { + buf = get_bits_long(gb, k); + + return buf + (i << k); + } else if (i == limit - 1) { + buf = get_bits_long(gb, esc_len); + + return buf + 1; + } else + return -1; + } +#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -364,6 +514,7 @@ static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, CLOSE_READER(re, gb); return buf; } +#endif } /**