From 6d87d92d6be3d9aeeda162e639ec9e25d2e0f06d Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 9 Aug 2021 23:13:35 +0200 Subject: [PATCH 1/1] mp4: Use read/write functions from portable_io.h. This removes quite a bit of ugly code. In particular, atom_get_size() is completely unnecessary and can be removed. Since there is no function in portable_io.h to read a 24 bit integer, we have to add read_u24_be(). --- mp4.c | 89 ++++++++------------------------------------------- portable_io.h | 5 +++ 2 files changed, 19 insertions(+), 75 deletions(-) diff --git a/mp4.c b/mp4.c index aab39a1e..af491fc2 100644 --- a/mp4.c +++ b/mp4.c @@ -8,6 +8,7 @@ #include #include "para.h" +#include "portable_io.h" #include "string.h" #include "mp4.h" @@ -30,34 +31,12 @@ static int32_t read_data(mp4ff_t * f, void *data, uint32_t size) return result; } -/* parse atom header size */ -static int32_t atom_get_size(const int8_t * data) -{ - uint32_t result; - uint32_t a, b, c, d; - - a = (uint8_t) data[0]; - b = (uint8_t) data[1]; - c = (uint8_t) data[2]; - d = (uint8_t) data[3]; - - result = (a << 24) | (b << 16) | (c << 8) | d; - return (int32_t) result; -} - static uint64_t read_int64(mp4ff_t * f) { uint8_t data[8]; - uint64_t result = 0; - int8_t i; read_data(f, data, 8); - - for (i = 0; i < 8; i++) { - result |= ((uint64_t) data[i]) << ((7 - i) * 8); - } - - return result; + return read_u64_be(data); } /* comnapre 2 atom names, returns 1 for equal, 0 for unequal */ @@ -325,7 +304,7 @@ static uint64_t atom_read_header(mp4ff_t * f, uint8_t * atom_type, if (ret != 8) return 0; - size = atom_get_size(atom_header); + size = read_u32_be(atom_header); *header_size = 8; /* check for 64 bit atom size */ @@ -393,33 +372,18 @@ static uint8_t read_char(mp4ff_t * f) static uint32_t read_int24(mp4ff_t * f) { - uint32_t result; - uint32_t a, b, c; int8_t data[4]; read_data(f, data, 3); - a = (uint8_t) data[0]; - b = (uint8_t) data[1]; - c = (uint8_t) data[2]; - - result = (a << 16) | (b << 8) | c; - return (uint32_t) result; + return read_u24_be(data); } static uint32_t read_int32(mp4ff_t * f) { - uint32_t result; - uint32_t a, b, c, d; int8_t data[4]; read_data(f, data, 4); - a = (uint8_t) data[0]; - b = (uint8_t) data[1]; - c = (uint8_t) data[2]; - d = (uint8_t) data[3]; - - result = (a << 24) | (b << 16) | (c << 8) | d; - return (uint32_t) result; + return read_u32_be(data); } static int32_t read_stsz(mp4ff_t * f) @@ -546,16 +510,10 @@ static int32_t read_stco(mp4ff_t * f) static uint16_t read_int16(mp4ff_t * f) { - uint32_t result; - uint32_t a, b; int8_t data[2]; read_data(f, data, 2); - a = (uint8_t) data[0]; - b = (uint8_t) data[1]; - - result = (a << 8) | b; - return (uint16_t) result; + return read_u16_be(data); } static uint32_t read_mp4_descr_length(mp4ff_t * f) @@ -1584,14 +1542,16 @@ static unsigned membuffer_write_atom_name(membuffer * buf, const char *data) static unsigned membuffer_write_int16(membuffer * buf, uint16_t data) { - uint8_t temp[2] = { (uint8_t) (data >> 8), (uint8_t) data }; + uint8_t temp[2]; + + write_u16_be(temp, data); return membuffer_write(buf, temp, 2); } static unsigned membuffer_write_int32(membuffer * buf, uint32_t data) { - uint8_t temp[4] = { (uint8_t) (data >> 24), (uint8_t) (data >> 16), - (uint8_t) (data >> 8), (uint8_t) data }; + uint8_t temp[4]; + write_u32_be(temp, data); return membuffer_write(buf, temp, 4); } @@ -1935,18 +1895,7 @@ uint32_t * out_size) static uint32_t fix_byte_order_32(uint32_t src) { - uint32_t result; - uint32_t a, b, c, d; - int8_t data[4]; - - memcpy(data, &src, sizeof (src)); - a = (uint8_t) data[0]; - b = (uint8_t) data[1]; - c = (uint8_t) data[2]; - d = (uint8_t) data[3]; - - result = (a << 24) | (b << 16) | (c << 8) | d; - return (uint32_t) result; + return read_u32_be(&src); } static uint32_t modify_moov(mp4ff_t * f, const mp4ff_metadata_t * data, @@ -2076,19 +2025,9 @@ static int32_t write_data(mp4ff_t * f, void *data, uint32_t size) static int32_t write_int32(mp4ff_t * f, const uint32_t data) { - uint32_t result; - uint32_t a, b, c, d; int8_t temp[4]; - - *(uint32_t *) temp = data; - a = (uint8_t) temp[0]; - b = (uint8_t) temp[1]; - c = (uint8_t) temp[2]; - d = (uint8_t) temp[3]; - - result = (a << 24) | (b << 16) | (c << 8) | d; - - return write_data(f, (uint8_t *) & result, sizeof (result)); + write_u32_be(temp, data); + return write_data(f, temp, sizeof(temp)); } static int32_t truncate_stream(mp4ff_t * f) diff --git a/portable_io.h b/portable_io.h index fdd4165d..81dfcf57 100644 --- a/portable_io.h +++ b/portable_io.h @@ -58,6 +58,11 @@ static inline uint32_t read_u32_be(const void *buf) return read_portable_be(32, buf); } +static inline uint32_t read_u24_be(const void *buf) +{ + return read_portable_be(24, buf); +} + static inline uint16_t read_u16_be(const void *buf) { return read_portable_be(16, buf); -- 2.39.2