]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
portable_io.h: Provide big-endian versions and use them for aac.
authorAndre Noll <maan@systemlinux.org>
Mon, 16 Dec 2013 21:18:27 +0000 (22:18 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Tue, 20 Dec 2016 15:53:52 +0000 (16:53 +0100)
The aac audio format handler code contains some instances that read a
big-endian encoded 32 or 64 bit number from a buffer. While for the
32 bit case there is a helper function aac_read_int32(), the 64 bit
case is open-coded.

We already have similar functions for the conversion of little-endian
entities. This patch adds their big endian counterparts as inline
functions to portable_io.h and changes the callers to use those.

The patch also gets rid of two fprintf() statements in write_portable()
which were commented out for ages.

aac.h
aac_afh.c
aac_common.c
portable_io.h

diff --git a/aac.h b/aac.h
index eb6e2bed29618952bd6ac0e15135e43eb8a7eccb..eeed252837570b75f055743f87664a1d8b07125d 100644 (file)
--- a/aac.h
+++ b/aac.h
@@ -12,9 +12,3 @@ NeAACDecHandle aac_open(void);
 int aac_find_esds(char *buf, size_t buflen, size_t *skip,
                unsigned long *decoder_length);
 ssize_t aac_find_entry_point(char *buf, size_t buflen, size_t *skip);
-
-static inline unsigned aac_read_int32(char *buf)
-{
-       uint8_t *d = (uint8_t*)buf;
-       return (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
-}
index e94f5b421f848c02eca86a5061fa46878b07e587..a30be96f4f9da70b841e26fd16a3044ebc1df8ac 100644 (file)
--- a/aac_afh.c
+++ b/aac_afh.c
@@ -15,6 +15,7 @@
 
 #include "para.h"
 #include "error.h"
+#include "portable_io.h"
 #include "afh.h"
 #include "string.h"
 #include "aac.h"
@@ -32,10 +33,10 @@ static int aac_find_stsz(char *buf, size_t buflen, off_t *skip)
                        continue;
                PARA_DEBUG_LOG("found stsz@%d\n", i);
                i += 8;
-               sample_size = aac_read_int32(buf + i);
+               sample_size = read_u32_be(buf + i);
                PARA_DEBUG_LOG("sample size: %d\n", sample_size);
                i += 4;
-               sample_count = aac_read_int32(buf + i);
+               sample_count = read_u32_be(buf + i);
                i += 4;
                PARA_DEBUG_LOG("sample count: %d\n", sample_count);
                *skip = i;
@@ -51,8 +52,7 @@ static int atom_cmp(const char *buf1, const char *buf2)
 
 static int read_atom_header(char *buf, uint64_t *subsize, char type[5])
 {
-       int i;
-       uint64_t size = aac_read_int32(buf);
+       uint64_t size = read_u32_be(buf);
 
        memcpy(type, buf + 4, 4);
        type[4] = '\0';
@@ -64,8 +64,7 @@ static int read_atom_header(char *buf, uint64_t *subsize, char type[5])
        }
        buf += 4;
        size = 0;
-       for (i = 0; i < 8; i++)
-               size |= ((uint64_t)buf[i]) << ((7 - i) * 8);
+       size = read_u64_be(buf);
        *subsize = size;
        return 16;
 }
@@ -168,7 +167,7 @@ static ssize_t aac_compute_chunk_table(struct afh_info *afhi,
        for (i = 1; i <= afhi->chunks_total; i++) {
                if (skip + 4 > numbytes)
                        break;
-               sum += aac_read_int32(map + skip);
+               sum += read_u32_be(map + skip);
                afhi->chunk_table[i] = sum;
                skip += 4;
 //             if (i < 10 || i + 10 > afhi->chunks_total)
index 8fc9523c28736e8e3b4add4551fff1ff38db23d3..70a9d77d51b320ee8511f394186528164362273a 100644 (file)
@@ -13,6 +13,7 @@
 #include "para.h"
 #include "aac.h"
 #include "error.h"
+#include "portable_io.h"
 
 /**
  * Get a new libfaad decoder handle.
@@ -120,7 +121,7 @@ ssize_t aac_find_entry_point(char *buf, size_t buflen, size_t *skip)
                        continue;
                PARA_INFO_LOG("found stco@%zu\n", i);
                i += 12;
-               ret = aac_read_int32(buf + i); /* first offset */
+               ret = read_u32_be(buf + i); /* first offset */
                i += 4;
                PARA_INFO_LOG("entry point: %zd\n", ret);
                *skip = i;
index f1a38929043ec3b1ed3993f05ca49d7eb1b52574..4e10c2e3386d1511ac6f71bbfc47210d02852e3a 100644 (file)
@@ -4,7 +4,7 @@
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file portable_io.h Inline functions for endian-independent binary IO. */
+/** \file portable_io.h Inline functions for binary IO. */
 
 static inline uint64_t read_portable(unsigned bits, const char *buf)
 {
@@ -18,6 +18,18 @@ static inline uint64_t read_portable(unsigned bits, const char *buf)
        return ret;
 }
 
+static inline uint64_t read_portable_be(unsigned bits, const char *buf)
+{
+       uint64_t ret = 0;
+       int i, num_bytes = bits / 8;
+
+       for (i = 0; i < num_bytes; i++) {
+               unsigned char c = buf[i];
+               ret += ((uint64_t)c << (8 * (num_bytes - i - 1)));
+       }
+       return ret;
+}
+
 static inline uint64_t read_u64(const char *buf)
 {
        return read_portable(64, buf);
@@ -38,13 +50,35 @@ static inline uint8_t read_u8(const char *buf)
        return read_portable(8, buf);
 }
 
+static inline uint64_t read_u64_be(const char *buf)
+{
+       return read_portable_be(64, buf);
+}
+
+static inline uint32_t read_u32_be(const char *buf)
+{
+       return read_portable_be(32, buf);
+}
+
+static inline uint16_t read_u16_be(const char *buf)
+{
+       return read_portable_be(16, buf);
+}
+
 static inline void write_portable(unsigned bits, char *buf, uint64_t val)
 {
        int i, num_bytes = bits / 8;
-//     fprintf(stderr, "val: %lu\n", val);
        for (i = 0; i < num_bytes; i++) {
                buf[i] = val & 0xff;
-//             fprintf(stderr, "buf[%d]=%x\n", i, buf[i]);
+               val = val >> 8;
+       }
+}
+
+static inline void write_portable_be(unsigned bits, char *buf, uint64_t val)
+{
+       int i, num_bytes = bits / 8;
+       for (i = 0; i < num_bytes; i++) {
+               buf[num_bytes - i - 1] = val & 0xff;
                val = val >> 8;
        }
 }
@@ -68,3 +102,18 @@ static inline void write_u8(char *buf, uint8_t val)
 {
        write_portable(8, buf, (uint64_t) val);
 }
+
+static inline void write_u64_be(char *buf, uint64_t val)
+{
+       write_portable_be(64, buf, val);
+}
+
+static inline void write_u32_be(char *buf, uint32_t val)
+{
+       write_portable_be(32, buf, (uint64_t) val);
+}
+
+static inline void write_u16_be(char *buf, uint16_t val)
+{
+       write_portable_be(16, buf, (uint64_t) val);
+}