[FFmpeg-devel] [PATCH] get/put_{le,be}_{float,double}

Daniel Verkamp daniel
Thu May 28 00:28:01 CEST 2009


Hi,

Attached is a patch to implement reading and writing of floating-point
numbers in ByteIOContexts.

It assumes that floating-point numbers are stored in IEEE 754 format,
and it uses unions to do type punning; I'd be interested in any
cleaner way to do this, but the current code works (at least on x86 -
testing welcome).

Thanks,
-- Daniel Verkamp
-------------- next part --------------
>From a13d476001b0c84cae274ceddf079707a3bc71a6 Mon Sep 17 00:00:00 2001
From: Daniel Verkamp <daniel at drv.nu>
Date: Wed, 27 May 2009 16:21:22 -0400
Subject: [PATCH 1/2] get/put_{le,be}_{float,double}

---
 libavformat/avio.h    |    8 ++++++++
 libavformat/aviobuf.c |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/libavformat/avio.h b/libavformat/avio.h
index a814759..872dacd 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -234,6 +234,10 @@ void put_be24(ByteIOContext *s, unsigned int val);
 void put_le16(ByteIOContext *s, unsigned int val);
 void put_be16(ByteIOContext *s, unsigned int val);
 void put_tag(ByteIOContext *s, const char *tag);
+void put_le_float(ByteIOContext *s, float val);
+void put_be_float(ByteIOContext *s, float val);
+void put_le_double(ByteIOContext *s, double val);
+void put_be_double(ByteIOContext *s, double val);
 
 void put_strz(ByteIOContext *s, const char *buf);
 
@@ -312,12 +316,16 @@ unsigned int get_le24(ByteIOContext *s);
 unsigned int get_le32(ByteIOContext *s);
 uint64_t get_le64(ByteIOContext *s);
 unsigned int get_le16(ByteIOContext *s);
+float get_le_float(ByteIOContext *s);
+double get_le_double(ByteIOContext *s);
 
 char *get_strz(ByteIOContext *s, char *buf, int maxlen);
 unsigned int get_be16(ByteIOContext *s);
 unsigned int get_be24(ByteIOContext *s);
 unsigned int get_be32(ByteIOContext *s);
 uint64_t get_be64(ByteIOContext *s);
+float get_be_float(ByteIOContext *s);
+double get_be_double(ByteIOContext *s);
 
 uint64_t ff_get_v(ByteIOContext *bc);
 
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index f270139..9e41d2f 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -288,6 +288,22 @@ void put_tag(ByteIOContext *s, const char *tag)
     }
 }
 
+#define PUT_FLOAT(endian, type, bits)                  \
+void put_##endian##_##type(ByteIOContext *s, type val) \
+{                                                      \
+    union {                                            \
+        type f;                                        \
+        uint##bits##_t i;                              \
+    } x;                                               \
+    x.f = val;                                         \
+    put_##endian##bits(s, x.i);                        \
+}
+
+PUT_FLOAT(le, float, 32)
+PUT_FLOAT(be, float, 32)
+PUT_FLOAT(le, double, 64)
+PUT_FLOAT(be, double, 64)
+
 /* Input stream */
 
 static void fill_buffer(ByteIOContext *s)
@@ -515,6 +531,22 @@ uint64_t get_be64(ByteIOContext *s)
     return val;
 }
 
+#define GET_FLOAT(endian, type, bits)        \
+type get_##endian##_##type(ByteIOContext *s) \
+{                                            \
+    union {                                  \
+        uint##bits##_t i;                    \
+        type f;                              \
+    } x;                                     \
+    x.i = get_##endian##bits(s);             \
+    return x.f;                              \
+}
+
+GET_FLOAT(le, float, 32)
+GET_FLOAT(be, float, 32)
+GET_FLOAT(le, double, 64)
+GET_FLOAT(be, double, 64)
+
 uint64_t ff_get_v(ByteIOContext *bc){
     uint64_t val = 0;
     int tmp;
-- 
1.6.3.1



More information about the ffmpeg-devel mailing list