From patchwork Thu Mar 25 15:49:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26603 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 5A76D44B722 for ; Thu, 25 Mar 2021 17:50:47 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3EF3C68AB81; Thu, 25 Mar 2021 17:50:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C68A668AAFB for ; Thu, 25 Mar 2021 17:50:35 +0200 (EET) Received: by mail-ej1-f46.google.com with SMTP id u9so3707412ejj.7 for ; Thu, 25 Mar 2021 08:50:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=StILkUjP3Ybp3120PLrMtkjmDYFmAtUeAsLrKngHB5M=; b=ZRnlTdn5kUMT67JTMmht9P0u/9sGOEpnLNJMp1QiavUUPZa0kHk2tup1vSUII/jW6G 8PZjw3qrNRSEzBUr2Oaw1FlyyrFa/9KnFUTZMfDshdYhvaE2Q4WRC4fDn48pjwG08tzI xxXH3D2xms/K1tdZfQls5HFIQ+Y7caTVeqmOKvSHUJLfWLE5edaB5+z61u3ZTdbvu/sT 1wkkng5F2OIz3qhc6GrL5q4GjwCLgILomRuLzW6ul9SPcT0LkILIUjfrHKi/A5g3yOD0 CX8SNEOjrPlO3eqfyIrSLUqY6nlutORSr3OSzGD++L5BFJeZ/uNptg+O6ubUblDBTWHr 4P0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=StILkUjP3Ybp3120PLrMtkjmDYFmAtUeAsLrKngHB5M=; b=bQVVx8c0bfHVO7x2RSGwP48pjgSDDQHuTpYIw2wIuXjwHPU56JOciwS3CCWpdKIlwv eHaCbWZUgvR4H06yKeygMcWFg3YodQPlX4JovMlElyMjVoomQbC4t8vcv9MbycQ/8s31 h9Mi5lb8EwpOxWGx28lbcztyBbIVxvVBZ/HIdPyqM87m835xPQwpSoOG26DIUW1dC21S GSMBocdoauB8QFi854hLlAr0GP+OZLj0XAuAKtEIaWUI42PVZRzBB20D+engeLgtv1O0 FOmfpljZkr2MBZernO2jMgA35PsQfYGZS0y3RklDYVjEvWQb1EIaHVRFmyInD3COGYIz kQqg== X-Gm-Message-State: AOAM532K3c3WNGNufJ4uusVaeuiGNm4MIuL09DOSXOpnUrmXy4rMCWAM Cd0FV/R7h7xJ9lqf8R5BFyUmWx+uKaKSpw== X-Google-Smtp-Source: ABdhPJwapCgTHMWgGrOMZuogiCp9lAp/99hMigXAi7WnO7DXnfeSs0CqLG4By4C0JhQf+xyhPpZ2Sg== X-Received: by 2002:a17:906:33d9:: with SMTP id w25mr10672155eja.413.1616687435039; Thu, 25 Mar 2021 08:50:35 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:34 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:50 +0100 Message-Id: <20210325154956.2405162-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/12] avcodec/put_bits: Add functions for amount of bytes written/left 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Often a caller doesn't want the amount of bits written via a PutBitContext, but the amount of bytes. This in particular happens after one has flushed the PutBitContext (e.g. at the end of encoding, when one wants to know the actual packet size). The current way of doing this is with put_bits_count(pb)/8 (or (put_bits_count(pb) + 7)/8). Yet this has some issues: It contains implicit multiplications and divisions by 8 with a cast in between; it obscurs the intent; and it restricts the size of the buffer to (currently) INT_MAX/8 (or to 1/8 of the maximum of whatever put_bits_count() returns), although said restriction is not really necessary for users that don't need a bitcount. Corresponding functions for the amount of bytes left have also been addded. Signed-off-by: Andreas Rheinhardt --- libavcodec/put_bits.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/libavcodec/put_bits.h b/libavcodec/put_bits.h index cd66a82a44..e8bc86a82c 100644 --- a/libavcodec/put_bits.h +++ b/libavcodec/put_bits.h @@ -85,6 +85,26 @@ static inline int put_bits_count(PutBitContext *s) return (s->buf_ptr - s->buf) * 8 + BUF_BITS - s->bit_left; } +/** + * @return the number of bytes output so far; may only be called + * when the PutBitContext is freshly initialized or flushed. + */ +static inline int put_bytes_output(const PutBitContext *s) +{ + av_assert2(s->bit_left == BUF_BITS); + return s->buf_ptr - s->buf; +} + +/** + * @param round_up When set, the number of bits written so far will be + * rounded up to the next byte. + * @return the number of bytes output so far. + */ +static inline int put_bytes_count(const PutBitContext *s, int round_up) +{ + return s->buf_ptr - s->buf + ((BUF_BITS - s->bit_left + (round_up ? 7 : 0)) >> 3); +} + /** * Rebase the bit writer onto a reallocated buffer. * @@ -111,6 +131,16 @@ static inline int put_bits_left(PutBitContext* s) return (s->buf_end - s->buf_ptr) * 8 - BUF_BITS + s->bit_left; } +/** + * @param round_up When set, the number of bits written will be + * rounded up to the next byte. + * @return the number of bytes left. + */ +static inline int put_bytes_left(const PutBitContext *s, int round_up) +{ + return s->buf_end - s->buf_ptr - ((BUF_BITS - s->bit_left + (round_up ? 7 : 0)) >> 3); +} + /** * Pad the end of the output stream with zeros. */