Open Broadcaster Software
Free, open source software for live streaming and recording
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
serializer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #pragma once
18 
19 #include "c99defs.h"
20 
21 /*
22  * General programmable serialization functions. (A shared interface to
23  * various reading/writing to/from different inputs/outputs)
24  */
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
34 };
35 
36 struct serializer {
37  void *data;
38 
39  size_t (*read)(void *, void *, size_t);
40  size_t (*write)(void *, const void *, size_t);
42  int64_t (*get_pos)(void *);
43 };
44 
45 static inline size_t s_read(struct serializer *s, void *data, size_t size)
46 {
47  if (s && s->read && data && size)
48  return s->read(s->data, (void*)data, size);
49  return 0;
50 }
51 
52 static inline size_t s_write(struct serializer *s, const void *data,
53  size_t size)
54 {
55  if (s && s->write && data && size)
56  return s->write(s->data, (void*)data, size);
57  return 0;
58 }
59 
60 static inline size_t serialize(struct serializer *s, void *data, size_t len)
61 {
62  if (s) {
63  if (s->write)
64  return s->write(s->data, data, len);
65  else if (s->read)
66  return s->read(s->data, data, len);
67  }
68 
69  return 0;
70 }
71 
72 static inline int64_t serializer_seek(struct serializer *s, int64_t offset,
73  enum serialize_seek_type seek_type)
74 {
75  if (s && s->seek)
76  return s->seek(s->data, offset, seek_type);
77  return -1;
78 }
79 
80 static inline int64_t serializer_get_pos(struct serializer *s)
81 {
82  if (s && s->get_pos)
83  return s->get_pos(s->data);
84  return -1;
85 }
86 
87 /* formatted this to be similar to the AVIO layout that ffmpeg uses */
88 
89 static inline void s_w8(struct serializer *s, uint8_t u8)
90 {
91  s_write(s, &u8, sizeof(uint8_t));
92 }
93 
94 static inline void s_wl16(struct serializer *s, uint16_t u16)
95 {
96  s_w8(s, (uint8_t)u16);
97  s_w8(s, u16 >> 8);
98 }
99 
100 static inline void s_wl24(struct serializer *s, uint32_t u24)
101 {
102  s_w8 (s, (uint8_t)u24);
103  s_wl16(s, (uint16_t)(u24 >> 8));
104 }
105 
106 static inline void s_wl32(struct serializer *s, uint32_t u32)
107 {
108  s_wl16(s, (uint16_t)u32);
109  s_wl16(s, (uint16_t)(u32 >> 16));
110 }
111 
112 static inline void s_wl64(struct serializer *s, uint64_t u64)
113 {
114  s_wl32(s, (uint32_t)u64);
115  s_wl32(s, (uint32_t)(u64 >> 32));
116 }
117 
118 static inline void s_wlf(struct serializer *s, float f)
119 {
120  s_wl32(s, *(uint32_t*)&f);
121 }
122 
123 static inline void s_wld(struct serializer *s, double d)
124 {
125  s_wl64(s, *(uint64_t*)&d);
126 }
127 
128 static inline void s_wb16(struct serializer *s, uint16_t u16)
129 {
130  s_w8(s, u16 >> 8);
131  s_w8(s, (uint8_t)u16);
132 }
133 
134 static inline void s_wb24(struct serializer *s, uint32_t u24)
135 {
136  s_wb16(s, (uint16_t)(u24 >> 8));
137  s_w8 (s, (uint8_t)u24);
138 }
139 
140 static inline void s_wb32(struct serializer *s, uint32_t u32)
141 {
142  s_wb16(s, (uint16_t)(u32 >> 16));
143  s_wb16(s, (uint16_t)u32);
144 }
145 
146 static inline void s_wb64(struct serializer *s, uint64_t u64)
147 {
148  s_wb32(s, (uint32_t)(u64 >> 32));
149  s_wb32(s, (uint32_t)u64);
150 }
151 
152 static inline void s_wbf(struct serializer *s, float f)
153 {
154  s_wb32(s, *(uint32_t*)&f);
155 }
156 
157 static inline void s_wbd(struct serializer *s, double d)
158 {
159  s_wb64(s, *(uint64_t*)&d);
160 }
161 
162 #ifdef __cplusplus
163 }
164 #endif
int64_t(* seek)(void *, int64_t, enum serialize_seek_type)
Definition: serializer.h:41
void * data
Definition: serializer.h:37
unsigned uint32_t
Definition: vc_stdint.h:31
size_t(* read)(void *, void *, size_t)
Definition: serializer.h:39
unsigned __int64 uint64_t
Definition: vc_stdint.h:33
unsigned char uint8_t
Definition: vc_stdint.h:27
size_t(* write)(void *, const void *, size_t)
Definition: serializer.h:40
Definition: serializer.h:33
Definition: serializer.h:36
Definition: serializer.h:31
int64_t(* get_pos)(void *)
Definition: serializer.h:42
Definition: serializer.h:32
__int64 int64_t
Definition: vc_stdint.h:32
serialize_seek_type
Definition: serializer.h:30
unsigned short uint16_t
Definition: vc_stdint.h:29