diff options
author | Andreas Rheinhardt <andreas.rheinhardt@gmail.com> | 2020-04-15 04:05:09 +0200 |
---|---|---|
committer | Andreas Rheinhardt <andreas.rheinhardt@gmail.com> | 2020-04-20 21:30:46 +0200 |
commit | 112afaccdfa56e446527f0303f3e1277bc34b500 (patch) | |
tree | c36ea6da228ba3a70193fd6f0ee7321a9d5ad2c8 /libavformat | |
parent | 40d038a63531bac12fca50e3b0f6f55037733671 (diff) | |
download | ffmpeg-112afaccdfa56e446527f0303f3e1277bc34b500.tar.gz |
avformat/matroskaenc: Refactor writing EBML lengths
This commit factors the ability to write ordinary EBML numbers out of
the functions for writing EBML lengths. This is in preparation for
future commits.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Diffstat (limited to 'libavformat')
-rw-r--r-- | libavformat/matroskaenc.c | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 3408b49a81..d3c6a7ea9b 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -190,20 +190,39 @@ static void put_ebml_size_unknown(AVIOContext *pb, int bytes) } /** - * Calculate how many bytes are needed to represent the length field - * of an EBML element whose payload has a given length. + * Returns how many bytes are needed to represent a number + * as EBML variable length integer. */ -static int ebml_length_size(uint64_t length) +static int ebml_num_size(uint64_t num) { int bytes = 0; - length++; do { bytes++; - } while (length >>= 7); + } while (num >>= 7); return bytes; } /** + * Calculate how many bytes are needed to represent the length field + * of an EBML element whose payload has a given length. + */ +static int ebml_length_size(uint64_t length) +{ + return ebml_num_size(length + 1); +} + +/** + * Write a number as EBML variable length integer on `bytes` bytes. + * `bytes` is taken literally without checking. + */ +static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes) +{ + num |= 1ULL << bytes * 7; + for (int i = bytes - 1; i >= 0; i--) + avio_w8(pb, (uint8_t)(num >> i * 8)); +} + +/** * Write a length as EBML variable length integer. * * @param bytes The number of bytes that need to be used to write the number. @@ -211,7 +230,7 @@ static int ebml_length_size(uint64_t length) */ static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes) { - int i, needed_bytes = ebml_length_size(length); + int needed_bytes = ebml_length_size(length); // sizes larger than this are currently undefined in EBML av_assert0(length < (1ULL << 56) - 1); @@ -221,10 +240,7 @@ static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes) // The bytes needed to write the given size must not exceed // the bytes that we ought to use. av_assert0(bytes >= needed_bytes); - - length |= 1ULL << bytes * 7; - for (i = bytes - 1; i >= 0; i--) - avio_w8(pb, (uint8_t)(length >> i * 8)); + put_ebml_num(pb, length, bytes); } /** |