diff --git a/libavcodec/av1_parse.h b/libavcodec/av1_parse.h index d0abd7ac7c..2b8cce4835 100644 --- a/libavcodec/av1_parse.h +++ b/libavcodec/av1_parse.h @@ -30,6 +30,7 @@ #include "av1.h" #include "get_bits.h" +#include "leb.h" // OBU header fields + max leb128 length #define MAX_OBU_HEADER_SIZE (2 + 8) @@ -88,19 +89,6 @@ int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length, */ void ff_av1_packet_uninit(AV1Packet *pkt); -static inline int64_t leb128(GetBitContext *gb) { - int64_t ret = 0; - int i; - - for (i = 0; i < 8; i++) { - int byte = get_bits(gb, 8); - ret |= (int64_t)(byte & 0x7f) << (i * 7); - if (!(byte & 0x80)) - break; - } - return ret; -} - static inline int parse_obu_header(const uint8_t *buf, int buf_size, int64_t *obu_size, int *start_pos, int *type, int *temporal_id, int *spatial_id) @@ -129,7 +117,7 @@ static inline int parse_obu_header(const uint8_t *buf, int buf_size, *temporal_id = *spatial_id = 0; } - *obu_size = has_size_flag ? leb128(&gb) + *obu_size = has_size_flag ? get_leb128(&gb) : buf_size - 1 - extension_flag; if (get_bits_left(&gb) < 0) diff --git a/libavcodec/bitstream.h b/libavcodec/bitstream.h index 17f8a5da83..35b7873b9c 100644 --- a/libavcodec/bitstream.h +++ b/libavcodec/bitstream.h @@ -103,7 +103,6 @@ # 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) @@ -133,7 +132,6 @@ # 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 4c7101632f..4f3d07275f 100644 --- a/libavcodec/bitstream_template.h +++ b/libavcodec/bitstream_template.h @@ -562,29 +562,6 @@ static inline int BS_FUNC(read_vlc_multi)(BSCTX *bc, uint8_t dst[8], return ret; } -/** - * 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. - * Values > UINT_MAX are truncated, but all coded bits are read. - */ -static inline unsigned BS_FUNC(read_leb)(BSCTX *bc) { - int more, i = 0; - unsigned leb = 0; - - do { - int byte = BS_FUNC(read)(bc, 8); - unsigned bits = byte & 0x7f; - more = byte & 0x80; - if (i <= 4) - leb |= bits << (i * 7); - if (++i == 8) - break; - } while (more); - - return leb; -} - #undef BSCTX #undef BS_FUNC #undef BS_JOIN3 diff --git a/libavcodec/bsf/extract_extradata.c b/libavcodec/bsf/extract_extradata.c index baa629295f..5d5d80c90f 100644 --- a/libavcodec/bsf/extract_extradata.c +++ b/libavcodec/bsf/extract_extradata.c @@ -68,7 +68,7 @@ static int metadata_is_global(const AV1OBU *obu) if (init_get_bits(&gb, obu->data, obu->size_bits) < 0) return 0; - metadata_type = leb128(&gb); + metadata_type = get_leb(&gb); return val_in_array(metadata_obu_types, FF_ARRAY_ELEMS(metadata_obu_types), metadata_type); diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index 9e19d2a439..cfcf97c021 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -94,7 +94,6 @@ 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) @@ -711,29 +710,6 @@ static inline int skip_1stop_8data_bits(GetBitContext *gb) return 0; } -/** - * 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 bits are read, but values > UINT_MAX are truncated. - */ -static inline unsigned get_leb(GetBitContext *s) { - int more, i = 0; - unsigned leb = 0; - - do { - int byte = get_bits(s, 8); - unsigned bits = byte & 0x7f; - more = byte & 0x80; - if (i <= 4) - leb |= bits << (i * 7); - if (++i == 8) - break; - } while (more); - - return leb; -} - #endif // CACHED_BITSTREAM_READER #endif /* AVCODEC_GET_BITS_H */ diff --git a/libavcodec/leb.h b/libavcodec/leb.h new file mode 100644 index 0000000000..5159c434b1 --- /dev/null +++ b/libavcodec/leb.h @@ -0,0 +1,70 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * leb128 handling implementations + */ + +#ifndef AVCODEC_LEB_H +#define AVCODEC_LEB_H + +#include "get_bits.h" + +/** + * 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 bits are read, but values > UINT_MAX are truncated. + */ +static inline unsigned get_leb(GetBitContext *s) { + int more, i = 0; + unsigned leb = 0; + + do { + int byte = get_bits(s, 8); + unsigned bits = byte & 0x7f; + more = byte & 0x80; + if (i <= 4) + leb |= bits << (i * 7); + if (++i == 8) + break; + } while (more); + + return leb; +} + +/** + * 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. + */ +static inline int64_t get_leb128(GetBitContext *gb) { + int64_t ret = 0; + + for (int i = 0; i < 8; i++) { + int byte = get_bits(gb, 8); + ret |= (int64_t)(byte & 0x7f) << (i * 7); + if (!(byte & 0x80)) + break; + } + + return ret; +} + +#endif /* AVCODEC_LEB_H */ diff --git a/libavformat/av1dec.c b/libavformat/av1dec.c index 8a06445958..c4542a5cbe 100644 --- a/libavformat/av1dec.c +++ b/libavformat/av1dec.c @@ -325,7 +325,7 @@ static int read_obu_with_size(const uint8_t *buf, int buf_size, int64_t *obu_siz skip_bits(&gb, 3); // extension_header_reserved_3bits } - *obu_size = leb128(&gb); + *obu_size = get_leb128(&gb); if (*obu_size > INT_MAX) return AVERROR_INVALIDDATA; diff --git a/libavformat/iamf_parse.c b/libavformat/iamf_parse.c index 32976e6788..a6443f4f3d 100644 --- a/libavformat/iamf_parse.c +++ b/libavformat/iamf_parse.c @@ -26,6 +26,7 @@ #include "libavutil/log.h" #include "libavcodec/get_bits.h" #include "libavcodec/flac.h" +#include "libavcodec/leb.h" #include "libavcodec/mpeg4audio.h" #include "libavcodec/put_bits.h" #include "avio_internal.h"