Browse Source

Improve serializer and add array serializer

The serializer code is meant to be used as a means of reading/writing
data from any arbitrary type of input/output.

The array output serializer makes it so we can stream data to a dynamic
array on the fly.
jp9000 12 years ago
parent
commit
d42ff7f0dd
3 changed files with 151 additions and 32 deletions
  1. 45 0
      libobs/util/array-serializer.c
  2. 28 0
      libobs/util/array-serializer.h
  3. 78 32
      libobs/util/serializer.h

+ 45 - 0
libobs/util/array-serializer.c

@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2014 Hugh Bailey <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "darray.h"
+#include "array-serializer.h"
+
+static size_t array_output_write(void *param, const void *data, size_t size)
+{
+	struct array_output_data *output = param;
+	da_push_back_array(output->bytes, data, size);
+	return size;
+}
+
+static uint64_t array_output_get_pos(void *param)
+{
+	struct array_output_data *data = param;
+	return data->bytes.num;
+}
+
+void array_output_serializer_init(struct serializer *s,
+		struct array_output_data *data)
+{
+	memset(data, 0, sizeof(struct array_output_data));
+	s->data    = data;
+	s->write   = array_output_write;
+	s->get_pos = array_output_get_pos;
+}
+
+void array_output_serializer_free(struct array_output_data *data)
+{
+	da_free(data->bytes);
+}

+ 28 - 0
libobs/util/array-serializer.h

@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2014 Hugh Bailey <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#pragma once
+
+#include "serializer.h"
+#include "darray.h"
+
+struct array_output_data {
+	DARRAY(uint8_t) bytes;
+};
+
+EXPORT void array_output_serializer_init(struct serializer *s,
+		struct array_output_data *data);
+EXPORT void array_output_serializer_free(struct array_output_data *data);

+ 78 - 32
libobs/util/serializer.h

@@ -16,11 +16,11 @@
 
 #pragma once
 
+#include "c99defs.h"
+
 /*
  *   General programmable serialization functions.  (A shared interface to
  * various reading/writing to/from different inputs/outputs)
- *
- * TODO: Not currently implemented
  */
 
 #ifdef __cplusplus
@@ -34,16 +34,37 @@ enum serialize_seek_type {
 };
 
 struct serializer {
-	void *param;
-	size_t (*serialize)(struct serializer, void *, size_t);
-	uint64_t (*seek)(struct serializer, int64_t, enum serialize_seek_type);
-	uint64_t (*getpos)(struct serializer);
+	void     *data;
+
+	size_t   (*read)(void *, void *, size_t);
+	size_t   (*write)(void *, const void *, size_t);
+	uint64_t (*seek)(void *, int64_t, enum serialize_seek_type);
+	uint64_t (*get_pos)(void *);
 };
 
+static inline size_t s_read(struct serializer *s, void *data, size_t size)
+{
+	if (s && s->read && data && size)
+		return s->read(s->data, (void*)data, size);
+	return 0;
+}
+
+static inline size_t s_write(struct serializer *s, const void *data,
+		size_t size)
+{
+	if (s && s->write && data && size)
+		return s->write(s->data, (void*)data, size);
+	return 0;
+}
+
 static inline size_t serialize(struct serializer *s, void *data, size_t len)
 {
-	if (s->serialize)
-		return s->serialize(s, data, len);
+	if (s) {
+		if (s->write)
+			return s->write(s, data, len);
+		else if (s->read)
+			return s->read(s, data, len);
+	}
 
 	return 0;
 }
@@ -51,66 +72,91 @@ static inline size_t serialize(struct serializer *s, void *data, size_t len)
 static inline uint64_t serializer_seek(struct serializer *s, int64_t offset,
 		enum serialize_seek_type seek_type)
 {
-	if (s->seek)
+	if (s && s->seek)
 		return s->seek(s, offset, seek_type);
 	return 0;
 }
 
-static inline uint64_t serializer_getpos(struct serializer *s)
+static inline uint64_t serializer_get_pos(struct serializer *s)
 {
-	if (s->getpos)
-		return s->getpos(s);
+	if (s && s->get_pos)
+		return s->get_pos(s);
 	return 0;
 }
 
-static inline void serializer_write_u8(struct serializer *s, uint8_t u8)
+/* formatted this to be similar to the AVIO layout that ffmpeg uses */
+
+static inline void s_w8(struct serializer *s, uint8_t u8)
+{
+	s_write(s, &u8, sizeof(uint8_t));
+}
+
+static inline void s_wl16(struct serializer *s, uint16_t u16)
+{
+	s_w8(s, (uint8_t)u16);
+	s_w8(s, u16 >> 8);
+}
+
+static inline void s_wl24(struct serializer *s, uint32_t u24)
+{
+	s_w8  (s, (uint8_t)u24);
+	s_wl16(s, (uint16_t)(u24 >> 8));
+}
+
+static inline void s_wl32(struct serializer *s, uint32_t u32)
 {
-	serialize(s, &u8, sizeof(uint8_t));
+	s_wl16(s, (uint16_t)u32);
+	s_wl16(s, (uint16_t)(u32 >> 16));
 }
 
-static inline void serializer_write_i8(struct serializer *s, int8_t i8)
+static inline void s_wl64(struct serializer *s, uint64_t u64)
 {
-	serialize(s, &i8, sizeof(int8_t));
+	s_wl32(s, (uint32_t)u64);
+	s_wl32(s, (uint32_t)(u64 >> 32));
 }
 
-static inline void serializer_write_u16(struct serializer *s, uint16_t u16)
+static inline void s_wlf(struct serializer *s, float f)
 {
-	serialize(s, &u16, sizeof(uint16_t));
+	s_wl32(s, *(uint32_t*)&f);
 }
 
-static inline void serializer_write_i16(struct serializer *s, int16_t i16)
+static inline void s_wld(struct serializer *s, double d)
 {
-	serialize(s, &i16, sizeof(int16_t));
+	s_wl64(s, *(uint64_t*)&d);
 }
 
-static inline void serializer_write_u32(struct serializer *s, uint32_t u32)
+static inline void s_wb16(struct serializer *s, uint16_t u16)
 {
-	serialize(s, &u32, sizeof(uint32_t));
+	s_w8(s, u16 >> 8);
+	s_w8(s, (uint8_t)u16);
 }
 
-static inline void serializer_write_i32(struct serializer *s, int32_t i32)
+static inline void s_wb24(struct serializer *s, uint32_t u24)
 {
-	serialize(s, &i32, sizeof(int32_t));
+	s_wb16(s, (uint16_t)(u24 >> 8));
+	s_w8  (s, (uint8_t)u24);
 }
 
-static inline void serializer_write_u64(struct serializer *s, uint32_t u64)
+static inline void s_wb32(struct serializer *s, uint32_t u32)
 {
-	serialize(s, &u64, sizeof(uint64_t));
+	s_wb16(s, (uint16_t)(u32 >> 16));
+	s_wb16(s, (uint16_t)u32);
 }
 
-static inline void serializer_write_i64(struct serializer *s, int32_t i64)
+static inline void s_wb64(struct serializer *s, uint64_t u64)
 {
-	serialize(s, &i64, sizeof(int64_t));
+	s_wb32(s, (uint32_t)(u64 >> 32));
+	s_wb32(s, (uint32_t)u64);
 }
 
-static inline void serializer_write_float(struct serializer *s, float f)
+static inline void s_wbf(struct serializer *s, float f)
 {
-	serialize(s, &f, sizeof(float));
+	s_wb32(s, *(uint32_t*)&f);
 }
 
-static inline void serializer_write_double(struct serializer *s, double d)
+static inline void s_wbd(struct serializer *s, double d)
 {
-	serialize(s, &d, sizeof(double));
+	s_wb64(s, *(uint64_t*)&d);
 }
 
 #ifdef __cplusplus