diff mbox series

[FFmpeg-devel,2/9] avcodec/get_bits: add get_leb()

Message ID 20231126012858.40388-3-jamrial@gmail.com
State New
Headers show
Series avformat: introduce AVStreamGroup | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

James Almer Nov. 26, 2023, 1:28 a.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/bitstream.h          |  2 ++
 libavcodec/bitstream_template.h | 22 ++++++++++++++++++++++
 libavcodec/get_bits.h           | 23 +++++++++++++++++++++++
 3 files changed, 47 insertions(+)

Comments

Leo Izen Nov. 26, 2023, 1:40 p.m. UTC | #1
On 11/25/23 20:28, James Almer wrote:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>   libavcodec/bitstream.h          |  2 ++
>   libavcodec/bitstream_template.h | 22 ++++++++++++++++++++++
>   libavcodec/get_bits.h           | 23 +++++++++++++++++++++++
>   3 files changed, 47 insertions(+)
> 

Out of curiosity, why do this (and the avio version) have to be global? 
What are they used for other than the iamf demuxer? Would it make sense 
to have a static inline read_leb function defined in iamf.c?

- Leo Izen (Traneptora)
James Almer Nov. 26, 2023, 1:43 p.m. UTC | #2
On 11/26/2023 10:40 AM, Leo Izen wrote:
> On 11/25/23 20:28, James Almer wrote:
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>   libavcodec/bitstream.h          |  2 ++
>>   libavcodec/bitstream_template.h | 22 ++++++++++++++++++++++
>>   libavcodec/get_bits.h           | 23 +++++++++++++++++++++++
>>   3 files changed, 47 insertions(+)
>>
> 
> Out of curiosity, why do this (and the avio version) have to be global? 
> What are they used for other than the iamf demuxer? Would it make sense 
> to have a static inline read_leb function defined in iamf.c?

They can be used by the av1 modules too, and potentially other formats 
in the future using the OBU encapsulation.
Anton Khirnov Nov. 30, 2023, 10:40 a.m. UTC | #3
> add get_leb()

Do you expect people to understand what this means?
Paul B Mahol Nov. 30, 2023, 12:49 p.m. UTC | #4
On Thu, Nov 30, 2023 at 11:40 AM Anton Khirnov <anton@khirnov.net> wrote:

> > add get_leb()
>
> Do you expect people to understand what this means?
>

get_leb() : get little-endian bits.


> --
> Anton Khirnov
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
James Almer Nov. 30, 2023, 1:08 p.m. UTC | #5
On 11/30/2023 7:40 AM, Anton Khirnov wrote:
>> add get_leb()
> 
> Do you expect people to understand what this means?

Will add "Read an unsigned integer coded as a variable number of 
little-endian bytes".
diff mbox series

Patch

diff --git a/libavcodec/bitstream.h b/libavcodec/bitstream.h
index 35b7873b9c..17f8a5da83 100644
--- a/libavcodec/bitstream.h
+++ b/libavcodec/bitstream.h
@@ -103,6 +103,7 @@ 
 # define bits_apply_sign    bits_apply_sign_le
 # define bits_read_vlc      bits_read_vlc_le
 # define bits_read_vlc_multi bits_read_vlc_multi_le
+# define bits_read_leb      bits_read_leb_le
 
 #elif defined(BITS_DEFAULT_BE)
 
@@ -132,6 +133,7 @@ 
 # define bits_apply_sign    bits_apply_sign_be
 # define bits_read_vlc      bits_read_vlc_be
 # define bits_read_vlc_multi bits_read_vlc_multi_be
+# define bits_read_leb      bits_read_leb_be
 
 #endif
 
diff --git a/libavcodec/bitstream_template.h b/libavcodec/bitstream_template.h
index 4f3d07275f..86cbab288e 100644
--- a/libavcodec/bitstream_template.h
+++ b/libavcodec/bitstream_template.h
@@ -562,6 +562,28 @@  static inline int BS_FUNC(read_vlc_multi)(BSCTX *bc, uint8_t dst[8],
     return ret;
 }
 
+static inline unsigned BS_FUNC(read_leb)(BSCTX *bc) {
+    int more, i = 0;
+    unsigned leb = 0;
+
+    do {
+        unsigned bits;
+        int byte = BS_FUNC(read)(bc, 8);
+        more = byte & 0x80;
+        bits = byte & 0x7f;
+        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;
+}
+
 #undef BSCTX
 #undef BS_FUNC
 #undef BS_JOIN3
diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h
index cfcf97c021..cf9d5129b5 100644
--- a/libavcodec/get_bits.h
+++ b/libavcodec/get_bits.h
@@ -94,6 +94,7 @@  typedef BitstreamContext GetBitContext;
 #define align_get_bits      bits_align
 #define get_vlc2            bits_read_vlc
 #define get_vlc_multi       bits_read_vlc_multi
+#define get_leb             bits_read_leb
 
 #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size)
 #define get_bits_le(s, n)                       bits_read_le((BitstreamContextLE*)s, n)
@@ -710,6 +711,28 @@  static inline int skip_1stop_8data_bits(GetBitContext *gb)
     return 0;
 }
 
+static inline unsigned get_leb(GetBitContext *gb) {
+    int more, i = 0;
+    unsigned leb = 0;
+
+    do {
+        unsigned bits;
+        int byte = get_bits(gb, 8);
+        more = byte & 0x80;
+        bits = byte & 0x7f;
+        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;
+}
+
 #endif // CACHED_BITSTREAM_READER
 
 #endif /* AVCODEC_GET_BITS_H */