summaryrefslogtreecommitdiff
path: root/libavcodec/put_bits.h
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2021-03-25 09:05:49 +0100
committerAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2021-03-30 12:36:32 +0200
commit11ff9cb5e976e87ec345e6f4856f62b86d6cb0b3 (patch)
treedc21b6c01a9a3a9270664cbb5b6a9a078fdbc306 /libavcodec/put_bits.h
parente48f18e2d5e492ef41139d55a3698044fc245eca (diff)
downloadffmpeg-11ff9cb5e976e87ec345e6f4856f62b86d6cb0b3.tar.gz
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@gmail.com>
Diffstat (limited to 'libavcodec/put_bits.h')
-rw-r--r--libavcodec/put_bits.h30
1 files changed, 30 insertions, 0 deletions
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
@@ -86,6 +86,26 @@ static inline int put_bits_count(PutBitContext *s)
}
/**
+ * @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.
*
* @param buffer the buffer where to put bits
@@ -112,6 +132,16 @@ static inline int put_bits_left(PutBitContext* s)
}
/**
+ * @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.
*/
static inline void flush_put_bits(PutBitContext *s)