diff mbox series

[FFmpeg-devel,06/12] avcodec/put_bits: Add functions for amount of bytes written/left

Message ID 20210325154956.2405162-6-andreas.rheinhardt@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,01/12] avcodec/vorbisenc: Remove always-false check
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt March 25, 2021, 3:49 p.m. UTC
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 <andreas.rheinhardt@gmail.com>
---
 libavcodec/put_bits.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
diff mbox series

Patch

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.
  */