[FFmpeg-cvslog] avcodec/put_bits: Add functions for amount of bytes written/left

Andreas Rheinhardt git at videolan.org
Tue Mar 30 16:49:33 EEST 2021


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Thu Mar 25 09:05:49 2021 +0100| [11ff9cb5e976e87ec345e6f4856f62b86d6cb0b3] | committer: Andreas Rheinhardt

avcodec/put_bits: Add functions for amount of bytes written/left

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 at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=11ff9cb5e976e87ec345e6f4856f62b86d6cb0b3
---

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



More information about the ffmpeg-cvslog mailing list