[FFmpeg-devel] [PATCH 3/9] avformat/aviobuf: add ffio_read_leb() and ffio_write_leb()
James Almer
jamrial at gmail.com
Sun Nov 26 03:28:52 EET 2023
Signed-off-by: James Almer <jamrial at gmail.com>
---
libavformat/avio_internal.h | 4 ++++
libavformat/aviobuf.c | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+)
diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h
index bd58499b64..6b6cd6e8b3 100644
--- a/libavformat/avio_internal.h
+++ b/libavformat/avio_internal.h
@@ -146,6 +146,10 @@ int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_siz
uint64_t ffio_read_varlen(AVIOContext *bc);
+unsigned int ffio_read_leb(AVIOContext *s);
+
+void ffio_write_leb(AVIOContext *s, unsigned val);
+
/**
* Read size bytes from AVIOContext into buf.
* Check that exactly size bytes have been read.
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 2899c75521..cdd1528155 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -971,6 +971,43 @@ uint64_t ffio_read_varlen(AVIOContext *bc){
return val;
}
+unsigned int ffio_read_leb(AVIOContext *s) {
+ int more, i = 0;
+ unsigned leb = 0;
+
+ do {
+ int byte = avio_r8(s);
+ unsigned bits = byte & 0x7f;
+ more = byte & 0x80;
+ if (i <= 3 || (i == 4 && bits < (1 << 4)))
+ leb |= bits << (i * 7);
+ else if (bits) { // leb > UINT_MAX
+ leb |= (bits & 0xF) << (i * 7);
+ break;
+ }
+ if (++i == 8 && more)
+ break; // invalid leb
+ } while (more);
+
+ return leb;
+}
+
+void ffio_write_leb(AVIOContext *s, unsigned val)
+{
+ int len;
+ uint8_t byte;
+
+ len = (av_log2(val) + 7) / 7;
+
+ for (int i = 0; i < len; i++) {
+ byte = val >> (7 * i) & 0x7f;
+ if (i < len - 1)
+ byte |= 0x80;
+
+ avio_w8(s, byte);
+ }
+}
+
int ffio_fdopen(AVIOContext **s, URLContext *h)
{
uint8_t *buffer = NULL;
--
2.42.1
More information about the ffmpeg-devel
mailing list