From patchwork Tue Jul 11 16:40:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 4297 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp5271566vsb; Tue, 11 Jul 2017 09:40:39 -0700 (PDT) X-Received: by 10.28.230.211 with SMTP id e80mr3454125wmi.17.1499791239566; Tue, 11 Jul 2017 09:40:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499791239; cv=none; d=google.com; s=arc-20160816; b=B7E3PQAJgT8xKaIinceRmw3LRtXHhcPI+rvTsnbxMSkKX3BXzBpbxza3UjuZ5Yb+1f wyVijR+O6Bnp2J5xzarKVUZH7DyVmKXuCxU0H8pkkuoDytacIhb+3t9uQGSjEPNS+u0m Ltsf2CPetveVE1Cyh2lUpyo8Q3SzwyuPVRe7Lrc70dO5l/DS8nzPtOvyRaoE67SQCVKw B9kO03dWht5Wlxpi3dZdv4sQVLvauifRX7uzlPtkC2ScXeDyxpVK0MoZIGZqx/fwIMJg cCEgafqlMTLFKsc1Z+5lermezlhcnF0BTaDoCLUcrYf6KVvsa/Ewe2titdJsaQYjOW+A yr4Q== 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:arc-authentication-results; bh=GLw0xQi7d/iS00khrWV66R5AQWaCcK0g9z7UbsKzQQE=; b=d6f84NTwQIYIQrYpoN1TlrggDFLpgIImi+r3UjZ1m4OzIYCMc85eU8fa+D2fqAVNii 9qntwtohAae3Uy3U5/lXuAEMc6Yz/nEOUoWO5Ju+WQsagdqzwglr336wJT50ITCTa23K fYvnAMGgwLMiPR7J7pAj3kDWCg7l8TSh+pkRmwBLCtTA8kRQ5s4WMZz29vmLvLhYxLX5 yMN70UJyLgmhvvJAuTWpNReWLh/S48E+Vj/4mRG0qmHCPEtGweQv81Xe2Pm1LImEfGPk x1KXFVhSNHbLNn0PXJNKwbafpLu9NjEQYwbD9dmKJM3aRRO5RG/vUN127VU1D09mFxe3 qviQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=Cb6ktFpl; 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=NONE 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 b204si9877486wmc.23.2017.07.11.09.40.38; Tue, 11 Jul 2017 09:40:39 -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.b=Cb6ktFpl; 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=NONE 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 E71D5689E56; Tue, 11 Jul 2017 19:40:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f195.google.com (mail-wr0-f195.google.com [209.85.128.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 261C3689BBA for ; Tue, 11 Jul 2017 19:40:24 +0300 (EEST) Received: by mail-wr0-f195.google.com with SMTP id k67so1061425wrc.1 for ; Tue, 11 Jul 2017 09:40:28 -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=+qKxQxwdsCrt/x7mzEmRFSNkYuyuIITTIFg7ueMu/kY=; b=Cb6ktFplOyJUhauUfbWUee88ii9MJW1+S/OQKwpKMuoA6I34DQEW0X1cks3YRzQNl5 +eyQD8vGdswv3W35pyuU1r0SuV462tcHMFd+n2EOXttJzF3Mk5N/owWKoTUmrBrqo74y Zvusgqzk99W8Siq68hqTzR6XuiJoR1ZvQsel2DV4bW3rmbzjpA8WL4E0rhRSXTPuyHeT Vnl5iVzqBBI3ec1Q6u0/omQyLS0H2rhp43EtXrKeB1zNFC/Fpo4epQj1N4so9flw9r+t dhH9AM7TUAx+R3DH/6icNPuBKhSsJOMeaqXg9MCoUioEr9Z20upG0lYxOmMFQWXxrSxc diGg== 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=+qKxQxwdsCrt/x7mzEmRFSNkYuyuIITTIFg7ueMu/kY=; b=h8pReBs4/IBKZwq8RwYJMy8UiCwxZp0AjQaGr4+NINzo4ocQZ1fLd+HIPUlWJdwHmN 1rAhVuty27x4Ys+NqhuQF9OtQ6ZzqDLK7okVcvmamjiiWf+oyF7ZBOr3EW4PFZjE84Yr 8+V+FeL73WLD5rd7HyGGg9pBO8r3MpyuGSyvWOWgYTHx9XxUw7opFs3o1v9pFFqpzf3/ Hdw4xYCD1lN+M28Tg4FbFupUl0buVoNKlv6Do8OIZTtgwG1VLDwDN+ykeli/NuPTikHP wFgWay2RVZ1Ll4N1YUcxWVX84qHS4P3mgpKeBe/h9TRiZptR44mNwPpyg14HkvXvTmXE ZPRA== X-Gm-Message-State: AIVw111QDu2WgDxVcl0MU+kRwibZJ2t64GOBY0WdHkb4y63KuquZeKnI fouNCuJpvMdeorzV X-Received: by 10.223.148.226 with SMTP id 89mr459987wrr.169.1499791228122; Tue, 11 Jul 2017 09:40:28 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id 35sm321388wrp.63.2017.07.11.09.40.25 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Jul 2017 09:40:26 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 11 Jul 2017 18:40:16 +0200 Message-Id: <20170711164016.26688-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170708091206.9293-1-onemda@gmail.com> References: <20170708091206.9293-1-onemda@gmail.com> 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 | 263 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 237 insertions(+), 26 deletions(-) diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index c530015..dbacdda 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. * @@ -54,6 +55,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; @@ -106,12 +111,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 @@ -196,20 +205,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) +{ + uint64_t ret; + av_assert2(n>0 && n<=63); +#ifdef BITSTREAM_READER_LE + ret = s->cache & ((UINT64_C(1) << n) - 1); + s->cache >>= n; +#else + ret = s->cache >> (64 - n); + s->cache <<= n; +#endif + 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 @@ -217,6 +319,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); @@ -227,8 +336,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; @@ -242,31 +353,61 @@ 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) { +#ifdef CACHED_BITSTREAM_READER + register int tmp = 0; +#ifdef BITSTREAM_READER_LE + uint64_t left = 0; +#endif + + av_assert2(n>0 && n<=32); + if (n > s->bits_left) { + n -= s->bits_left; +#ifdef BITSTREAM_READER_LE + left = s->bits_left; +#endif + tmp = get_val(s, s->bits_left); + refill_32(s); + } + +#ifdef BITSTREAM_READER_LE + tmp = get_val(s, n) << left | tmp; +#else + tmp = get_val(s, n) | tmp << n; +#endif + +#else register int tmp; 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; } @@ -278,6 +419,7 @@ static av_always_inline int get_bitsz(GetBitContext *s, int n) return n ? get_bits(s, n) : 0; } +#ifndef CACHED_BITSTREAM_READER static inline unsigned int get_bits_le(GetBitContext *s, int n) { register int tmp; @@ -289,29 +431,56 @@ static inline unsigned int get_bits_le(GetBitContext *s, int 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); + + return get_val(s, 1); +#else unsigned int index = s->index; uint8_t result = s->buffer[index >> 3]; #ifdef BITSTREAM_READER_LE @@ -328,6 +497,7 @@ static inline unsigned int get_bits1(GetBitContext *s) s->index = index; return result; +#endif } static inline unsigned int show_bits1(GetBitContext *s) @@ -348,6 +518,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 { @@ -359,6 +533,7 @@ static inline unsigned int get_bits_long(GetBitContext *s, int n) return ret | get_bits(s, n - 16); #endif } +#endif } /** @@ -442,6 +617,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; } @@ -543,6 +722,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 @@ -554,6 +746,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); @@ -564,6 +774,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)