aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat
diff options
context:
space:
mode:
authorJames Almer <jamrial@gmail.com>2023-11-25 11:07:34 -0300
committerJames Almer <jamrial@gmail.com>2023-12-18 15:19:36 -0300
commit7f9308380f0f2e299ed8d2295b026cdd4ce057d2 (patch)
treebebf8fe24fc82f6b9cb84aafbf4e7ddeb55f6a75 /libavformat
parent0cc0d8c0b579f0e47052a717568dc2faa09d4e60 (diff)
downloadffmpeg-7f9308380f0f2e299ed8d2295b026cdd4ce057d2.tar.gz
avformat/aviobuf: add ffio_read_leb() and ffio_write_leb()
Signed-off-by: James Almer <jamrial@gmail.com>
Diffstat (limited to 'libavformat')
-rw-r--r--libavformat/avio_internal.h10
-rw-r--r--libavformat/aviobuf.c33
2 files changed, 43 insertions, 0 deletions
diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h
index bd58499b64..f2e4ff30cb 100644
--- a/libavformat/avio_internal.h
+++ b/libavformat/avio_internal.h
@@ -147,6 +147,16 @@ int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_siz
uint64_t ffio_read_varlen(AVIOContext *bc);
/**
+ * Read a unsigned integer coded as a variable number of up to eight
+ * little-endian bytes, where the MSB in a byte signals another byte
+ * must be read.
+ * All coded bytes are read, but values > UINT_MAX are truncated.
+ */
+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.
* @return number of bytes read or AVERROR
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 2899c75521..5a329ce465 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -971,6 +971,39 @@ 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 <= 4)
+ leb |= bits << (i * 7);
+ if (++i == 8)
+ break;
+ } 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;